jQuery scrollTop()在Firefox中的'body'元素上不起作用 [英] jQuery scrollTop() not working on 'body' element in Firefox
问题描述
我不明白为什么scrollTop()jQuery函数在Firefox的'body'元素上不起作用.
I do not understand why the scrollTop() jquery function is not working on the 'body' element on Firefox.
$('body').scrollTop(0);
我使用以下方法解决了问题:
I fixed my issue using:
$(window).scrollTop(0);
但是,根据jquery文档,scrollTop()应该适用于所有示例中的元素:
However according to the jquery documentation scrollTop() is supposed to work on all elements like in this example:
$( "div.demo" ).scrollTop( 300 );
我也用'nav'和'main'进行了测试,但是它们也不起作用.
I have also tested with 'nav' and 'main' but it is not working either.
推荐答案
滚动
$(window).scrollTop(0);
.
动画滚动
如果要对滚动进行动画处理,如果使用window
对象(已测试1.11.2),则jQuery返回错误.相反,要制作滚动动画,最好同时使用html
和body
来涵盖使用其中任一引擎的引擎.所以:
If you want to animate a scroll, jQuery returns an error if using the window
object (1.11.2 tested). Instead, to animate a scroll, it's best to use both html
and body
to cover engines which utilise either one. So:
$('html, body').animate({scrollTop:0},500);
将在半秒钟内滚动到浏览器顶部.
$('html, body').animate({scrollTop:0},500);
will scroll to the top of the browser in half a second.
滚动位置
您不能使用$('html,body').scrollTop()
查找页面的当前滚动位置-至少Chrome不支持此功能(始终返回0).相反,为了始终如一地找到页面的滚动位置,必须使用$(window).scrollTop();
.
You cannot use $('html,body').scrollTop()
to find the current scroll position of the page - at least Chrome doesn't support this (always returns 0). Instead, to consistently find the scroll position of a page, it's necessary to use $(window).scrollTop();
.
这篇关于jQuery scrollTop()在Firefox中的'body'元素上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!