標籤:網頁 延遲 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——給網頁添加返回頂部按鈕