TopAndDownButtonsEverywhere

Top and Down buttons everywhere (no Jquery)

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