jquery - jQuery 平滑滚动脚本不适用于下拉链接
问题描述
我正在进行单页设计,但在实现平滑滚动脚本时遇到了麻烦。
该脚本适用于简单的链接,甚至适用于没有下拉菜单的菜单项。
我已经尝试稍微修改脚本,甚至尝试了不同的脚本,但仍然是同样的问题。
jQuery
$(function() {
$('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
}, 1000);
return false;
}
}
});
});
HTML:
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#section-2" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> WER WIR SIND </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="https://www.domain.de">TEAM</a>
<a class="dropdown-item" href="http://www..domain.de">PARTNER</a>
</div>
</li>
如果有一个正常的链接,它可以工作,如果它有下拉切换类,它就不起作用。
工作链接示例:
<li class="nav-item"><a href="#section-7" class="nav-link"> KONTAKT </a>
解决方案
推荐阅读
- java - 方法内类型参数的Java泛型不兼容类型错误
- html - 由于被阻止的表格,如何在下面获得可点击的链接
- c# - 快速拖动矩形对象时,拖动停止
- php - Composer 库找不到依赖项
- php - is_file 函数总是为数组的第三个元素返回 false
- react-native - 无法在 react-native 中加载远程占位符图像
- azure-managed-identity - 为 Azure 资源配置托管标识
- apache-spark - 拆分 spark DataFrame 列
- ios - iOS / swift - swift中UIViewController中的常量
- graphql - 将 graphql 解析器文件拆分为单独的文件