2012-07-10 28 views
0

我找到了Mary Lou的代碼http://tympanus.net/codrops/2010/06/02/smooth-vertical-or-horizontal-page-scrolling-with-jquery 我嘗試在5秒後自動添加動畫。不幸的是,無濟於事。jquery:添加settime函數

我可以尋求幫助,並添加這些行。我不是程序員,也不知道我還有什麼奇怪的技巧。

$(function() { 
      $('ul.nav a').bind('click',function(event){ 
       var $anchor = $(this); 
        $('html, body').stop().animate({ 
      scrollLeft: $($anchor.attr('href')).offset().left 
      }, 1000); 
      event.preventDefault(); 
      }); 
     }); 

THX 德德

回答

1

animate內的1000將動畫的持續時間,所以如果你想等待動畫明星之前,您應該使用:delay(5000)這樣的:

$(function() { 
      $('ul.nav a').bind('click',function(event){ 
       var $anchor = $(this); 
        $('html, body').stop().delay(5000).animate({ 
      scrollLeft: $($anchor.attr('href')).offset().left 
      }, 1000); 
      event.preventDefault(); 
      }); 
     }); 

如果delay不起作用,請嘗試寫入idle而不是延遲。

如果你希望動畫點擊或5秒,頁面加載後rihgt工作時,你應該這樣做:

$(document).ready(function() { 
     $('ul.nav a').bind('click',function(event){ 
      var $anchor = $(this); 
      $('html, body').stop().animate({ 
       scrollLeft: $($anchor.attr('href')).offset().left 
      }, 1000); 
      event.preventDefault(); 
     }); 
     var $anchor = $('ul.nav a'); 
     $('html, body').stop().delay(5000).animate({ 
      scrollLeft: $($anchor.attr('href')).offset().left 
     }, 1000); 
     event.preventDefault(); 

}); 

希望這是有幫助的!

+0

我認爲這是錯誤的。嘗試更改後,看起來當您單擊動畫後1秒開始。我希望動畫獨立於點擊而工作。圖像必須在5秒後自動移動或點擊。 對不起,我的英語 - 寫的谷歌翻譯 – dEDe 2012-07-10 12:30:49

+0

我的英文主要是由谷歌翻譯...你寫了你的代碼在$(document).ready(function(){.......}'? (無論點擊次數如何) – ParPar 2012-07-10 12:41:55

+0

它幾乎可以工作,下面是一個演示 - http://tympanus.net/Tutorials/WebsiteScrolling/。現在它只在點擊時移動,我希望它在5秒後自動移動,當你到達最後一張幻燈片時,它應該返回到開頭並重復動畫。 – dEDe 2012-07-10 13:05:42