這篇文章將為大家詳細(xì)講解有關(guān)js怎么實現(xiàn)返回頂部緩沖效果,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)憑借專業(yè)的設(shè)計團(tuán)隊扎實的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務(wù),在成都十年的網(wǎng)站建設(shè)設(shè)計經(jīng)驗,為成都近千家中小型企業(yè)策劃設(shè)計了網(wǎng)站。
運行原理
通過定時器30毫秒執(zhí)行一次滾動條上升,每次上升的高度為當(dāng)前高度的80%,這樣就達(dá)到了上升緩沖的動畫效果。
判斷當(dāng)滾動條高度超過一屏?xí)r,按鈕顯示,默認(rèn)隱藏
知識要點
scrollTop//獲取滾動條高度 需要寫兼容 clientHeight//可視窗口高度 需要寫兼容 setInterval//定時器 window.onscroll//滾動觸發(fā)事件
完整代碼
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>demo</title> <style> body,h2,h3,h4,h5,h6,h7,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;} h2,h3,h4,h5,h6,h7{font-size:100%;} address,cite,dfn,em,var{font-style:normal;} code,kbd,pre,samp{font-family:courier new,courier,monospace;} ul,ol{list-style:none;} a{text-decoration:none;} a:hover{text-decoration:none;} sup{vertical-align:text-top;} sub{vertical-align:text-bottom;} legend{color:#000;} fieldset,img{border:0;} button,input,select,textarea{font-size:100%;} table{border-collapse:collapse;border-spacing:0;} .clear{clear: both;float: none;height: 0;overflow: hidden;} .bg{background:#9B1BC5; width: 1000px; height: 3000px; margin: 0 auto;} #gotop{width: 50px; height: 50px; background:#5490F5; color: #fff; position: fixed; left: 50%; bottom: 30px; text-align: center; font-family: "Microsoft Yahei",tahoma,arial; font-size: 14px; cursor: pointer; margin-left: 520px; display: none;} #gotop span{display: block;padding: 5px;} </style> </head> <body> <div class="bg"></div> <div id="gotop"><span>返回頂部</span></div> <script type="text/javascript"> //在頁面加載完后立即執(zhí)行多個函數(shù)方案 function addloadEvent(func){ var oldonload=window.onload; if(typeof window.onload !="function"){ window.onload=func; } else{ window.onload=function(){ if(oldonload){ oldonload(); } func(); } } } //在頁面加載完后立即執(zhí)行多個函數(shù)方案結(jié)束 addloadEvent(b); function b(){ var gotop=document.getElementById("gotop"); var timer; var tf=true; //滾動觸發(fā) window.onscroll=function(){ //獲取滾動條高度 var ostop=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; //獲取窗口可視區(qū)域高度 //console.log(ostop) var ch=document.documentElement.clientHeight||document.body.clientHeight; //如果頁面超過一屏高度按鈕顯示,否則隱藏 if(ostop>=ch){ gotop.style.display="block"; }else{ gotop.style.display="none"; } // if(!tf){ clearInterval(timer); } tf=false; } //點擊觸發(fā) gotop.onclick=function(){ //創(chuàng)建定時器 timer=setInterval(function(){ //獲取滾動條高度 var ostop=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; //每次上升高度的20% var speed=Math.ceil(ostop/5); //每次上升當(dāng)前高度的80% document.documentElement.scrollTop=document.body.scrollTop=ostop-speed; //如果高度為0,清除定時器 if(ostop==0){ clearInterval(timer); } tf=true; },30); } } </script> </body> </html>
關(guān)于“js怎么實現(xiàn)返回頂部緩沖效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
本文名稱:js怎么實現(xiàn)返回頂部緩沖效果
本文地址:http://chinadenli.net/article48/jgggep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、服務(wù)器托管、網(wǎng)站設(shè)計、App設(shè)計、網(wǎng)頁設(shè)計公司、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)