如何在15000px之后停止 - Javascript scrollTo

时间:2014-05-08 19:43:16

标签: javascript jquery-animate scrolltop scrollto

以下是一个示例:http://jsfiddle.net/jquerybyexample/pusqN/light/ 我想在15000px之后停止滚动动画

$(document).ready(function () {
var myInterval = false;
myInterval = setInterval(function () {
    var iScroll = $(window).scrollTop();
    if (iScroll + $(window).height() == $(document).height()) {
        clearInterval(myInterval);
    } else {
        iScroll = iScroll + 200;
        $('html, body').animate({
            scrollTop: iScroll
        }, 1000);
    }
}, 2000);

});

1 个答案:

答案 0 :(得分:0)

只需在iScroll + $(window).height() > 15000中添加条件if

...
if (iScroll + $(window).height() == $(document).height() ||
    iScroll + $(window).height() > 15000            // Stop after 15000px
   ) {
     clearInterval(myInterval);
}
...

Fiddle