Add button for Smooth Scroll to the top / bottom

为页面添加按钮,平滑的滚动到顶部/底部

目前為 2015-05-24 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Add button for Smooth Scroll to the top / bottom
  3. // @author burningall
  4. // @description 为页面添加按钮,平滑的滚动到顶部/底部
  5. // @version 2015.5.25.2
  6. // @include *
  7. // @supportURL http://www.burningall.com
  8. // @contributionURL troy450409405@gmail.com|alipay.com
  9. // @namespace https://gf.qytechs.cn/zh-CN/users/3400-axetroy
  10. // ==/UserScript==
  11.  
  12. (function(){
  13. //================公共函数区============
  14. function addEvent(obj, event, fn) {return obj.addEventListener ? obj.addEventListener(event, fn, false) : obj.attachEventListener("on" + event, fn);};
  15. function getScrollHeight() {return document.body ? bodyScrollHeight = document.body.scrollHeight: document.documentElement.scrollHeight;};
  16. function getClientHeight() {return document.documentElement ? document.documentElement.clientHeight: document.body.clientHeight;};
  17. function hasScroll() {return getScrollHeight() > getClientHeight() ? true : false;};
  18. function $(id) {return document.getElementById(id);}
  19. function getStyle(obj, attr) {return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];}
  20. function getScrollTop() {return document.documentElement.scrollTop ? document.documentElement.scrollTop: document.body.scrollTop;}
  21. function getClientHeight() {return document.documentElement ? document.documentElement.clientHeight: document.body.clientHeight;}
  22. function createElement(tagName, idName, styleList, appendPosition, endFn) {
  23. var newElement = document.createElement(tagName);
  24. newElement.id = idName;
  25. newElement.style.cssText = styleList;
  26. appendPosition && appendPosition.appendChild(newElement);
  27. endFn && endFn();
  28. }
  29. function doMove(obj, attr, dir, target, endFn) {
  30. dir = parseInt(getStyle(obj, attr)) < target ? dir: -dir;
  31. clearInterval(obj.timer);
  32. obj.timer = setInterval(function() {
  33. var speed = parseInt(getStyle(obj, attr)) + dir;
  34. if (speed > target && dir > 0 || speed < target && dir < 0) {
  35. speed = target;
  36. };
  37. obj.style[attr] = speed + "px";
  38. if (speed == target) {
  39. clearInterval(obj.timer);
  40. endFn && endFn();
  41. };
  42. },
  43. 30);
  44. };
  45. function movein(){
  46. clearTimeout($("scrollMars-troy").timerHover);
  47. doMove($("scrollMars-troy"), "right", 10, 20,function() {
  48. doMove($("scrollMars-troy"), "width", 20, 100);
  49. });
  50. };
  51. function moveout(){
  52. clearTimeout($("scrollMars-troy").timerHover);
  53. $("scrollMars-troy").timerHover = setTimeout(function() {
  54. doMove($("scrollMars-troy"), "right", 10, -80,function() {
  55. doMove($("scrollMars-troy"), "width", 20, 160);
  56. });
  57. },
  58. 3000);//鼠标离开后,3s隐藏到边栏
  59. };
  60. function scrollTop(){
  61. clearInterval($("goTop-troy").timerScroll);
  62. $("goTop-troy").timerScroll = setInterval(function() {
  63. var speed = (getScrollTop() / 5) + 10;
  64. position = getScrollTop() - speed;
  65. if (position <= 0) {
  66. document.body.scrollTop = document.documentElement.scrollTop = 0;
  67. clearInterval($("goTop-troy").timerScroll);
  68. $("goTop-troy").timerScroll = null;
  69. }
  70. document.body.scrollTop = document.documentElement.scrollTop = position;
  71. },
  72. 10);
  73. };
  74. function scrollBtn(){
  75. clearInterval($("goTop-troy").timerScroll);
  76. $("goTop-troy").timerScroll = setInterval(function() {
  77. var speed = (getScrollTop() / 5) + 10;
  78. position = getScrollTop() + speed;
  79. if (position + getClientHeight() >= getScrollHeight()) {
  80. document.body.scrollTop = document.documentElement.scrollTop = getScrollHeight();
  81. clearInterval($("goTop-troy").timerScroll);
  82. $("goTop-troy").timerScroll = null;
  83. }
  84. document.body.scrollTop = document.documentElement.scrollTop = position;
  85. },
  86. 10);
  87. };
  88. //================主要代码区============
  89. function createBtn(){
  90. if($("scrollMars-troy") && hasScroll() == true){//如果有滚动条,并且存在滚动按钮
  91. $('scrollMars-troy').style.top=(getClientHeight()/3)+'px';//调整按钮位置
  92. }else if($("scrollMars-troy") && hasScroll() == false){//如果没有滚动条,但是有按钮
  93. $("scrollMars-troy").remove($("scrollMars-troy"));//删除按钮
  94. };
  95. if (hasScroll() == false && !$("scrollMars-troy")) {//如果没有滚动条,并且没有按钮
  96. return false;
  97. }else if(hasScroll() == true && !$("scrollMars-troy")){//如果有滚动条,并且没有按钮
  98. createElement("div", "scrollMars-troy",MarsCSS, document.documentElement);
  99. $("scrollMars-troy").innerHTML = "<div id='goTop-troy' class='Top-and-Btn'></div><div id='goBtn-troy' class='Top-and-Btn'></div>";
  100. $('scrollMars-troy').style.top=(getClientHeight()/3)+'px';
  101. $("goTop-troy").style.cssText = BottonCSS;
  102. $("goBtn-troy").style.cssText = BottonCSS;
  103. $("goTop-troy").innerHTML = "顶<br/>部";
  104. $("goBtn-troy").innerHTML = "底<br/>部";
  105. addEvent($("goTop-troy"), "click",function() {scrollTop()});
  106. addEvent($("goBtn-troy"), "click",function() {scrollBtn()});
  107. addEvent(window, "resize",function() {$('scrollMars-troy').style.top=(getClientHeight()/3)+'px';});
  108. addEvent($("scrollMars-troy"), "mouseover",function() {movein()});
  109. addEvent($("scrollMars-troy"), "mouseout",function() {moveout()});
  110. addEvent($("goTop-troy"), "mouseover",function() {$("goTop-troy").style.background = "#FF0004";});
  111. addEvent($("goTop-troy"), "mouseout",function() {$("goTop-troy").style.background = "#303030";});
  112. addEvent($("goBtn-troy"), "mouseover",function() {$("goBtn-troy").style.background = "#FF0004";});
  113. addEvent($("goBtn-troy"), "mouseout",function() {$("goBtn-troy").style.background = "#303030";});
  114. moveout();//页面加载完成,默认3s后隐藏到边栏
  115. };
  116. };
  117. //================样式区============
  118. //遮罩层样式
  119. var MarsCSS='\
  120. width:100px;\
  121. height:120px;\
  122. position:fixed;\
  123. right:20px;\
  124. z-index:99999;\
  125. '
  126. //两个按钮样式
  127. var BottonCSS='\
  128. width:40px;\
  129. height:40px;\
  130. text-align:center;\
  131. padding:5px;\
  132. margin:5px auto;\
  133. background:#303030;\
  134. color:#fff;\
  135. display:block;\
  136. opacity:0.8;\
  137. fitter:alpha(opacity:80);\
  138. cursor:pointer;\
  139. border-radius:50%;\
  140. box-shadow:2px 2px 40px 2px #303030;\
  141. font-size:16px;\
  142. font-family:"微软雅黑";\
  143. z-index:9999;\
  144. '
  145. //================执行区============
  146. addEvent(window.top,"load",function(){createBtn()});
  147. addEvent(window.top, "resize",function(){createBtn()});
  148. })()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址