本文先詳細介紹回到頂部的5種寫法,而後對其實現功能增長,最後獲得最終實現javascript
【1】錨點html
使用錨點連接是一種簡單的返回頂部的功能實現。該實現主要在頁面頂部放置一個指定名稱的錨點連接,而後在頁面下方放置一個返回到該錨點的連接,用戶點擊該連接便可返回到該錨點所在的頂部位置java
[注意]關於錨點的詳細信息移步至此瀏覽器
<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,且表示元素上方不可見內容的像素寬度動畫
因爲scrollTop是可寫的,能夠利用scrollTop來實現回到頂部的功能spa
[注意]關於頁面的scrollTop的兼容問題詳細內容移步至此code
<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()orm
scrollTo(x,y)方法滾動當前window中顯示的文檔,讓文檔中由座標x和y指定的點位於顯示區域的左上角htm
設置scrollTo(0,0)能夠實現回到頂部的效果
<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】scrollIntoView()
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>
下面對回到頂部的功能進行加強
【1】顯示加強
使用CSS畫圖,將「回到頂部」變成可視化的圖形(若是兼容IE8-瀏覽器,則用圖片代替)
使用CSS僞元素及僞類hover效果,當鼠標移動到該元素上時,顯示回到頂部的文字,移出時不顯示
<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>
【2】動畫加強
爲回到頂部增長動畫效果,滾動條以必定的速度回滾到頂部
動畫有兩種:一種是CSS動畫,須要有樣式變化配合transition;一種是javascript動畫,使用定時器來實現
在上面的5種實現中,scrollTop、scrollTo()和scrollBy()方法能夠增長動畫,且因爲無樣式變化,只能增長javascript動畫
定時器又有setInterval、setTimeout和requestAnimationFrame這三種可使用,下面使用性能最好的定時器requestAnimationFrame來實現
[注意]IE9-瀏覽器不支持該方法,可使用setTimeout來兼容
一、增長scrollTop的動畫效果
使用定時器,將scrollTop的值每次減小50,直到減小到0,則動畫完畢
<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>
二、增長scrollTo()動畫效果
將scrollTo(x,y)中的y參數經過scrollTop值獲取,每次減小50,直到減小到0,則動畫完畢
<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>
三、增長scrollBy()動畫效果
將scrollBy(x,y)中的y參數設置爲-50,直到scrollTop爲0,則回滾中止
<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>
因爲scrollTop、scrollBy()和scrollTo()方法,都以scrollTop值是否減小爲0做爲動畫中止的參照,且三個動畫的原理和實現都基本類似,性能也類似。最終,以最經常使用的scrollTop屬性實現動畫加強效果
固然,若是以爲50的速度不合適,能夠根據實際狀況進行調整
<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>
歡迎交流