<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
scroll-behavior: smooth;
}
</style>
</head>
<body>
<a href="#destination">Click me: I’m <em>smoooooth</em>.</a>
… <div style="height: 2000px"></div>
<p id="destination">This is the target, further down the page.
<script>
var anchorLink=document.querySelector('a[href="#destination"]')
,target=document.getElementById('destination')
anchorLink.addEventListener('click',(event) => {
if(window.scrollTo){ //
event.preventDefault();
window.scrollTo({'behavior':'smooth','top':target.offsetTop}) //
}
})
</script>
</body>
</html>
网友评论