JS——給網頁添加返回頂部按鈕

來源:互聯網
上載者:User

標籤:網頁   延遲   src   hidden   function   logs   表徵圖   fadeout   lan   

在頁面右下方添加一個返回頂部按鈕,當頁面滑到一定位置時,按鈕出現,否則消失,預設隱藏

 

代碼如下:

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>返回頂部</title> 6     <style type="text/css"> 7     .content{ width: 100%;height: 500px; background:green; overflow: hidden; } 8     #back-to-top{ position: fixed; right: 50px;bottom: 10%;  width: 44px;height: 44px; text-align: center; display: none; cursor: pointer;} 9     #back-to-top a:hover{ color: #fff; }10 </style>11 <script src="js/jquery-1.7.2.min.js"></script>12 </head>13 <body>14 <div class="content"></div>15 <div class="content"></div>16 <div class="content"></div>17 <div id="back-to-top"><a href="#" title="返回頂部"><img src="images/top.png" width="44" height="44"></a></div>18 <script>19 $(function(){20     $(‘#back-to-top‘).hide();21     $(function(){22         $(window).scroll(function(){23             if($(window).scrollTop()>100){24                 $(‘#back-to-top‘).fadeIn(100);25             }else{26                 $(‘#back-to-top‘).fadeOut(100);27             }28         });29         $(‘#back-to-top‘).click(function(){30             $(‘body,html‘).animate({scrollTop:0},300)31             return false;32         })33     })34 })35 </script>36 </body>37 </html>

.fadeIn() 設定表徵圖淡出延遲,  .fadeOut()設定表徵圖淡入(隱藏)延遲。這裡要注意兩者不要弄反了,初學者往往容易因為out和in的字面意思,弄反該方法的意思。

在給該表徵圖添加一個click事件。

JS——給網頁添加返回頂部按鈕

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.