单击锚点时,我希望进行平滑滚动,但是首先,我想检查锚点链接是否链接到id
。如果只是散列,则不要滚动。
喜欢:
<a href="#">do not scroll</a>
<a href="#anyID">Yes do the scroll</a>
我当前的代码滚动单击每个仅具有哈希值的锚点。
请修复我的代码,以便如果锚点仅具有哈希值时它不会滚动
$('a[href^="#"]').click(function (e) {
e.preventDefault();
var target = this.hash;
if (typeof($(target).offset()) != 'undefined') {
$('html, body').animate({
scrollTop: $(target).offset().top - 60
}, 1000);
}
});
如果确定每个锚点都存在一个不仅仅包含哈希的元素,则可以将选择器更改为:
$('a[href^="#"][href!="#"]')
显式测试元素的存在也很不错。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句