本文實例講述了jQuery實現平滑滾動到指定錨點的方法。分享給大家供大家參考。具體如下:
定義好指定的anchor錨點,調用下面的js代碼可以讓頁面平滑的滾動到指定的位置,非常實用,比如返回頁面頂部,去往頁面底部等功能
// HTML:
// <h1 id="anchor">Lorem Ipsum</h1>
// <p><a href="#anchor" class="topLink">Back to Top</a></p>
$(document).ready(function() {
$("a.topLink").click(function() {
$("html, body").animate({
scrollTop: $($(this).attr("href")).offset().top + "px"
}, {
duration: 500,
easing: "swing"
});
return false;
});
});
希望本文所述對大家的jQuery程序設計有所幫助。
|