最好的方法,我使用jQuery + jQuery用戶界面,目前有以下代碼: -jQuery的 - 爲「滾動錨」的同一頁面和新的頁面
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
我得到的滾動效應,當滾動到是在同一頁面上,但如果我加載一個新頁面,它不會滾動到該部分,例如from/home/to/about /#section3
如何在同一頁面錨點上同時工作,並在加載新頁面時滾動到錨點?
您是否嘗試過使用'$(window).load()'啓動平滑滾動? – ntgCleaner
爲什麼downvote?真的不知道有時 – nsilva
沒有一個好方法讓它在新頁面上滾動。您必須等待頁面完成加載後才能準確定位您希望滾動的位置,然後瀏覽器的默認跳轉到目標就已經發生。 –