最好的方法,我使用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
没有一个好方法让它在新页面上滚动。您必须等待页面完成加载后才能准确定位您希望滚动的位置,然后浏览器的默认跳转到目标就已经发生。 –