javascript 使 Div 固定在滚动上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9531754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a Div fixed on Scrolling
提问by sandbox
How to make a div fixed on detection of Scrolling by the Users.
Example: Right Sidebar of Facebook, it gets stuck when a certain scroll position is attained.
如何使 div 固定在用户检测滚动时。
示例:Facebook 的右侧边栏,到达某个滚动位置时它会卡住。
回答by Mr Lister
position:fixed
is the answer.
But you can always look at a website's source if you want to know how they do something. Very educational!
position:fixed
是答案。
但是如果你想知道他们是如何做某事的,你总是可以查看网站的来源。很有教育意义!
回答by Ohgodwhy
Monitor whether or not we're scrolling.
监视我们是否在滚动。
if($(window).scrollTop() > 0){
//we're scrolling our position is greater than 0 from the top of the page.
$("#element").css({'position' : 'fixed'});
}
*EDIT
*编辑
Do it without jQuery..
不用jQuery就可以做到..
if(window.scrollTop() > 0){
document.getElementById('element').style.position="fixed";
}
回答by Nikhil
I have come across this article which explains a solution https://www.virendrachandak.com/techtalk/make-a-div-stick-to-top-when-scrolled-to/
我遇到了这篇文章,它解释了一个解决方案https://www.virendrachandak.com/techtalk/make-a-div-stick-to-top-when-scrolled-to/
回答by Volmar
Not sure if this is what you mean?
不确定这是否是您的意思?
But you can add the CSS-propery position: fixed; to it to make it appear on the sam place even after scrolling.
但是你可以添加 CSS 属性 position: fixed; 使它即使在滚动后也能出现在相同的地方。
回答by Fireblaze
http://csswizardry.com/2012/02/pragmatic-practical-font-sizing-in-css/
http://csswizardry.com/2012/02/pragmatic-practical-font-sizing-in-css/
uses jQuery with a scroll lock plugin: https://gist.github.com/837729
使用带有滚动锁定插件的 jQuery:https: //gist.github.com/837729