,所以我想这应该很简单,但是我是新手,因为代码只运行两次,或者滚动两次,或者滚动两次,而不是我需要的任何数字,然后结束。
scrNum
是每次脚本在某些页面上运行时都不同的数字,并且我在页面加载时会得到它,通常是很小的数字。
也可能有一种更简单的方法来使用jQuery?
var i = 0;
function scroll(){
setTimeout(function(){
//scroll to the bottom of page
window.scrollTo(0, document.body.scrollHeight);
//if less than needed execute again
if( i < scrNum ){
i++;
scroll();
} else
window.scrollTo(0, document.head.scrollHeight);
}, 2000);
}
此代码应该起作用,请注意,可能有一种更好的方法来获得您想要的位置,我只是为一般的身体标签做到了。
var i = 0;
var myInterval = setInterval(function() {
if (i < scrNum) { //check if you need to scroll
$(window).scrollTop($('body').height()); // scroll to the bottom of body ( == the height of body)
i++;
} else {
$(window).scrollTop($('body').offset().top); //if you did all the scrolls that needed, scroll to the top of body
clearInterval(myInterval); // IMPORTANT- clear the interval that running.
}
}, 2000);