基于JS實(shí)現(xiàn)回到頁(yè)面頂部的五種寫(xiě)法(從實(shí)現(xiàn)到增強(qiáng))
寫(xiě)法
【1】錨點(diǎn)
使用錨點(diǎn)鏈接是一種簡(jiǎn)單的返回頂部的功能實(shí)現(xiàn)。該實(shí)現(xiàn)主要在頁(yè)面頂部放置一個(gè)指定名稱的錨點(diǎn)鏈接,然后在頁(yè)面下方放置一個(gè)返回到該錨點(diǎn)的鏈接,用戶點(diǎn)擊該鏈接即可返回到該錨點(diǎn)所在的頂部位置
[注意]關(guān)于錨點(diǎn)的詳細(xì)信息移步至此
<body style="height:2000px;"> <div id="topAnchor"></div> <a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到頂部</a> </body>
【2】scrollTop
scrollTop屬性表示被隱藏在內(nèi)容區(qū)域上方的像素?cái)?shù)。元素未滾動(dòng)時(shí),scrollTop的值為0,如果元素被垂直滾動(dòng)了,scrollTop的值大于0,且表示元素上方不可見(jiàn)內(nèi)容的像素寬度
由于scrollTop是可寫(xiě)的,可以利用scrollTop來(lái)實(shí)現(xiàn)回到頂部的功能
[注意]關(guān)于頁(yè)面的scrollTop的兼容問(wèn)題詳細(xì)內(nèi)容移步至此
<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)方法滾動(dòng)當(dāng)前window中顯示的文檔,讓文檔中由坐標(biāo)x和y指定的點(diǎn)位于顯示區(qū)域的左上角
設(shè)置scrollTo(0,0)可以實(shí)現(xiàn)回到頂部的效果
<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)方法滾動(dòng)當(dāng)前window中顯示的文檔,x和y指定滾動(dòng)的相對(duì)量
只要把當(dāng)前頁(yè)面的滾動(dòng)長(zhǎng)度作為參數(shù),逆向滾動(dòng),則可以實(shí)現(xiàn)回到頂部的效果
<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方法滾動(dòng)當(dāng)前元素,進(jìn)入瀏覽器的可見(jiàn)區(qū)域
該方法可以接受一個(gè)布爾值作為參數(shù)。如果為true,表示元素的頂部與當(dāng)前區(qū)域的可見(jiàn)部分的頂部對(duì)齊(前提是當(dāng)前區(qū)域可滾動(dòng));如果為false,表示元素的底部與當(dāng)前區(qū)域的可見(jiàn)部分的尾部對(duì)齊(前提是當(dāng)前區(qū)域可滾動(dòng))。如果沒(méi)有提供該參數(shù),默認(rèn)為true
使用該方法的原理與使用錨點(diǎn)的原理類(lèi)似,在頁(yè)面最上方設(shè)置目標(biāo)元素,當(dāng)頁(yè)面滾動(dòng)時(shí),目標(biāo)元素被滾動(dòng)到頁(yè)面區(qū)域以外,點(diǎn)擊回到頂部按鈕,使目標(biāo)元素重新回到原來(lái)位置,則達(dá)到預(yù)期效果
<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>
增強(qiáng)
下面對(duì)回到頂部的功能進(jìn)行增強(qiáng)
【1】顯示增強(qiáng)
使用CSS畫(huà)圖,將“回到頂部”變成可視化的圖形(如果兼容IE8-瀏覽器,則用圖片代替)
使用CSS偽元素及偽類(lèi)hover效果,當(dāng)鼠標(biāo)移動(dòng)到該元素上時(shí),顯示回到頂部的文字,移出時(shí)不顯示
<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】動(dòng)畫(huà)增強(qiáng)
為回到頂部增加動(dòng)畫(huà)效果,滾動(dòng)條以一定的速度回滾到頂部
動(dòng)畫(huà)有兩種:一種是CSS動(dòng)畫(huà),需要有樣式變化配合transition;一種是javascript動(dòng)畫(huà),使用定時(shí)器來(lái)實(shí)現(xiàn)
在上面的5種實(shí)現(xiàn)中,scrollTop、scrollTo()和scrollBy()方法可以增加動(dòng)畫(huà),且由于無(wú)樣式變化,只能增加javascript動(dòng)畫(huà)
定時(shí)器又有setInterval、setTimeout和requestAnimationFrame這三種可以使用,下面使用性能最好的定時(shí)器requestAnimationFrame來(lái)實(shí)現(xiàn)
[注意]IE9-瀏覽器不支持該方法,可以使用setTimeout來(lái)兼容
1、增加scrollTop的動(dòng)畫(huà)效果
使用定時(shí)器,將scrollTop的值每次減少50,直到減少到0,則動(dòng)畫(huà)完畢
<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>
2、增加scrollTo()動(dòng)畫(huà)效果
將scrollTo(x,y)中的y參數(shù)通過(guò)scrollTop值獲取,每次減少50,直到減少到0,則動(dòng)畫(huà)完畢
<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>
3、增加scrollBy()動(dòng)畫(huà)效果
將scrollBy(x,y)中的y參數(shù)設(shè)置為-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>
實(shí)現(xiàn)
由于scrollTop、scrollBy()和scrollTo()方法,都以scrollTop值是否減少為0作為動(dòng)畫(huà)停止的參照,且三個(gè)動(dòng)畫(huà)的原理和實(shí)現(xiàn)都基本相似,性能也相似。最終,以最常用的scrollTop屬性實(shí)現(xiàn)動(dòng)畫(huà)增強(qiáng)效果
當(dāng)然,如果覺(jué)得50的速度不合適,可以根據(jù)實(shí)際情況進(jìn)行調(diào)整
<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>
以上所述是小編給大家介紹的基于JS實(shí)現(xiàn)回到頁(yè)面頂部的五種寫(xiě)法(從實(shí)現(xiàn)到增強(qiáng))的全部敘述,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
uni-app實(shí)現(xiàn)數(shù)據(jù)上拉加載更多功能實(shí)例
數(shù)據(jù)列表在很多時(shí)候,經(jīng)常會(huì)用到,下面這篇文章主要給大家介紹了關(guān)于uni-app實(shí)現(xiàn)數(shù)據(jù)上拉加載更多功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08js 中將多個(gè)逗號(hào)替換為一個(gè)逗號(hào)的代碼
這篇文章主要介紹了js 中將多個(gè)逗號(hào)替換為一個(gè)逗號(hào)的代碼,需要的朋友可以參考下2014-06-06web開(kāi)發(fā)js字符串拼接占位符及conlose對(duì)象Api詳解
本篇文章主要為大家介紹了web開(kāi)發(fā)中字符串的拼接,占位符的使用以及conlose對(duì)象Api的使用,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09從零使用TypeScript開(kāi)發(fā)項(xiàng)目打包發(fā)布到npm
這篇文章主要介紹了從零使用TypeScript開(kāi)發(fā)項(xiàng)目打包發(fā)布到npm,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02JS實(shí)現(xiàn)輪播圖效果的3種簡(jiǎn)單方法
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)輪播圖效果的3種簡(jiǎn)單方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11javascript實(shí)現(xiàn)的一個(gè)帶下拉框功能的文本框
這篇文章主要介紹了javascript實(shí)現(xiàn)的一個(gè)帶下拉框功能的文本框,需要的朋友可以參考下2014-05-05