jQuery scrolltop Firefox 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17776544/
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
jQuery scrolltop firefox not working
提问by seanlevan
This script:
这个脚本:
function onscroll(){
document.getElementById("divs").style.top=""+$('body').scrollTop()+"px";
}
Fiddle: http://jsfiddle.net/Hed2J/1/
小提琴:http: //jsfiddle.net/Hed2J/1/
Doesn't work on the latest Firefox version!
不适用于最新的 Firefox 版本!
What am I doing wrong? :) Thanks for the help!
我究竟做错了什么?:) 谢谢您的帮助!
Edit:Edited with JSFiddle and full script :) as attached to an onscroll event
编辑:使用 JSFiddle 和完整脚本进行编辑 :) 作为附加到 onscroll 事件
回答by Lucas Willems
Try this fiddle, it is working in chrome, ie and the latest version of mozilla : http://jsfiddle.net/Hed2J/3/
试试这个小提琴,它在 chrome 中工作,即和最新版本的 mozilla:http: //jsfiddle.net/Hed2J/3/
I replace $('body').scrollTop()
with $(window).scrollTop()
.
我替换$('body').scrollTop()
为$(window).scrollTop()
.
回答by Thanh Trung
A little off-topic, but if you want to animate using scrolltop, you must do
有点跑题,但如果你想使用 scrolltop 制作动画,你必须这样做
$('html,body').animate({scrollTop:0}, 'slow');
Note that we target both html
and body
because html
will make scroll in Firefox works and body
for other browsers.
请注意,我们同时针对html
和body
因为html
将使 Firefox 和body
其他浏览器中的滚动有效。