js和jQuery两种方式实现:点击按钮,平滑滚动到锚点处
warning:
这篇文章距离上次修改已过201天,其中的内容可能已经有所变动。
JavaScript原生方式:
document.querySelector('#myButton').addEventListener('click', function() {
document.querySelector('#anchor').scrollIntoView({
behavior: 'smooth'
});
});
jQuery方式:
$('#myButton').on('click', function() {
$('#anchor').get(0).scrollIntoView({
behavior: 'smooth'
});
});
在这两个例子中,当按钮被点击时,页面会平滑滚动到id为anchor
的元素位置。
评论已关闭