2009-01-23 60 views

回答

22

快速演示here

基本上所有你需要的是

$('html, body').animate({ 
     scrollTop: $('#yourDiv').offset().top 
    }, 3000); 
+0

呀,似乎工作。但是,如果再次點擊,它會給頁面一個混亂,並嘗試再次生成動畫。有沒有辦法檢查div是否已經可見而不是動畫? – 2009-01-23 14:03:54

+0

JimmyP幫助下面! – redsquare 2009-01-23 14:17:40

8
$.extend($.expr[':'],{ 
    inView: function(a) { 
     var st = (document.documentElement.scrollTop || document.body.scrollTop), 
      ot = $(a).offset().top, 
      wh = (window.innerHeight && window.innerHeight < $(window).height()) ? window.innerHeight : $(window).height(); 
     return ot > st && ($(a).height() + ot) < (st + wh); 
    } 
}); 

if ($('#whatever').is(':not(:inView)')) { 
    $('html,body').animate({ 
     scrollTop: $('#whatever').offset().top 
    }, 3000); 
} 
相關問題