[英]Javascript/Jquery: code not repeating with any loop I've tried
Is there a reason why I am unable to loop the autoscroll function in the following code?是否有原因我无法在以下代码中循环自动滚动功能?
$(window).load(function(){
autoScroll();
});
function autoScroll(){
$("html, body").animate({ scrollTop: $(document).height() }, 22500,'linear');
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$(window).scrollTop(0);
}
});
}
I've already tried several answers to other simular questions on this site with no success.我已经在这个网站上尝试了几个其他类似问题的答案,但没有成功。
EDIT:编辑:
To put it simply I have two html pages, index.html and iframe.html.简单地说,我有两个 html 页面,index.html 和 iframe.html。 Index.html has a iframe pointing to iframe.html. Index.html 有一个 iframe 指向 iframe.html。 The above code is running on iframe.html.上面的代码在 iframe.html 上运行。 The above code scrolls down the webpage contained in the iframe.上面的代码向下滚动包含在 iframe 中的网页。 What I want it to do is go back to the top of the page after it finishes scrolling to the bottom and then repeat for 'X' amount of times.我想要它做的是在完成滚动到底部后返回页面顶部,然后重复“X”次。
I've already tried the following:我已经尝试过以下方法:
$(window).load(function(){
var i = 0;
do
autoScroll();
i++
while(i<10);
});
function autoScroll(){
$("html, body").animate({ scrollTop: $(document).height() }, 22500,'linear');
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$(window).scrollTop(0);
}
});
}
$(window).load(function(){
var i = 0;
while(i<10){
autoScroll();
i++
};
});
function autoScroll(){
$("html, body").animate({ scrollTop: $(document).height() }, 22500,'linear');
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$(window).scrollTop(0);
}
});
}
$(window).load(function(){
for(i<10; i<10; i++){
autoScroll();
};
});
function autoScroll(){
$("html, body").animate({ scrollTop: $(document).height() }, 22500,'linear');
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$(window).scrollTop(0);
}
});
}
$(window).load(function(){
setInterval(function(){
autoScroll();
}, 3000)
});
function autoScroll(){
$("html, body").animate({ scrollTop: $(document).height() }, 22500,'linear');
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$(window).scrollTop(0);
}
});
}
I've also tried all of the above shown above but around the contents of the autoScroll function like this:我也尝试了上面显示的所有内容,但围绕 autoScroll 函数的内容,如下所示:
$(window).load(function(){
autoScroll();
});
function autoScroll()
var i = 0;
do
$("html, body").animate({ scrollTop: $(document).height() }, 22500,'linear');
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
$(window).scrollTop(0);
}
});
i++
while(i<10);
}
Use a recursive function: by calling autoScroll();使用递归函数:通过调用 autoScroll(); after the animation is completed:动画完成后:
autoScroll();
function autoScroll(){
$("html, body").animate({ scrollTop: $(document).height() }, 22500,'linear',function(){
$(window).scrollTop(0);
autoScroll();
});
}
demo: https://jsfiddle.net/bbdsj2bg/1/演示: https : //jsfiddle.net/bbdsj2bg/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.