2 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個贊
讓它平穩(wěn)運(yùn)行的一種簡單方法是將毫秒設(shè)置為 0 而不是 800,并將該scroll-behavior: smooth;
屬性添加到html
標(biāo)記中。
html {scroll-behavior: smooth;}

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超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;
}
添加回答
舉報