【问题标题】:Jquery setTimeout is not working properly in loopJquery setTimeout 在循环中无法正常工作
【发布时间】:2016-05-30 10:29:40
【问题描述】:

你好,我有一个页面,里面有三个 div,看起来像这样

每个 div 的高度为 500px 我想要做的是让屏幕每三秒自动滚动一次,直到到达下一个 div 位置,一旦到达最后一个 div 就返回并从头开始并无限执行这是我的j 查询代码

   $(document).ready(function(){
       myfunction();   
   });

   num = 0;
   function myfunction(){
   if(num == 1500)
    {
      num = 0;
    }

   setTimeout(function(){$('html,    body').animate({scrollTop:num}, "normal")},3000); 
   num = num + 500;
   myfunction(); 
  } 

【问题讨论】:

    标签: jquery loops settimeout


    【解决方案1】:
    setTimeout(function(){
      $('html,    body').animate({scrollTop:num}, "normal");  
      num = num + 500;
      myfunction(); 
    }
    ,3000); 
    

    【讨论】:

      【解决方案2】:

      试试这个

      $(document).ready(function () {
      
          var scrollAmount = 0;
          var divHeight = $("div").outerHeight();
          var pageHeight = $(document).outerHeight();
      
          var interval = setInterval(function () {
              if (pageHeight > scrollAmount) {
                  scrollAmount += divHeight;
              }
              else {
                  scrollAmount = 0;
              }
              $("html, body").animate({
                  scrollTop: scrollAmount
              });
      
          }, 2000);
      });
      

      【讨论】:

        猜你喜欢
        • 2013-06-26
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 2019-01-01
        相关资源
        最近更新 更多