【发布时间】:2021-10-30 02:44:31
【问题描述】:
我一直使用这个 jQuery 脚本来平滑滚动:
var root = $('html, body');
$('a[href^="#"]').click(function() {
root.animate({
scrollTop: $($.attr(this, 'href')).offset().top
}, 1000);
return false;
});
如何使用原生 JavaScript 做到这一点?我试过这个:
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
但它不适用于 Safari,这对我来说是一个交易破坏者。
【问题讨论】:
标签: javascript html smooth-scrolling