[英]How can I scroll to the div automatically when I click the button?
我在頁面頂部有一個按鈕,在中間有4個表,在頁面底部有一個div(id =“ MasterDiv”)。 單擊按鈕時如何自動滾動到div? 謝謝
您可以使用此函數(使用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');
});
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>
您可以嘗試以下庫: http : //demos.flesler.com/jquery/scrollTo/
沒有動畫基本的Javascript示例(使用ID代替name屬性)
window.location = "#MasterDiv";
jQuery動畫示例
$.scrollTo("#MasterDiv");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.