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