带有动画返回顶部
Posted 风尘救我
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有动画返回顶部相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.w {
width: 1200px;
margin: 0 auto;
}
.header {
height: 100px;
background-color: salmon;
}
.main {
margin-top: 20px;
height: 1400px;
background-color: seagreen;
}
.footer {
margin-top: 20px;
height: 200px;
background-color: darkkhaki;
}
.aside {
width: 50px;
height: 200px;
background-color: rgb(29, 22, 19);
position: absolute;
left: 50%;
top: 300px;
margin-left: 600px;
}
</style>
</head>
<body style="height: 2000px;">
<div class="aside"></div>
<div class="header w"></div>
<div class="main w"></div>
<div class="footer w"></div>
</body>
<script>
var header = document.querySelector(".header");
var aside = document.querySelector(".aside");
var h = header.offsetHeight
var tt = aside.offsetTop;
var lossDistance = aside.offsetTop - h;
document.addEventListener("scroll", function () {
var pageY = window.pageYOffset;
if (window.pageYOffset >= h) {
aside.style.position = "fixed"
aside.style.top = lossDistance + "px"
} else {
aside.style.position = "absolute"
aside.style.top = tt + "px"
}
})
aside.addEventListener("click", function () {
var timer = setInterval(function () {
var pageY = window.pageYOffset;
var current = Math.ceil(pageY / 10)
window.scrollTo(0, pageY - current);
if (current == 0) {
clearInterval(timer)
}
}, 15)
})
</script>
</html>
以上是关于带有动画返回顶部的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript-Tool-lhgDialog:动画示例