Jquery:scrollTop()未设置为值

时间:2014-06-04 06:32:59

标签: javascript jquery html

我试图在点击链接时将页面滚动到页面中的ajax loader gif。 如下图所示我试图滚动到id为“divWaitImage”的div 但是,当将变量offs设为parm时,下面使用的scrollTop不起作用。

var offS = $("#divWaitImage").offset().top;
$('html, body').animate({ scrollTop: offS}, 'slow');

然而,如果我传入一个硬编码的值,它会起作用:

 $('html, body').animate({ scrollTop: 500}, 'slow');

任何人都可以指出错误或解决方法,我确信这是我错过的东西。

提前感谢任何指示!

1 个答案:

答案 0 :(得分:0)

根据您目前的问题,这可能是一个很好的方法。

http://jsfiddle.net/rRsM4/1/

这不是真的 - >您需要确保在父容器中设置了高度。你可以删除高度。

更新版本:http://jsfiddle.net/floradu88/rRsM4/2/

HTML:

<div style="height:1500px;">
    <img src="http://linux.m2osw.com/sites/linux.m2osw.com/files/images/waiting-wheel-300x300.gif" alt="image" style="margin-top:1000px;" id="divWaitImage"/>
</div>

JS:

var offS = $("#divWaitImage").offset().top;
$('html, body').animate({ scrollTop: offS}, 'slow');

P.S。如果您提供问题的更新,我会更新答案。