我正在向我的一页滚动站点(index.html)添加2个HTML文件。我对Navbar(Bootstrap)有问题。当单击" FAQ"的"预订表格"时,由于随附的index.html上的滚动功能,页面未转移到所选的HTML文件。我该如何解决?
我尝试在#home,#service,#corporate和#contact链接中添加类,并将同一类添加到" Navbar A"背后的jQuery函数。它是这样起作用的,但是在index.html上的滚动消失了。
index.html navbar:
<body data-spy="scroll" data-offset="80">
<div id="nav" class="navbar navbar-default navbar-fixed-top menu-top">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="index.html" class="navbar-brand">LOGO</a>
</div>
<div class="navbar-collapse collapse">
<nav>
<ul class="nav navbar-nav navbar-right">
<li class="active"><a class="page-scroll" href="#home">Home</a></li>
<li><a class="page-scroll" href="#service">Services</a></li>
<li><a class="page-scroll" href="#corporate">Corporate</a></li>
<li><a class="page-scroll" href="#contact">Contact</a></li>
<li><a href="bookingform.html">Booking Form</a></li>
<li><a href="faqs.html">FAQ</a></li>
</ul>
</nav>
</div>
</div>
<!--- END COL -->
</div>
<!--- END COL -->
</div>
<!--- END CONTAINER -->
</div>
faqs.html navbar:
<body data-spy="scroll" data-offset="80">
<div id="nav" class="navbar navbar-default navbar-fixed-top menu-top">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse"
data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="index.html" class="navbar-brand">LOGO</a>
</div>
<div class="navbar-collapse collapse">
<nav>
<ul class="nav navbar-nav navbar-right">
<li><a href="index.html#home">Home</a></li>
<li><a href="index.html#service">Services</a></li>
<li><a href="index.html#corporate">Corporate</a></li>
<li><a href="index.html#contact">Contact</a></li>
<li><a href="bookingform.html">Booking Form</a></li>
<li class="active"><a href="faqs.html">FAQ</a></li>
</ul>
</nav>
</div>
</div>
<!--- END COL -->
</div>
<!--- END COL -->
</div>
<!--- END CONTAINER -->
</div>
JS File的jQuery函数:
$(document).ready(function () {
$('select').niceSelect();
// Add smooth scrolling to all links in navbar + footer link
$(".navbar a, footer a[href='#myPage'], .overlay-detail a").on('click', function (event) {
// 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 (900) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 900, function () {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
});
这是在navbar中单击"预订"或"常见问题"选项卡时我遇到的错误:
Uncaught TypeError: Cannot read property 'top' of undefined app.js:17
at HTMLAnchorElement.<anonymous> (app.js:17)
at HTMLAnchorElement.dispatch (jquery.min.js:3)
at HTMLAnchorElement.r.handle (jquery.min.js:3)
您的某些元素给出哈希=";您可以尝试以下修改您并尝试
{
var currentpage = window.location.pathname.split("/").pop();
var nextpage = this.href.split("/").pop();
if ("".search("#" > -1)) {
nextpage = nextpage.split("#")[0];
}
if (this.hash != "" && currentpage == nextpage) {
//prevent default only if required
// pages are different AND has hash
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (900) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 900, function () {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
}
else
{
//
}
}
尝试此
添加以下内容,在您需要此效果
的文档中添加以下内容从两个附加的HTML文件返回index.html时要进行更平滑的过渡
var hash=location.hash;
if (location.hash!='') {
window.scrollTo(0,0);
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 900, function () {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
}
```