<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>锚点平滑滚动</title>
<style>
#d1{
background-color: #FFFCF0;
height: 400px;
}
#d2{
background-color: #44AA11;
height: 700px;
}
#d3{
background-color: #C34914;
height: 300px;
}
#d4{
background-color: #f43a89;
height: 1500px;
}
#d5{
background-color: #13f683;
height: 500px;
}
#d6{
background-color: #f43a89;
height: 700px;
}
.links{
position: fixed;
right: 30px;
top: 0;
}
.links > *{
display: block;
}
</style>
</head>
<body>
<div id="d1">d1</div>
<div id="d2">d2</div>
<div id="d3">d3</div>
<div id="d4">d4</div>
<div id="d5">d5</div>
<div id="d6">d6</div>
<div class="links">
<a href="#d1">d1</a>
<a href="#d2">d2</a>
<a href="#d3">d3</a>
<a href="#d4">d4</a>
<a href="#d5">d5</a>
<a href="#d6">d6</a>
</div>
<script>
document.querySelectorAll('a[href^="#"]').forEach(item => {
item.addEventListener('click', e => {
let target = document.querySelector(item.getAttribute('href'));
target.scrollIntoView({ behavior: 'smooth' });
e.preventDefault()
})
});
</script>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/10513.html