1. 程式人生 > >點擊按鈕,回到頁面頂部的5種寫法

點擊按鈕,回到頁面頂部的5種寫法

flow pos set nim orm sta 常用 畫的 pan

1.錨點方式:

1 <body style="height:2000px;">
2 <div id="topAnchor"></div>
3 <a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到頂部</a>
4 </body>

2.scrollTop:scrollTop屬性表示被隱藏在內容區域上方的像素數。元素未滾動時,scrollTop的值為0,如果元素被垂直滾動了,scrollTop的值大於0,且表示元素上方不可見內容的像素寬度

由於scrollTop是可寫的,可以利用scrollTop來實現回到頂部的功能
1
<body style="height:2000px;"> 2 <button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button> 3 <script> 4 test.onclick = function(){ 5 document.body.scrollTop = document.documentElement.scrollTop = 0; 6 } 7 </script> 8 </body>

3.scrollTo:scrollTo(x,y)方法滾動當前window中顯示的文檔,讓文檔中由坐標x和y指定的點位於顯示區域的左上角,設置scrollTo(0,0)可以實現回到頂部的效果

1 <body style="height:2000px;">
2 <button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button>
3 <script>
4 test.onclick = function(){
5 scrollTo(0,0);
6 }
7 </script>
8 </body>

4.scrollBy():scrollBy(x,y)方法滾動當前window中顯示的文檔,x和y指定滾動的相對量,只要把當前頁面的滾動長度作為參數,逆向滾動,則可以實現回到頂部的效果

1 <body style="height:2000px;">
2 <button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button>
3 <script>
4 test.onclick = function(){
5 var top = document.body.scrollTop || document.documentElement.scrollTop
6 scrollBy(0,-top);
7 }
8 </script>
9 </body>

5.scrolltoView()Element.scrollIntoView方法滾動當前元素,進入瀏覽器的可見區域,該方法可以接受一個布爾值作為參數。如果為true,表示元素的頂部與當前區域的可見部分的頂部對齊(前提是當前區域可滾動);如果為false,表示元素的底部與當前區域的可見部分的尾部對齊(前提是當前區域可滾動)。如果沒有提供該參數,默認為true,使用該方法的原理與使用錨點的原理類似,在頁面最上方設置目標元素,當頁面滾動時,目標元素被滾動到頁面區域以外,點擊回到頂部按鈕,使目標元素重新回到原來位置,則達到預期效果

1 <body style="height:2000px;">
2 <div id="target"></div>
3 <button id="test" style="position:fixed;right:0;bottom:0">回到頂部</button>
4 <script>
5 test.onclick = function(){
6 target.scrollIntoView();
7 }
8 </script>
9 </body>
增強 下面對回到頂部的功能進行增強 【1】顯示增強 使用CSS畫圖,將“回到頂部”變成可視化的圖形(如果兼容IE8-瀏覽器,則用圖片代替) 使用CSS偽元素及偽類hover效果,當鼠標移動到該元素上時,顯示回到頂部的文字,移出時不顯示  
  1. <style>
  2. .box{
  3. position:fixed;
  4. right:10px;
  5. bottom: 10px;
  6. height:30px;
  7. width: 50px;
  8. text-align:center;
  9. padding-top:20px;
  10. background-color: lightblue;
  11. border-radius: 20%;
  12. overflow: hidden;
  13. }
  14. .box:hover:before{
  15. top:50%
  16. }
  17. .box:hover .box-in{
  18. visibility: hidden;
  19. }
  20. .box:before{
  21. position: absolute;
  22. top: -50%;
  23. left: 50%;
  24. transform: translate(-50%,-50%);
  25. content:‘回到頂部‘;
  26. width: 40px;
  27. color:peru;
  28. font-weight:bold;
  29. }
  30. .box-in{
  31. visibility: visible;
  32. display:inline-block;
  33. height:20px;
  34. width: 20px;
  35. border: 3px solid black;
  36. border-color: white transparent transparent white;
  37. transform:rotate(45deg);
  38. }
  39. </style>
  40. <body style="height:2000px;">
  41. <div id="box" class="box">
  42. <div class="box-in"></div>
  43. </div>
  44. </body>
【2】動畫增強 為回到頂部增加動畫效果,滾動條以一定的速度回滾到頂部 動畫有兩種:一種是CSS動畫,需要有樣式變化配合transition;一種是javascript動畫,使用定時器來實現   在上面的5種實現中,scrollTop、scrollTo()和scrollBy()方法可以增加動畫,且由於無樣式變化,只能增加javascript動畫 定時器又有setInterval、setTimeout和requestAnimationFrame這三種可以使用,下面使用性能最好的定時器requestAnimationFrame來實現 [註意]IE9-瀏覽器不支持該方法,可以使用setTimeout來兼容 1、增加scrollTop的動畫效果 使用定時器,將scrollTop的值每次減少50,直到減少到0,則動畫完畢
  1. <script>
  2. var timer = null;
  3. box.onclick = function(){
  4. cancelAnimationFrame(timer);
  5. timer = requestAnimationFrame(function fn(){
  6. var oTop = document.body.scrollTop || document.documentElement.scrollTop;
  7. if(oTop > 0){
  8. document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
  9. timer = requestAnimationFrame(fn);
  10. }else{
  11. cancelAnimationFrame(timer);
  12. }
  13. });
  14. }
  15. </script>
2、增加scrollTo()動畫效果 將scrollTo(x,y)中的y參數通過scrollTop值獲取,每次減少50,直到減少到0,則動畫完畢
  1. <script>
  2. var timer = null;
  3. box.onclick = function(){
  4. cancelAnimationFrame(timer);
  5. timer = requestAnimationFrame(function fn(){
  6. var oTop = document.body.scrollTop || document.documentElement.scrollTop;
  7. if(oTop > 0){
  8. scrollTo(0,oTop-50);
  9. timer = requestAnimationFrame(fn);
  10. }else{
  11. cancelAnimationFrame(timer);
  12. }
  13. });
  14. }
  15. </script>
3、增加scrollBy()動畫效果 將scrollBy(x,y)中的y參數設置為-50,直到scrollTop為0,則回滾停止
  1. <script>
  2. var timer = null;
  3. box.onclick = function(){
  4. cancelAnimationFrame(timer);
  5. timer = requestAnimationFrame(function fn(){
  6. var oTop = document.body.scrollTop || document.documentElement.scrollTop;
  7. if(oTop > 0){
  8. scrollBy(0,-50);
  9. timer = requestAnimationFrame(fn);
  10. }else{
  11. cancelAnimationFrame(timer);
  12. }
  13. });
  14. }
  15. </script>
實現 由於scrollTop、scrollBy()和scrollTo()方法,都以scrollTop值是否減少為0作為動畫停止的參照,且三個動畫的原理和實現都基本相似,性能也相似。最終,以最常用的scrollTop屬性實現動畫增強效果 當然,如果覺得50的速度不合適,可以根據實際情況進行調整
  1. <style>
  2. .box{
  3. position:fixed;
  4. right:10px;
  5. bottom: 10px;
  6. height:30px;
  7. width: 50px;
  8. text-align:center;
  9. padding-top:20px;
  10. background-color: lightblue;
  11. border-radius: 20%;
  12. overflow: hidden;
  13. }
  14. .box:hover:before{
  15. top:50%
  16. }
  17. .box:hover .box-in{
  18. visibility: hidden;
  19. }
  20. .box:before{
  21. position: absolute;
  22. top: -50%;
  23. left: 50%;
  24. transform: translate(-50%,-50%);
  25. content:‘回到頂部‘;
  26. width: 40px;
  27. color:peru;
  28. font-weight:bold;
  29. }
  30. .box-in{
  31. visibility: visible;
  32. display:inline-block;
  33. height:20px;
  34. width: 20px;
  35. border: 3px solid black;
  36. border-color: white transparent transparent white;
  37. transform:rotate(45deg);
  38. }
  39. </style>
  40. <body style="height:2000px;">
  41. <div id="box" class="box">
  42. <div class="box-in"></div>
  43. </div>
  44. </body>
  45. <script>
  46. var timer = null;
  47. box.onclick = function(){
  48. cancelAnimationFrame(timer);
  49. timer = requestAnimationFrame(function fn(){
  50. var oTop = document.body.scrollTop || document.documentElement.scrollTop;
  51. if(oTop > 0){
  52. document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
  53. timer = requestAnimationFrame(fn);
  54. }else{
  55. cancelAnimationFrame(timer);
  56. }
  57. });
  58. }
  59. </script>

點擊按鈕,回到頁面頂部的5種寫法