[JavaScript] javascript代码控制页面滚动到网页顶端 →→→→→进入此内容的聊天室

来自 , 2019-04-08, 写在 JavaScript, 查看 133 次.
URL http://www.code666.cn/view/e564618b
  1. <script language="javascript">
  2. var goto_top_type = -1;
  3. var goto_top_itv = 0;
  4.  
  5. function goto_top_timer() {
  6.         var y = goto_top_type == 1 ? document.documentElement.scrollTop
  7.                         : document.body.scrollTop;
  8.         var moveby = 15;
  9.         y -= Math.ceil(y * moveby / 100);
  10.         if (y < 0) {
  11.                 y = 0;
  12.         }
  13.         if (goto_top_type == 1) {
  14.                 document.documentElement.scrollTop = y;
  15.         } else {
  16.                 document.body.scrollTop = y;
  17.         }
  18.         if (y == 0) {
  19.                 clearInterval(goto_top_itv);
  20.                 goto_top_itv = 0;
  21.         }
  22. }
  23.  
  24. function goto_top() {
  25.         if (goto_top_itv == 0) {
  26.                 if (document.documentElement && document.documentElement.scrollTop) {
  27.                         goto_top_type = 1;
  28.                 } else if (document.body && document.body.scrollTop) {
  29.                         goto_top_type = 2;
  30.                 } else {
  31.                         goto_top_type = 0;
  32.                 }
  33.                 if (goto_top_type > 0) {
  34.                         goto_top_itv = setInterval('goto_top_timer()', 50);
  35.                 }
  36.         }
  37. }
  38. </script>
  39.  
  40. <a href="javascript:void(0)" onclick="goto_top()">【返回顶部】</a>
  41. //javascript/5933

回复 "javascript代码控制页面滚动到网页顶端"

这儿你可以回复上面这条便签

captcha