[英]Jquery Scroll on div using anchor tag is not Working properly
我添加了一個 jQuery 代碼來使用錨標記在不同的 div 上滾動,滾動有效,但是當它到達目標 div 時,它會滾動回頁面頂部。 這是我寫的代碼
$('a[href^="#"]').click(function(e) {
e.preventDefault();
var target = this.hash;
var $target = $(target);
$('html, body').animate({
'scrollTop': $target.offset().top - 160
}, 1000).stop();
});
有什么建議?
你可以這樣做:
$(document).ready(function(){
$("a").on('click', function(event) {
if (this.hash !== "") {
event.preventDefault();
var hash = this.hash;
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
window.location.hash = hash;
});
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.