美文网首页
js回到顶部

js回到顶部

作者: jh2k15 | 来源:发表于2018-05-19 17:05 被阅读0次

<!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>

相关文章

网友评论

      本文标题:js回到顶部

      本文链接:https://www.haomeiwen.com/subject/bskadftx.html