2011-02-16 181 views
0

我在頁面頂部有一個按鈕,中間有4個表格,底部有一個div(id =「MasterDiv」)。當我點擊按鈕時,如何自動滾動到div? 感謝當我點擊按鈕時,如何自動滾動到div?

+0

可能重複: http://stackoverflow.com/questions/2659354/jquery-scroll-down-page-a-set-increment-in-pixels-on-click – DeaconDesperado 2011-02-16 20:40:24

回答

6

您可以使用此功能(使用jQuery的.animate

function scrollToElement(selector, callback){ 
    var animation = {scrollTop: $(selector).offset().top}; 
    $('html,body').animate(animation, 'slow', 'swing', function() { 
     if (typeof callback == 'function') { 
      callback(); 
     } 
     callback = null; 
    }); 
} 

你這樣稱呼它:

scrollToElement('#MasterDiv'); 

或者,如果你想有一個回調:

scrollToElement('#MasterDiv', function(){ 
    alert('Page scrolled'); 
}); 
+0

這個函數是從我問的問題:http://stackoverflow.com/questions/5019951 – 2011-02-16 20:48:34

2
Example 
A named anchor inside an HTML document: 

<a name="tips">Useful Tips Section</a> 
Create a link to the "Useful Tips Section" inside the same document: 

<a href="#tips">Visit the Useful Tips Section</a> 
Or, create a link to the "Useful Tips Section" from another page: 

<a href="http://www.w3schools.com/html_links.htm#tips"> 
Visit the Useful Tips Section</a> 

source

相關問題