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