0
我想让我的页面滚动到特定的<section>
点击一个href。我已经能够使页面跳转到我想要的<section>
,但它不光滑(这就像我的.animate不在jQuery中工作)。jQuery滚动不是动画
HTML
<li><a href="#about" class="about-link">about us</a></li>
<section class="about" id="about">
<div class="container">
<h1>about us</h1>
<p>text</p>
</div>
</section>
jQuery的
$(document).ready(function(){
// Add smooth scrolling to all links
$(".about-link").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
});
});
提前感谢!
我用你的jQuery,它将我移动到''上,但它仍然不是动画,只是瞬间跳转。此外,我试图用href而不是按钮来做到这一点。 –
所以这个滚动顺利,所以为什么你的一个。你是否正确地给出了'top'的位置。 –
我相信它确实拥有正确的最高价值,因为它跳到了我应该点击链接的位置,但它仍然没有顺利进行 –