<!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>
div,p{
margin: 0;
padding: 0;
}
#content {
background-color: orange;
height: 4000px;
width: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
}
#backTop {
position: fixed;
top: 20px;
}
#backBottom {
position: fixed;
bottom: 20px;
}
.arrow {
width: 100px;
padding: 10px;
right: 20px;
background-color: antiquewhite;
border-radius: 5px;
text-align: center;
display: none;
}
.box{
background-color: aqua;
width: 100%;
line-height: 40;
}
</style>
</head>
<body>
<div id="content">
<p class="box">我是顶部</p>
<p class="box">我是底部</p>
</div>
<div id="backTop" class="arrow">回到顶部</div>
<div id="backBottom" class="arrow">到达底部</div>
<script>
window.onload = function () {
let content = document.getElementById("content");
let backBottom = document.getElementById("backBottom");
let backTop = document.getElementById("backTop");
let currentScrollTop = document.documentElement.scrollTop;
window.onscroll = function (e) {
if (document.documentElement.scrollTop > currentScrollTop) {
backTop.style.display = "none";
backBottom.style.display = "block";
currentScrollTop = document.documentElement.scrollTop;
} else {
backTop.style.display = "block";
backBottom.style.display = "none";
currentScrollTop = document.documentElement.scrollTop;
}
}
backTop.onclick = function(){
document.documentElement.scrollTop = 0;
}
backBottom.onclick = function(){
document.documentElement.scrollTop = content.clientHeight;
}
}
</script>
</body>
</html>