TopAndDownButtonsEverywhere


SUBMITTED BY: AnD888

DATE: Oct. 6, 2016, 1:52 p.m.

FORMAT: Text only

SIZE: 6.3 kB

HITS: 2349

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

comments powered by Disqus