jQuery的scrollTop()方法不工作 [英] jQuery scrollTop() method not working
问题描述
我有以下jQuery代码
I have the following jQuery code
$(document).ready(function () {
$('.navtoTop').click(function(){
$("html").scrollTop( $("#topofthePage").offset().top );
});
});
其中'navtoTop'是按钮的类(类似于'Back to top')页面左下角的固定位置和'topofthePage'是我页面最顶部的< div>
的标识。
where 'navtoTop' is the class of the button(something like 'Back to top') which has the fixed position in the bottom-left of the page and 'topofthePage' is the id of the <div>
at the most top of my page.
我甚至尝试过这种方式。
I have even tried this
$('.navtoTop').click(function(){
$('html, body').animate({scrollTop : 0},800);
return false;
});
这里是html代码
<body>
<div id="topofthePage"></div>
...
...
<img src="navtoTop.png" class="navtoTop">
</body>
我不知道发生了什么问题,但这不起作用。有人会解释并给出一个好的解决方案吗?
I don't know what is going wrong but this is not working. Will someone explain and give a good solution?
如果您想要,请告诉我代码的详细信息。
Ask me the detail of code if you want.
推荐答案
您必须使用窗口
而不是 html
:
$(window).scrollTop( $("#topofthePage").offset().top );
请注意,窗口
不应包含在引用作为对象而不是标签。
Note that window
should not be enclosed in quotes as i'ts an object and not a tag.
这篇关于jQuery的scrollTop()方法不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!