【问题标题】:set time interval for each div为每个 div 设置时间间隔
【发布时间】:2016-08-21 07:54:51
【问题描述】:

这是我的代码:Demo

该演示在手动滚动每个 div 到 scrolltop 时运行良好。

我需要的是:如果我单击Auto Start 按钮,我想自动滚动 1,自动滚动 2,...自动滚动 n 每个 div 到 scrolltop。

  $(".jumper").on("click", function() { 
            var links = $(this).attr('href');
            var type = links.substring(links.indexOf('#')+1);

          $("body, html").animate({            
               scrollTop: $('#'+type).offset().top  
            }, 1500);   

           });

每个 div 应该到达 scrolltop 并停止,然后以相同的时间间隔转到下一个 div scrolltop。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    我就是这样做的:

    $(".autostart").on("click", function() {
        scrollToElem($("#auto-scroll"));
    
        var scrollList = $("#auto-scroll").nextAll();
        var current = 0;
        time = setInterval(function() {
          scrollToElem($(scrollList.get(current)));
          current++;
          if (scrollList.length == current) {
            clearInterval(time);
          }
        }, 2000);
      });
    

    Here is the JSFiddle demo

    【讨论】:

      【解决方案2】:

      您的代码中有错误。 .top 未定义。您可以使用链接作为选择器,因为它同时包含 idselector + id :

      $(".jumper").on("click", function() { 
          var links = $(this).attr('href');
          $("body, html").animate({            
              scrollTop: $(links).offset().top  
          }, 1500);   
      });
      

      【讨论】:

        猜你喜欢
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        • 2013-08-04
        • 2022-01-25
        • 2023-01-12
        • 2023-03-05
        • 2012-04-06
        • 1970-01-01
        相关资源
        最近更新 更多