2017-04-13 36 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/