1.锚点方式javascript
<body style="height:2000px;">
<div id="topAnchor"></div>
<a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到顶部</a>
</body>
2.scrollTop:scrollTop属性表示被隐藏在内容区域上方的像素数。元素未滚动时,scrollTop的值为0,若是元素被垂直滚动了,scrollTop的值大于0,且表示元素上方不可见内容的像素宽度html
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script> test.onclick = function(){ document.body.scrollTop = document.documentElement.scrollTop = 0; } </script>
</body>
3.scrollTo:scrollTo(x,y)方法滚动当前window中显示的文档,让文档中由坐标x和y指定的点位于显示区域的左上角,设置scrollTo(0,0)能够实现回到顶部的效果java
<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script> test.onclick = function(){ scrollTo(0,0); } </script>
</body>
4.scrollBy():scrollBy(x,y)方法滚动当前window中显示的文档,x和y指定滚动的相对量,只要把当前页面的滚动长度做为参数,逆向滚动,则能够实现回到顶部的效果浏览器
<body style="height:2000px;"> <button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script> test.onclick = function(){ var top = document.body.scrollTop || document.documentElement.scrollTop scrollBy(0,-top); } </script> </body>
5.scrolltoView()Element.scrollIntoView方法滚动当前元素,进入浏览器的可见区域,该方法能够接受一个布尔值做为参数。若是为true,表示元素的顶部与当前区域的可见部分的顶部对齐(前提是当前区域可滚动);若是为false,表示元素的底部与当前区域的可见部分的尾部对齐(前提是当前区域可滚动)。若是没有提供该参数,默认为true,使用该方法的原理与使用锚点的原理相似,在页面最上方设置目标元素,当页面滚动时,目标元素被滚动到页面区域之外,点击回到顶部按钮,使目标元素从新回到原来位置,则达到预期效果性能
<body style="height:2000px;">
<div id="target"></div>
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script> test.onclick = function(){ target.scrollIntoView(); } </script>
</body>
<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>
<body style="height:2000px;">
<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>
<script>
var timer = null; box.onclick = function(){ cancelAnimationFrame(timer); timer = requestAnimationFrame(function fn(){ var oTop = document.body.scrollTop || document.documentElement.scrollTop; if(oTop > 0){ scrollTo(0,oTop-50); timer = requestAnimationFrame(fn); }else{ cancelAnimationFrame(timer); } }); } </script>
<script>
var timer = null; box.onclick = function(){ cancelAnimationFrame(timer); timer = requestAnimationFrame(function fn(){ var oTop = document.body.scrollTop || document.documentElement.scrollTop; if(oTop > 0){ scrollBy(0,-50); timer = requestAnimationFrame(fn); }else{ cancelAnimationFrame(timer); } }); } </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>
<body style="height:2000px;">
<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>
嗯。就酱~~动画
参考:https://www.cnblogs.com/yangguoe/p/9838147.htmlspa