我希望默认情况下每个 href anchor 都平滑滚动(这有效!)...并手动防止其中一些不平滑滚动。
有什么聪明的方法可以做到这一点吗?也许是一个“不可滚动”的 CSS 类作为目标?它会是什么样子?
jQuery( function ( $ ) {
'use strict';
// Smooth scroll on anchor links
$('a[href^="#"]').on('click', function(e) {
e.preventDefault();
var target = this.hash;
var $target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top-20
}, 900, 'swing', function() {
window.location.hash = target;
});
});
});
使用此脚本,所有 html anchor 都可以平滑滚动。好!
一些特定的 anchor 不应该平滑滚动。这是需要的。
最佳答案
只需将一个新类添加到您想要禁用的 a
标记中即可:
<a class="noscroll" href="# ...
并将其更改为:
$('a[href^="#"]').not(".noscroll").on(...
关于jquery - 有没有办法禁用某些 html anchor 的 jQuery 滚动动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54042569/