TopAndDownButtonsEverywhere

Top and Down buttons everywhere (no Jquery) 在一切页面上根据情况自动添加一个/两个按钮:页面最上方、页面最下方,鼠标悬停的时候,缓缓翻页移动

  1. // ==UserScript==
  2. // @name TopAndDownButtonsEverywhere
  3. // @namespace TopAndDownButtonsEverywhere
  4. // @description Top and Down buttons everywhere (no Jquery) 在一切页面上根据情况自动添加一个/两个按钮:页面最上方、页面最下方,鼠标悬停的时候,缓缓翻页移动
  5. // @version 1.4
  6. // @author Max Max
  7. // @license MIT
  8. // @include *
  9. // @run-at document-end
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. // [1] skip all iframe
  14. if (window.self!=window.top) {return}
  15.  
  16. // create element
  17. if (!window.ce) function ce(n) { return document.createElement(n); } // end of function
  18.  
  19. // add style
  20. function addStyle(css) {
  21. var head = document.head || document.getElementsByTagName('head')[0];
  22. if (head) {
  23. var style = ce("style");
  24. style.type = "text/css";
  25. style.appendChild(document.createTextNode(css));
  26. head.appendChild(style);
  27. } // end if
  28. } // end of function
  29.  
  30. // global variables
  31. var position,
  32. // figure out if this is moz || IE because they use documentElement
  33. el = (navigator.userAgent.indexOf('Firefox') != -1 || navigator.userAgent.indexOf('MSIE') != -1) ? document.documentElement : document.body,
  34. // timer
  35. t1, t2,
  36. // speed by
  37. speed_by_click = 500, // edit this value
  38. speed_by_over = 100, // edit this value
  39. // z-index
  40. zIindex = 999; // edit this value
  41.  
  42. // move up
  43. function move_up() {
  44. position = document.documentElement.scrollTop || document.body.scrollTop;
  45. window.scrollTo(0, position-1);
  46. t1 = setTimeout(move_up, speed_by_over);
  47. } // end of function
  48.  
  49. // move downn
  50. function move_dn() {
  51. position = document.documentElement.scrollTop || document.body.scrollTop;
  52. window.scrollTo(0, position+1);
  53. t2 = setTimeout(move_dn, speed_by_over);
  54. } // end of function
  55.  
  56. // document height
  57. function getDocumentHeight() {
  58. return (document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
  59. } // end of function
  60.  
  61. // document scroll
  62. function get_scroll(a) {
  63. var d = document,
  64. b = d.body,
  65. e = d.documentElement,
  66. c = "client" + a,
  67. a = "scroll" + a;
  68. return /CSS/.test(d.compatMode)? (e[c]< e[a]) : (b[c]< b[a])
  69. } // end of function
  70.  
  71.  
  72.  
  73.  
  74. function scrollTo(element, to, duration) {
  75. var start = element.scrollTop,
  76. change = to - start,
  77. currentTime = 0,
  78. increment = 20,
  79. newDuration = (typeof(duration) === 'undefined') ? 500: duration;
  80. var animateScroll = function(){
  81. currentTime += increment;
  82. var val = Math.easeInOutQuad(currentTime, start, change, newDuration);
  83. element.scrollTop = val;
  84. if(currentTime < newDuration) {
  85. setTimeout(animateScroll, increment);
  86. }
  87. };
  88. animateScroll();
  89. } // end of function
  90.  
  91. //t = current time
  92. //b = start value
  93. //c = change in value
  94. //d = duration
  95. Math.easeInOutQuad = function (t, b, c, d) {
  96. t /= d/2;
  97. if (t < 1) return c/2*t*t + b;
  98. t--;
  99. return -c/2 * (t*(t-2) - 1) + b;
  100. };
  101.  
  102. function create_btn_element() {
  103. // get scroll
  104. var up, dn,
  105. scrolled,
  106. h = get_scroll('Height');
  107. // exit
  108. if(!h) { return; } // end if
  109. // add css
  110. shareCSS();
  111.  
  112. // if
  113. if(el){
  114. // create DOM element
  115. up = ce('span');
  116. dn = ce('span');
  117. // set attribute
  118. up.setAttribute('id','play_btn_up');
  119. dn.setAttribute('id','play_btn_dn');
  120. // set class
  121. up.className = "play_btn";
  122. dn.className = "play_btn";
  123. // append element
  124. document.body.appendChild(up);
  125. document.body.appendChild(dn);
  126. // scroll
  127. scrolled = window.pageYOffset || document.documentElement.scrollTop;
  128. // if scroll
  129. up.style.display = (scrolled > 0) ? "" : "none";
  130. // add event over
  131. up.addEventListener('mouseover', move_up, false);
  132. dn.addEventListener('mouseover', move_dn, false);
  133. // add event out
  134. up.addEventListener('mouseout', function(){clearTimeout(t1);},false);
  135. dn.addEventListener('mouseout', function(){clearTimeout(t2);},false);
  136. // add event click
  137. up.addEventListener('click', function(){ scrollTo(el, 0, speed_by_click); }, false);
  138. dn.addEventListener('click', function(){ scrollTo(el, getDocumentHeight(), speed_by_click); }, false);
  139. // add event scroll
  140. window.onscroll = function() {
  141. var scrolled = window.pageYOffset || document.documentElement.scrollTop, diffHeight = document.body.scrollHeight - window.innerHeight;
  142. // if scroll up
  143. up.style.display = (scrolled > 0) ? "" : "none";
  144. // if scroll dn
  145. dn.style.display = (diffHeight > scrolled) ? "" : "none";
  146. }; // end of function
  147. } // end if
  148. } // end of function
  149.  
  150. // add css
  151. function shareCSS(){
  152. // variables
  153. var s='', img_up, img_dn;
  154. // img vs button
  155. img_up = 'data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAUCAYAAACAl21KAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAB+SURBVDhPY1i1atV/amAGahgCMoNhaIGlS5cKAp19BoRBbLJcj2QILDJINwzoAmMgfoclIkBixkS5DI8hMJcRNgxoSBoOl6CnNZBhaVhdBjWE1MSJahjQkA4KEmYH2GUrV66cSYEhYB+AzKBtFiHkQqKiH6Ro1CDCQTWgYQQAs81DU0G/83sAAAAASUVORK5CYII=';
  156. img_dn = 'data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAUCAYAAACAl21KAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACPSURBVDhPY2DAAlatWvUfH8amB6vYqEGEg2pgw4iQ7cTKM6xcuXImsYpxqQOZAQ4woIIOCgzrQAl1oEFpZBiWhitFgwx7R4SBIDXYDYGZDFRgTMAwkCHGhBMRJMxwGUa8ITCbli5dKgg08AySN8+AxIhyCboiJMPIN4Qsm6miiYioxltawvSDYogohYTUAQC80UNTOht/YwAAAABJRU5ErkJggg==';
  157. // button id
  158. s+='#play_btn_up { position:fixed; right:0; bottom:53%;z-index:'+zIindex+'; height:36px; width:36px; cursor:pointer; background:url('+img_up+') no-repeat scroll 50% 50% rgba(0, 0, 0, 0.7); border-radius:5px 0 0 5px; margin-top:-24px; }';
  159. s+='#play_btn_dn { position:fixed; right:0; top:53%; z-index:'+zIindex+'; height:36px; width:36px; cursor:pointer; background:url('+img_dn+') no-repeat scroll 50% 50% rgba(0, 0, 0, 0.7); border-radius:5px 0 0 5px; margin-top:-24px; }';
  160. // button class
  161. s+='.play_btn { -webkit-transition-duration:0.5s linear; -o-transition-duration:0.5s linear; -moz-transition-duration:0.5s linear; transition-duration:0.5s linear; opacity:0.65; }';
  162. s+='.play_btn:hover { opacity:1; }';
  163. // append
  164. addStyle(''+s);
  165. } // end of function
  166.  
  167. // run it
  168. create_btn_element();

QingJ © 2025

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