【发布时间】:2017-04-05 14:29:00
【问题描述】:
我已经构建了一个侧列表菜单,需要平滑滚动和滚动间谍到页面的不同部分。我的链接跳转到锚链接,但我似乎无法平滑滚动和滚动间谍工作。我正在使用引导程序和 wordpress。
这是我的 HTML:
<div class="page-menu">
<ul class="page-menulist">
<li class="menuitem active">
<a href="#S1"></a>
</li>
<li class="menuitem">
<a href="#S2"></a>
</li>
<li class="menuitem">
<a href="#S3"></a>
</li>
<li class="menuitem">
<a href="#S4"></a>
</li>
<li class="menuitem">
<a href="#S5"></a>
</li>
<li class="menuitem">
<a href="#S6"></a>
</li>
</ul>
</div>
这是我的 JS:
$(document).ready(function() {
// Add smooth scrolling to all links
$("a").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
});
});
【问题讨论】:
-
问题不复现时很难调试代码。您能否使用 sn-p 工具或您选择的 sn-p 网站为您的问题创建一个minimal reproducible example?
标签: jquery wordpress twitter-bootstrap smooth-scrolling scrollspy