2017-04-13 37 views
0

假设我有www.domain.com一个按钮,通过一个锚标记<a href="#destination">text link</a>禁用URL变化

链接到页面的下部是可以防止的URL浏览器的url栏显示为www.domain.com/#destination

感谢

+2

可能重复[追加/从当前的网址中删除锚点名称而不刷新](http://stackoverflow.com/questions/5928435/append-remove-anchor-name-from-current-url-without-refresh) –

回答

0

您可以使用此:

$("a.anchor").on("click", function(e) {  
 
    e.preventDefault(); 
 
    $(window).scrollTop($($(this).attr('href')).offset().top); 
 
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> 
 
<a href="#second" class="anchor">Second</a> 
 
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> 
 
<h2 id="second">Second Part</h2>

现在的锚链接显示悬停的地址,但滚动到目的地,而不是导航。

0

使用jQuery这样的:

$("#down").click(function() { 
    $('html,body').animate({ 
     scrollTop: $("#b").offset().top 
    }); 
}); 

$("#up").click(function() 
    $('html,body').animate({ 
     scrollTop: $("#a").offset().top 
    }); 
}) 

希望这有助于。

小提琴:

http://jsfiddle.net/ZGk5F/