Scroll To Top-Bottom Button

Add Scroll To Top-Bottom Button in the lower right corner of the page

  1. // ==UserScript==
  2. // @name Scroll To Top-Bottom Button
  3. // @version v1.3.2
  4. // @include http://*
  5. // @include https://*
  6. // @description Add Scroll To Top-Bottom Button in the lower right corner of the page
  7. // @grant GM_addStyle
  8. // @namespace https://greasyfork.org/users/293086
  9. // ==/UserScript==
  10. (function(global) {
  11. if(global !== window) return;
  12.  
  13. function _(id) {
  14. return document.getElementById(id);
  15. }
  16.  
  17. function bind(context, name) {
  18. return function() {
  19. return context[name].apply(context, arguments);
  20. }
  21. }
  22.  
  23. global.addEventListener('scroll', scrollHandler, false);
  24.  
  25. function scrollHandler() {
  26. !scroll.isScrolling && ((scroll.getScrollY() > 0) ? scroll.showBtn() : scroll.hideBtn());
  27. }
  28.  
  29. var scroll = {
  30. __scrollY : 0,
  31. isScrolling : false, //is scrolling
  32. imgBtn : null,
  33. isBtnShow : false,
  34. pageHeight : 0,
  35. speed : 0.75,
  36. init : function() {
  37. var document = global.document,
  38. div = document.createElement('div'),
  39. css;
  40. css = '#__scrollToTop{font:12px/1em Arial,Helvetica,sans-serif;margin:0;padding:0;position:fixed;display:none;left:92%;top:80%;text-align:center;z-index:999999; width:74px;height:50px;' +
  41. 'cursor:pointer;opacity:0.5;padding:2px;}' +
  42. '#__scrollToTop:hover{opacity:1;}' +
  43. '#__scrollToTop span.__scroll__arrow{ position:relative;top:20px;background:none repeat scroll 0 0 #eee;border-style:solid; border-width:1px;' +
  44. 'border-color:#ccc #ccc #aaa; border-radius:5px;color:#333;font-size:36px;padding:5px 8px 2px;}' +
  45. ' #__scroll__scroll{height:50px;width:50px;float:left;z-index:100001;position:absolute;} ' +
  46. '#__scroll__util{font:12px/1em Arial,Helvetica,sans-serif;text-align:center;height:44px;width:20px;float:right;position:absolute;left:54px;z-index:100000; ' +
  47. 'border-style:solid; border-width:1px;border-color:#ccc #ccc #aaa; border-radius:2px;top:5px;display:none;}' +
  48. '#__scroll__util span{display:block;height:18px;padding-top:4px;text-align:center;text-shadow:2px 2px 2px #888;font-size:16px;} ' +
  49. '#__scroll__util span:hover{background-color: #fc9822;}';
  50.  
  51. GM_addStyle(css);
  52.  
  53. div.id = '__scrollToTop';
  54. div.title = 'Back To Top';
  55. div.innerHTML = '<div id="__scroll__scroll">' +
  56. '<span class="__scroll__arrow">▲</span>' +
  57. '</div>' +
  58. '<div id="__scroll__util">' +
  59. '<span name="__hide" title="Hide the Button">x</span>' +
  60. '<span name="__bottom" title="Scroll to the bottom">▼</span>' +
  61. '</div>';
  62. document.body.appendChild(div);
  63. div.addEventListener('mousedown', bind(this, 'control'),false);
  64. div.addEventListener('mouseover', bind(this, 'showUtil'),false);
  65. div.addEventListener('mouseout', bind(this, 'hideUtil'),false);
  66.  
  67. this.util = _('__scroll__util');
  68. this.pageUtil = _('__scroll__page');
  69. this.pageHeight = document.body.scrollHeight;
  70. return this.imgBtn = div;
  71.  
  72. },
  73. getImgBtn : function() {
  74. return this.imgBtn || this.init();
  75. },
  76. show : function(elem) {
  77. elem.style.display = 'block';
  78. },
  79. hide : function(elem) {
  80. elem.style.display = 'none';
  81. },
  82. showBtn : function() {
  83. if(this.isBtnShow) return;
  84. this.isBtnShow = true;
  85. this.show(this.getImgBtn());
  86. },
  87. hideBtn : function() {
  88. if(!this.isBtnShow) return;
  89. this.isBtnShow = false;
  90. this.hide(this.getImgBtn());
  91. },
  92. getScrollY : function() {
  93. //this piece of code is from John Resig's book 'Pro JavaScript Techniques'
  94. var de = document.documentElement;
  95. return this.__scrollY = (self.pageYOffset ||
  96. ( de && de.scrollTop ) ||
  97. document.body.scrollTop);
  98. },
  99. closeBtn : function(event) {
  100. event.preventDefault();
  101. event.stopPropagation();
  102. this.hideBtn();
  103. window.removeEventListener('scroll', scrollHandler, false);
  104. },
  105. showUtil : function() {
  106. this.show(this.util);
  107. },
  108. hideUtil : function() {
  109. this.hide(this.util);
  110. },
  111. scroll : function() {
  112. if(!this.isScrolling) {
  113. this.isScrolling = true;
  114. }
  115. var isStop = false,
  116. scrollY = this.__scrollY;
  117. if(this.direction === 'top') {
  118. isStop = scrollY > 0;
  119. this.__scrollY = Math.floor(scrollY * this.speed);
  120. } else {
  121. isStop = scrollY < this.pageHeight;
  122. this.__scrollY += Math.ceil((this.pageHeight - scrollY) * (1 - this.speed)) + 10;
  123. }
  124. this.isScrolling = isStop;
  125. window.scrollTo(0, this.__scrollY);
  126. isStop ? setTimeout(bind(scroll, 'scroll'), 20) : (this.direction === 'top' && this.hideBtn());
  127. },
  128. control : function(e) {
  129. var t = e.target, name = t.getAttribute('name');
  130. switch(name) {
  131. case '__bottom':
  132. this.scrollToBottom();
  133. break;
  134. case '__hide' :
  135. this.closeBtn(e);
  136. break;
  137. default :
  138. this.scrollToTop();
  139. break;
  140. }
  141. },
  142. scrollToTop : function() {
  143. this.direction = 'top';
  144. this.scroll();
  145. },
  146. scrollToBottom : function() {
  147. this.direction = 'bottom';
  148. var bodyHeight = global.document.body.scrollHeight,
  149. documentElementHeight = global.document.documentElement.scrollHeight;
  150. this.pageHeight = Math.max(bodyHeight, documentElementHeight);
  151. this.scroll();
  152. }
  153. };
  154.  
  155. //Autoscroll
  156. (function() {
  157. var isAutoScroll = false;
  158.  
  159. var autoScroll = {
  160. __autoScrollID : 0,
  161.  
  162. isAutoScroll : false,
  163.  
  164. defaultSpeed : 1,
  165.  
  166. currentSpeed : 1,
  167.  
  168. intervalTime : 100,
  169.  
  170. reset : function() {
  171. this.isAutoScroll && (this.currentSpeed = this.defaultSpeed);
  172. },
  173.  
  174. startOrStop : function() {
  175. var that = this;
  176. if(that.isAutoScroll) {
  177. that.isAutoScroll = false;
  178. clearInterval(that.__autoScrollID);
  179. } else {
  180. that.isAutoScroll = true;
  181. that.__autoScrollID = setInterval(function() {
  182. global.scrollBy(0, that.currentSpeed);
  183. }, that.intervalTime);
  184. }
  185. },
  186.  
  187. fast : function() {
  188. this.isAutoScroll && this.currentSpeed <= 10 && this.currentSpeed++;
  189. },
  190.  
  191. slow : function() {
  192. this.isAutoScroll && this.currentSpeed > 1 && this.currentSpeed--;
  193. },
  194.  
  195. keyControl : function(e) {
  196. if(e.target != global.document.body && e.target != global.document.documentElement) return false; // only when the cursor focus on the page rather than the input area can trigger this event.
  197. var charCode = e.charCode,
  198. key = this.keyMap[charCode];
  199. key && this[key]();
  200. },
  201.  
  202. keyMap : {
  203. '100' : 'slow', // press 'd', slow the speed of the scroll
  204. '102' : 'fast', // press 'f', speed scroll
  205. '114' : 'reset', // press 'r', reset the autoscroll's speed
  206. '115' : 'startOrStop' //when you click 's' at the first time, the autoscroll is begin, and then you click again, it will stop.
  207. }
  208. };
  209. global.addEventListener('keypress', bind(autoScroll, 'keyControl'), false);
  210. }())
  211. }(window.top))