炫酷的返回頂部功能
js核心代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 window.onscroll = function(){ var t = document.documentElement.scrollTop || document.body.scrollTop; var top_div = document.getElementById( "go" ); if( t >= 300 ) { top_div.style.display = "block"; } else { top_div.style.display = "none"; } } var ptt=20; function pageScroll() { document.getElementById('flypig').style.display='block'; window.scrollBy(0,-10); //每次上升10px if(document.body.scrollTop==0) { document.getElementById('flypig').style.marginBottom=ptt+'px'; ptt=ptt+10; } scrolldelay = setTimeout('pageScroll()',2); //2毫秒重復執行一次 if(ptt>=1000){ ptt=0; document.getElementById('flypig').style.marginBottom=20+'px'; document.getElementById('flypig').style.display='none'; clearTimeout(scrolldelay); } //pageScroll ends }html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 <!DOCTYPE html> <html> <head> <title>返回頂部按鈕的實現</title> <meta charset="utf-8" /> <style> body{ margin:0px; } #flypig{ display: none; float: left; position: fixed; bottom: 20px; margin-left: 75%; cursor: pointer; margin-bottom: 20px; } #go{ display: block; float: left; position: fixed; bottom: 10px; margin-left: 75%; cursor: pointer; } </style> <script> window.onscroll = function(){ var t = document.documentElement.scrollTop || document.body.scrollTop; var top_div = document.getElementById( "go" ); if( t >= 300 ) { top_div.style.display = "block"; } else { top_div.style.display = "none"; } } var ptt=20; function pageScroll() { document.getElementById('flypig').style.display='block'; window.scrollBy(0,-10); //每次上升10px if(document.body.scrollTop==0) { document.getElementById('flypig').style.marginBottom=ptt+'px'; ptt=ptt+10; } scrolldelay = setTimeout('pageScroll()',2); //2毫秒重復執行一次 if(ptt>=1000){ ptt=0; document.getElementById('flypig').style.marginBottom=20+'px'; document.getElementById('flypig').style.display='none'; clearTimeout(scrolldelay); } //pageScroll ends } </script> </head> <body> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <h1 style="text-align:center;">請往下滑</h1> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /><br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /><!--讓你的頁面足夠的長--> <div style="display: none; float: left; position: fixed; margin-left: 50%; cursor: pointer; margin-bottom: 20px;" id="flypig"> <img src="/wp-content/themes/Jakesoft/images/flypig.gif"> </div> <div style="display: none; float: left; position: fixed; bottom: 10px; margin-left: 50%; cursor: pointer;" id="go" onclick="pageScroll()">返回頂部</div></html>以上所述就是本文的全部內容了,希望大家能夠喜歡。