简体   繁体   English

Javascript/Jquery:代码不重复我尝试过的任何循环

[英]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.

相关问题 此javascript代码在没有for循环的情况下可以正常运行,但是没有循环。任何帮助,我都尝试了一切 - This javascript code is running properly without the for loop but not with it.Any help, I have tried everything 为什么我的 javascript 不能重定向以下代码,我尝试了多种解决方案,我在 StackOverFlow 中找到了 - Why can't my javascript redirect the following code, I've tried multiple solution that i found in StackOverFlow 如何避免在 javascript for 循环中重复代码? - How do I avoid repeating code in a javascript for loop? JavaScript classList不起作用。 我尝试了在这里看到的各种方法 - JavaScript classList is not working. I've tried various methods I've seen on here 试图重复任何 string.ex-"hello" --&gt; 'hheelloo' 的双字母但卡在 for 循环中,有什么解决方案吗? - tried to repeating the double letter of any string.ex- "hello" --> 'hheelloo' but stucked at for loop, any solution? jQuery添加一个类-我尝试过的所有方法均会在单击时删除该类 - jQuery adding a class - all methods I've tried remove the class on click 完成一个手风琴 jQuery 功能,我尝试了很多,似乎没有任何效果? - Completing an accordion jQuery feature, I've tried a lot, nothing seems to work? 我试过在 JavaScript 中设置我的 Canvas 但它不起作用 - I've tried setting up my Canvas in JavaScript and it isn't working 如何在macOS中使用脚本将XLSB文件转换为XLSX或CSV? 我尝试过R和JavaScript失败了吗? - How to convert XLSB files to XLSX or CSV with a script in macOS? I've tried R and JavaScript without success? 我编写的JQuery代码停止工作了,是否有我忽略的错误? - JQuery code I wrote stopped working, is there anywrong I've overlooked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM