javascript回到頂部特效_javascript技巧

來源:互聯網
上載者:User

本文執行個體為大家分享了javascript回到頂部效果,供大家參考,具體內容如下

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> <title>回到頂部效果(原生js)</title> <style type="text/css">  body{   margin: 0;  }  .bg{   width: 1022px;   margin: 0 auto;  }  .btn{   display: none;   width: 75px;   height: 75px;   background:url(../images/網頁top小表徵圖(返回頂部)/返回頂部-085.png) no-repeat left top;   position: fixed;   left: 50%;   margin-left: 530px;   bottom: 10px;   text-indent: -9999px;   outline: none;  }  .btn:hover{   background-position: 0 -75px;  } </style>  <script>   var timer=null; var isScroll=true; //1.2構造oScroll函數 function oScroll(){  var osTop=document.documentElement.scrollTop||document.body.scrollTop;//1.1滾動高度,相容  var oSpeed=Math.ceil(osTop/4);//捲動速度  document.documentElement.scrollTop=document.body.scrollTop=osTop-oSpeed;  if(osTop==0){//判斷到達頂部,清理定時器   clearInterval(timer);  }  isScroll=true;  }       window.onload=function() {  var obtn=document.getElementById('btn');//擷取按鈕元素  var clientHeight=document.documentElement.clientHeight||document.body.clientHeight;  // 1.點擊返回按鈕事件   btn.onclick=function(){//為按鈕綁定點擊事件   timer=setInterval(oScroll,50);   }   window.onscroll=function() {   var osTop=document.documentElement.scrollTop||document.body.scrollTop;//1.1滾動高度,相容   if (osTop>clientHeight) {    btn.style.display="block";   }else{    btn.style.display="none";   }   if(!isScroll){    clearInterval(timer);   }   isScroll=false;  } }  </script></head><body> <div class="bg">  <img src="../images/jz.jpg"> </div> <a href="javascript:void(0);" title="返回頂部" id="btn" class="btn">返回頂部</a></body></html>

以上就是本文的全部內容,希望對大家學習JavaScript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.