JS 实现返回顶部效果
代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>返回顶部</title>
<style>
body{
height: 3000px;
}
.toTop{
width: 50px;
height: 50px;
border-radius: 50%;
background: radial-gradient(#fff, #fe604a 80%,#fff);
position: fixed;
bottom: 30px;
right: 30px;
color: #fe604a;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<div class="toTop">Top</div>
</body>
<script>
let toTop=document.querySelector(".toTop");
toTop.onclick=function () {
document.documentElement.scrollTop=0;
}
//documentElement.scrollTop,文档超出浏览器窗口Top的位置(距离)值
</script>
</html>