html - 为网站上的锚链接提供平滑滚动,但现在无法访问外部链接
问题描述
我的网站上有一个有趣的问题。
我将为您提供该网站的 URL,然后如果您按照以下说明操作,您可以看到我的问题。
指示:
- 加载网站
- 在导航栏菜单上,选择任何链接,您可以看到它执行平滑滚动。可以选择该导航栏菜单中的任何链接,并且它在滚动到页面上的相关部分时按预期工作。
- 我们现在要访问外部链接,因此请执行以下操作:从导航栏菜单中选择“课程”,然后选择“订购我们的在线课程和考试包”。您应该被带到作为外部页面的相关页面。
- 现在在该页面上,选择任何导航栏链接,您可以看到没有任何反应,并且控制台显示一个错误,指出它找不到#top。
这就是我面临的问题。我希望平滑滚动工作,但这样做会破坏主页外部页面上的导航栏链接。
我希望导航栏链接能够正常工作,以便如果用户在任何外部页面上并选择任何导航栏链接,它将把用户带到主页上网站的正确锚点。
下面是执行平滑滚动的代码:
头.php:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
// Add smooth scrolling to all links
$('a[href^="/../#"]').on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
</script>
头文件.php
<!-----NavigationBar---->
<section id="top">
</section>
<section id="nav-bar">
<nav class="navbar navbar-expand-lg navbar-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="/../#top">HOME</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/../#about">ABOUT</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/../#courses">COURSES</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/../#services">SERVICES</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/../#testimonials">TESTIMONIALS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/../#contact">CONTACT</a>
</li>
</ul>
</div>
</nav>
</section>
home.php(这是主页,包含平滑滚动到的锚链接,我包括一个按钮,可以将用户带到外部页面,就像说明一样):
<html>
<head>
<?php include "./head.php" ?>
</head>
<body>
<?php include "./header.php" ?>
<!-------About------->
<section id="about">
...
</section>
<!-------Courses------->
<section id="courses">
...
<a href="courses/course-BRFAITC009">
<button type="button">Order online course and exam bundle</button>
</a>
...
</section>
<!-------Services------->
<section id="services">
...
</section>
<!-------Testimonials------->
<section id="testimonials">
...
</section>
<!-------Contacts------->
<section id="contacts">
...
</section>
...
</body>
</html>
目录->课程/课程-BRFAITC009.php
<html>
<head>
<?php include "./head.php" ?>
</head>
<body>
<?php include "./header.php" ?>
...
</body>
</html>
解决方案
问题是您的 jquery 代码在 DOM 中查找锚点,但子页面上不存在锚点,因此在浏览器控制台中会失败。您可能需要添加以下内容:
if(window.location.pathname === "/") { ... rest of jquery }
仅在主页上运行此代码。
推荐阅读
- php - 如何计算具有相同值但不同id的行
- excel-formula - 如果单元格包含不是数字或连字符的任何字符,则 Excel 公式的语法?
- excel - 使用 VBA 从 XML 中获取属性名称
- angular - 无法解析 TemlComponent 的所有参数:(?)。在 syntaxError (compiler.js:1016)
- css - 语义 UI RTL 设置问题?
- elasticsearch - elasticseach 6滚动,第二次获取不可能
- javascript - 如何使用 jQuery 插入多个 HTML 元素
- batch-file - 如何在文件大小最大的文件夹中获取 *.docx 文件的文件名?
- javascript - JavaScript 正则表达式不匹配
- java - Materialviewpager 找不到引用的方法