【问题标题】:Jquery Scrolling div - Prevent from entering site footerJquery Scrolling div - 防止进入网站页脚
【发布时间】:2011-02-25 21:55:42
【问题描述】:

我的网站上有一个侧边菜单,我希望它始终可见。
为此,我正在使用此页面中的以下代码示例:

http://camwebdesign.com/demos/jquery-scrolling-element.html

问题在于,与示例不同,我的网站有一个巨大的 1000 像素高度的页脚。 当用户在页脚 div 上滚动内容时,侧边菜单会覆盖它。 有没有办法修改底部有 1000px 边界的 Jquery 代码来防止这种情况发生?

谢谢!

<html> 

<title>Keep element in view while scrolling using JQuery</title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js?ver=CDN"></script> 
<script> 
    $().ready(function() {
        var $scrollingDiv = $("#scrollingDiv");

        $(window).scroll(function(){            
            $scrollingDiv
                .stop()
                .animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow" );          
        });
    });
</script> 
<div style="float: left; width: 70%"> 

<h1>Keep element in view while scrolling using JQuery</h1> 

<div id="scrollingDiv" style="position: absolute;   top: 32px; right: 16px; width: 21%; padding: 0% 2% 2% 2%; border: 2px solid red; background-color: #ffeaea;"> 
    <h2>Scrolling Element</h2> 
    Scroll down/up to see me smoothly reposition myself and keep in view.<br /><br /> 

    <em>Smooooooooooooooooooth</em><br /> 

</div> 


 <div style="clear:both;"></div>

 <div id="footer" style="width:100%; height:1000px; background:#333; color:#FFF;" >
 <h1>Footer</h1>





 </div>

【问题讨论】:

  • 你能发布你的代码吗?将更容易帮助您拥有的东西。
  • 当然,刚刚用代码更新了它...,它与问题中的链接几乎相同,但在底部添加了两个 div。一个清除两者,然后一个用于页脚。
  • 只添加 if/else 子句,您可以在其中检查 "$(window).height() - $scrollingDiv.offset()
  • 不确定该放在哪里?能详细点吗?
  • 在.scroll()函数之上,我会尝试做一个代码示例

标签: jquery css scroll overlap


【解决方案1】:

演示

http://jsfiddle.net/NsfwM/

全屏 http://jsfiddle.net/NsfwM/embedded/result/

JS

var $scrollingDiv = $("#scrollingDiv");

$(window).scroll(function(){            
    var y = $(this).scrollTop(),
        maxY = $('#footer').offset().top,
        scrollHeight = $scrollingDiv.height();

    if(y< maxY-scrollHeight ){
        $scrollingDiv
        .stop()
        .animate({"marginTop": ($(window).scrollTop()) + "px"}, "slow" );        
    }    
});

还有一个有 30px 偏移的 http://jsfiddle.net/NsfwM/1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多