2016-05-30 48 views
0

你好我有有它內部的三個div的頁面看起來像這樣enter image description herejQuery的的setTimeout不環路正常工作

每個div有500px的高度是什麼我試圖做的是讓屏幕自動滾動每次三秒鐘,直至到達下一個DIV位置,一旦它到達最後一個div回去從頭開始,並無限這裏做,這是我Ĵ查詢代碼

$(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(); 
    } 

回答

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

非常感謝,它現在正在工作 –

0

試試這個

$(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); 
});