每次看淘寶,看微信,都回有回到頂部的小logo,小圖標,或者雙擊返回頂部。所以就學習了如何返回頂部的操作,一開始是聯想html中的鏈接描點,在開頭出設置個標簽,下面點擊另外一個標志回去。有三種覺得比較適合使用,就保留下來。
第一種 添加一個超鏈接,然后給其href="#top",則可以使用
<a href="#top" class="btn" title="回到頂端"></a>
第二種 href指向特定的id
<body id="goTop"> <div class="box"> <img src="1.jpg"> <img src="1.jpg"> <img src="1.jpg"> </div> <a href="#goTop" class="btn" title="回到頂端"></a> </body>
第三種 用scrollTo(0,0)中的x、y的值來跳轉到頁面的具體位置。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"/> 5 <title>返回頂端</title> 6 <link rel="stylesheet" type="text/css" href="hello.css"> 7 </head> 8 <body id="goTop"> 9 <div class="box"> 10 <img src="1.jpg"> 11 <img src="1.jpg"> 12 <img src="1.jpg"> 13 </div> 14 <a href="javascript:window.scrollTo(0,0)" class="btn" title="回到頂端"></a> 15 </body> 16 </html>

1 *{ 2 margin: 0; 3 padding: 0; 4 } 5 .box{ 6 width: 1000px; 7 margin: 0 auto; 8 9 } 10 .btn{ 11 width: 40px; 12 height: 40px; 13 position: fixed; 14 bottom: 30px; 15 background-color: #aaa; 16 border:1px solid #333; 17 border-radius: 50%; 18 left: 50%; 19 margin-left:500px; 20 21 } 22 .btn:after{ 23 content: ""; 24 width:16px; 25 height:16px; 26 border-left:2px solid #333; 27 border-bottom: 2px solid #333; 28 position: absolute; 29 left: 12px; 30 top: 15px; 31 transform :rotate(135deg); 32 33 } 34 .btn:hover{ 35 background-color: #fff; 36 }
返回頂部的圖標要一直處於底部,position:fixed這個位置樣式