我正在编写一个页面,我只想在 UI 中使用原始 JavaScript 代码,而不受任何插件或框架的干扰。
现在我正在努力寻找一种无需 jQuery 即可平滑滚动页面的方法。
最佳答案
JavaScript 中的原生浏览器平滑滚动是这样的:
// scroll to specific values,
// same as window.scroll() method.
// for scrolling a particular distance, use window.scrollBy().
window.scroll({
top: 2500,
left: 0,
behavior: 'smooth'
});
// scroll certain amounts from current position
window.scrollBy({
top: 100, // negative value acceptable
left: 0,
behavior: 'smooth'
});
// scroll to a certain element
document.querySelector('.hello').scrollIntoView({
behavior: 'smooth'
});
关于javascript - 不使用 jQuery 的平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10063380/