[英]jquery smooth scroll on click (both directions, down and up)
我有一個基本腳本,當用戶點擊它們時,它會平滑滾動到錨定鏈接。 我的問題是平滑滾動似乎只能以一種方式工作,當我單擊“轉到頂部”鏈接之一時,頁面只是跳轉...
$(function(){
// Smooth scrolling for anchored links
$('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
}, 2000);
return false;
}
}
});
// Scroll down automatically on page load
$('html, body').animate({
scrollTop: $('.destination').offset().top
}, 2000);
});
https://jsfiddle.net/p70d4doq/22/
任何幫助將非常感激!
謝謝,安德烈亞斯
你忘了添加這一點:
$('a[href="#"]').click(function () {
$('html, body').animate({
scrollTop: 0
}, 2000);
});
請不要給一個不起作用的小提琴! 在發布鏈接之前檢查。
工作小提琴: https : //jsfiddle.net/a5u0zzLr/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.