Javascript 防止使用scrollTop锚链接更新URL
我有下面的JS,但我不想用散列更新URL,我怎样才能防止这种情况 我希望scrollTop既能用于相同的页面锚链接,也能在链接到当前工作正常的不同页面上的ID时工作Javascript 防止使用scrollTop锚链接更新URL,javascript,scrolltop,Javascript,Scrolltop,我有下面的JS,但我不想用散列更新URL,我怎样才能防止这种情况 我希望scrollTop既能用于相同的页面锚链接,也能在链接到当前工作正常的不同页面上的ID时工作 var jump=function(e) { if (e){ e.preventDefault(); var target = $(this).attr("href"); } else { var target = location.hash; } $('html,body').an
var jump=function(e)
{
if (e){
e.preventDefault();
var target = $(this).attr("href");
} else {
var target = location.hash;
}
$('html,body').animate(
{
scrollTop: $(target).offset().top
}, 2000, function()
{
location.hash = target;
});
}
$('html, body').hide();
$(document).ready(function()
{
$('a[href^=#]').bind("click", jump);
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show();
jump();
}, 0);
}else{
$('html, body').show();
}
});
将window.location.hash=''添加到跳转函数:
... //previous code
window.location.hash = ''
...
这将从导航栏中的url中删除哈希
编辑:另外,在跳转函数的末尾添加“return false”。
这将阻止事件传播和更改地址栏中的哈希值我尝试过,小提琴和哈希值没有显示在地址栏中。什么功能不正常?