<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<style>
.box {
position: fixed;
right: 10px;
bottom: 10px;
height: 30px;
width: 50px;
text-align: center;
padding-top: 20px;
background-color: lightblue;
border-radius: 20%;
overflow: hidden;
}
.box:hover:before {
top: 50%
}
.box:hover .box-in {
visibility: hidden;
}
.box:before {
position: absolute;
top: -50%;
left: 50%;
transform: translate(-50%, -50%);
content: '回到顶部';
width: 40px;
color: peru;
font-weight: bold;
}
.box-in {
visibility: visible;
display: inline-block;
height: 20px;
width: 20px;
border: 3px solid black;
border-color: white transparent transparent white;
transform: rotate(45deg);
}
</style>
</head>
<body style="height:2000px;">
<div>我是顶部</div>
<div id="box" class="box">
<div class="box-in"></div>
</div>
</body>
<script>
var timer = null;
box.onclick = function () {
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn() {
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
if (oTop > 0) {
document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
timer = requestAnimationFrame(fn);
} else {
cancelAnimationFrame(timer);
}
});
}
</script>
</html>