[英]Making a Div fixed on Scrolling
如何使 div 固定在用户检测滚动时。
示例:Facebook 的右侧边栏,到达某个滚动位置时它会卡住。
position:fixed
就是答案。
但是,如果您想知道网站的工作方式,则可以随时查看网站的来源。 很有教育意义!
监视我们是否正在滚动。
if($(window).scrollTop() > 0){
//we're scrolling our position is greater than 0 from the top of the page.
$("#element").css({'position' : 'fixed'});
}
* 编辑
不用jQuery就可以做到。
if(window.scrollTop() > 0){
document.getElementById('element').style.position="fixed";
}
它接缝你正在寻找的position: sticky;
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.