gpt4 book ai didi

jquery - 平滑 anchor 链接滚动冲突

转载 作者:行者123 更新时间:2023-12-01 07:14:52 29 4
gpt4 key购买 nike

我使用下面的代码在 anchor 链接上平滑滚动

jQuery(function() {
jQuery('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = jQuery(this.hash);
target = target.length ? target : jQuery('[name=' + this.hash.slice(1) +']');
if (target.length) {
jQuery('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});

但是,这与我用来显示信息的一些隐藏 div 发生冲突。这是一个例子。

<div style="display:none;">
<div id="contact-email" >
[gravityform id=15 ajax=true title=false description=false tabindex=20]
</div>
</div>

如果我使用上面的代码,隐藏的 div 根本不会显示。

是否可以排除与我的隐藏 div 相对应的 anchor 链接 - 我只有几个..

最佳答案

好的解决方案是使用:

$(".scroll").click(function(event){     
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});

然后使用

<a href="#comments" class="scroll">Scroll to comments</a>

对于我希望平滑滚动的 anchor 链接。

我只是想知道这段代码的效率和浏览器兼容性如何。

关于jquery - 平滑 anchor 链接滚动冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305018/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com