2 回答
TA贡献1934条经验 获得超2个赞
让它平稳运行的一种简单方法是将毫秒设置为 0 而不是 800,并将该scroll-behavior: smooth;
属性添加到html
标记中。
html {scroll-behavior: smooth;}
TA贡献1871条经验 获得超8个赞
你可能想尝试这样的事情,
$(document).ready(function(){
// Add smooth scrolling to required elements, here I've considered all the buttons with
//class name 'button'
$(".button").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
});
});
或者,为整个文档添加平滑滚动行为。
html {
scroll-behavior: smooth;
}
添加回答
举报