Javascript jQuery:Make自动滚动Div
谁能告诉我如何实现滚动“侧边栏”跟随用户滚动页面,但在到达“页脚”时停止Javascript jQuery:Make自动滚动Div,javascript,jquery,sidebar,Javascript,Jquery,Sidebar,谁能告诉我如何实现滚动“侧边栏”跟随用户滚动页面,但在到达“页脚”时停止 <div id="section"> <div id="sidebar"> </div> <div id="hello"> </div> </div> <div id="footer"> </div> Div'section'没有设置的高度,这意味着每当id为hello的Div增长时,它就会增长。这意味着“
<div id="section">
<div id="sidebar"> </div>
<div id="hello"> </div>
</div>
<div id="footer"> </div>
Div'section'没有设置的高度,这意味着每当id为hello的Div增长时,它就会增长。这意味着“hello”和“section”都没有设置的高度
Div“section”的宽度为728,“hello”的宽度为400边栏的宽度为200
我希望发生的是(使用jquery),当用户滚动到侧边栏的上方时,侧边栏应该与页面一起滚动。侧边栏上方不应与页脚重叠
因此,侧边栏应该只沿着页面滚动到div部分的末尾
红色块(在my上)是想要滚动的侧边栏。下面的内容应该可以让您开始(仅测试CHROME): JS HTML
你做过jQuery吗?如果不是,你应该先做那项研究。如果您确实了解jQuery,那么您可以发布您尝试过的内容吗。谢谢。这需要JQuery吗?你不能只使用固定CSS定位吗?你看过scroll()和scrollTop()吗?@Brendan,要使跨浏览器兼容,它必须是。抱歉,但这不是我要找的我只是希望侧边栏在用户滚动时定位固定,然后我希望它在div部分超出范围时定位固定,就像您描述的是实现细节而不是需求一样。我再次阅读了你的问题,我相信上面的内容产生了预期的效果(尽管可能不是预期的实现)。我会用我自己的话重申你想要的。“滚动页面时,让侧边栏(红色方框)跟随页面,直到到达页脚(黄色方框)。”您能否提供更多详细信息,说明您不喜欢此实现的原因或其他要求。谢谢。是的,请再看一次。请确保在访问Through或hole shift之前删除缓存,然后按web浏览器上的“重新加载”按钮。所以我想让红色块向下滚动页面基本位置,然后我想让红色块不与页脚重叠,意思是保持在页脚的顶部,基本上是相对位置
var $sb = $('#sidebar');
var $foot = $('#footer');
var footerTop = $foot.offset().top;
$foot.html(footerTop);
$(document).scroll(function(){
//+ 100 since the height of the sidebar is 100px
if($(this).scrollTop() + 100 > footerTop){
//when we get close, line up perfectly with the footer
$sb.css({top:footerTop - 100});
}else{
//otherwise scroll with the page
$sb.css({top:$(this).scrollTop()});
}
//Visualy display the position of the bottom edge of the sidebar
$sb.html($sb.offset().top + 100)
});
<div id="section">
<div id="sidebar"> </div>
<div id="hello"> </div>
</div>
<div id="footer"> </div>
#section{
vertical-align:top;
}
#sidebar, #hello{
display:inline-block;
position:relative;
vertical-align:top;
top:0px;
left:0px;
}
#sidebar{
height:100px;
width:50px;
background-color:red;
}
#hello{
height:900px;
width:50px;
background-color:green;
}
#footer{
height:450px;
width:100px;
background-color:yellow;
}