Smoothscroll

Smooth scrolling on pages using javascript

当前为 2018-03-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Smoothscroll
  3. // @author Creec Winceptor
  4. // @description Smooth scrolling on pages using javascript
  5. // @namespace https://greasyfork.org/users/3167
  6. // @include *
  7. // @version 5.4
  8. // ==/UserScript==
  9.  
  10. var Smoothscroll = {};
  11.  
  12. //dev
  13. Smoothscroll.Debug = false;
  14. Smoothscroll.Refreshrate = 60;
  15.  
  16. //settings
  17. Smoothscroll.Smoothness = 0.5;
  18. Smoothscroll.Acceleration = 0.5;
  19.  
  20. //scrolling and animation
  21. function Timeout(element, newtimeout)
  22. {
  23. if (newtimeout!=undefined)
  24. {
  25. var oldtimeout = element.ScrollTimeout;
  26. if (oldtimeout!=undefined)
  27. {
  28. clearTimeout(oldtimeout);
  29. }
  30. element.ScrollTimeout = newtimeout;
  31. return newtimeout;
  32. }
  33. else
  34. {
  35. var oldtimeout = element.ScrollTimeout;
  36. if (oldtimeout!=undefined)
  37. {
  38. return oldtimeout;
  39. }
  40. return null;
  41. }
  42. }
  43. function ScrollSubpixels(element, newvalue)
  44. {
  45. if (newvalue!=undefined)
  46. {
  47. element.scrollsubpixels = newvalue;
  48. return newvalue;
  49. }
  50. else
  51. {
  52. var olddelta = element.scrollsubpixels;
  53. if (olddelta!=undefined)
  54. {
  55. return olddelta;
  56. }
  57. return 0;
  58. }
  59. }
  60. function ScrollPixels(element, newvalue)
  61. {
  62. if (newvalue!=undefined)
  63. {
  64. element.scrollpixels = newvalue;
  65. ScrollSubpixels(element, 0);
  66. return newvalue;
  67. }
  68. else
  69. {
  70. var olddelta = element.scrollpixels;
  71. if (olddelta!=undefined)
  72. {
  73. return olddelta;
  74. }
  75. return 0;
  76. }
  77. }
  78. function AnimateScroll(target) {
  79. var updaterate = Math.floor(1000/(Smoothscroll.Refreshrate));
  80. var scrollsubpixels = ScrollSubpixels(target);
  81. var scrollpixels = ScrollPixels(target);
  82.  
  83. var scrolldirection = 0;
  84. if (scrollpixels>0) {
  85. scrolldirection = 1;
  86. }
  87. if (scrollpixels<0) {
  88. scrolldirection = -1;
  89. }
  90.  
  91. var scrollratio = 1-Math.pow( Smoothscroll.Refreshrate, -1/(Smoothscroll.Refreshrate*Smoothscroll.Smoothness));
  92. var scrollrate = scrollpixels*scrollratio;
  93. if (Math.abs(scrollpixels)>1) {
  94. var fullscrolls = Math.floor(Math.abs(scrollrate))*scrolldirection;
  95. var scrollsubpixelsadded = scrollrate - fullscrolls;
  96.  
  97. var additionalscrolls = Math.floor(Math.abs(scrollsubpixels + scrollsubpixelsadded))*scrolldirection;
  98. var scrollsubpixelsleft = scrollsubpixels + scrollsubpixelsadded - additionalscrolls;
  99.  
  100. ScrollPixels(target, scrollpixels-fullscrolls-additionalscrolls);
  101. ScrollSubpixels(target, scrollsubpixelsleft);
  102. target.scrollTop += fullscrolls + additionalscrolls;
  103.  
  104. Timeout(target, setTimeout(function() {
  105.  
  106. AnimateScroll(target);
  107. }, updaterate));
  108. } else
  109. {
  110. ScrollPixels(target, 0);
  111. }
  112. }
  113.  
  114. Smoothscroll.Stop = function(target) {
  115. if (target) {
  116. ScrollPixels(target, null);
  117. Timeout(target, null);
  118. }
  119. }
  120. Smoothscroll.Start = function(target, scrollamount) {
  121. if (target) {
  122. var scrollpixels = ScrollPixels(target);
  123. ScrollPixels(target, scrollamount);
  124. AnimateScroll(target);
  125. }
  126. }
  127.  
  128. if (typeof module !== 'undefined') {
  129. module.exports = Smoothscroll;
  130. }
  131.  
  132.  
  133.  
  134. //scroll target detection
  135. function IsScrollable(element, dir)
  136. {
  137. var checkradius = 2; //pixels to try scrolling
  138.  
  139. if (dir>0)
  140. {
  141. dir = checkradius;
  142. }
  143. if (dir<0)
  144. {
  145. dir = -checkradius;
  146. }
  147.  
  148. var originalscroll = element.scrollTop;
  149. var testscroll = Math.round(originalscroll + dir);
  150. element.scrollTop = testscroll;
  151.  
  152. var scrollable = Math.round(element.scrollTop)==testscroll;
  153. element.scrollTop = originalscroll;
  154.  
  155. return scrollable;
  156. }
  157. function HasScrollbars(element)
  158. {
  159. //return (document.documentElement.scrollHeight !== document.documentElement.clientHeight);
  160.  
  161. // Get the computed style of the body element
  162. var cStyle = element.currentStyle||window.getComputedStyle(element, "");
  163.  
  164. // Check the overflow and overflowY properties for "auto" and "visible" values
  165. var scrollbar1 = cStyle.overflow == "scroll" || cStyle.overflowY == "scroll";
  166. var scrollbar2 = cStyle.overflow == "auto" || cStyle.overflowY == "auto";
  167.  
  168. //body or html always have scrollbars
  169. var scrollbar3 = element==document.body || element==document.documentElement;
  170. var scrollbar4 = cStyle.overflow != "hidden" && cStyle.overflowY != "hidden";
  171.  
  172. return scrollbar1 || scrollbar2 || (scrollbar3 && scrollbar4);
  173. }
  174. function GetTarget(e) {
  175. var direction = e.deltaY;
  176. var nodes = e.path;
  177. for (var i=0; i<nodes.length; i++) {
  178. var node = nodes[i];
  179. if (IsScrollable(node, direction) && HasScrollbars(node))
  180. {
  181. if (Smoothscroll.Debug) {
  182. console.log("scrollbar: ");
  183. console.log(node);
  184. }
  185. return node;
  186. }
  187. }
  188.  
  189. return null;
  190. }
  191.  
  192.  
  193.  
  194. //mouse event scroll handlers
  195. function StopScroll(e) {
  196. var nodes = e.path;
  197. for (var i=0; i<nodes.length; i++) {
  198. var node = nodes[i];
  199. Smoothscroll.Stop(node);
  200. }
  201. }
  202. function StartScroll(e, target) {
  203.  
  204. if (e.defaultPrevented)
  205. {
  206. return true;
  207. }
  208. else
  209. {
  210. var direction = e.deltaY;
  211.  
  212. var scrollpixels = ScrollPixels(target);
  213.  
  214. var accelerationratio = Math.sqrt(Math.abs(scrollpixels/direction*Smoothscroll.Acceleration));
  215.  
  216. var acceleration = Math.round(direction*accelerationratio);
  217.  
  218. Smoothscroll.Start(target, scrollpixels + direction + acceleration);
  219.  
  220. e.preventDefault();
  221. }
  222. }
  223.  
  224.  
  225.  
  226. //mouse event call handlers
  227. function WheelEvent(e) {
  228. var target = GetTarget(e);
  229. if (target) {
  230. StartScroll(e, target);
  231. }
  232. }
  233. function ClickEvent(e) {
  234. StopScroll(e);
  235. }
  236.  
  237.  
  238.  
  239. //init function
  240. function Init()
  241. {
  242.  
  243. if (window.top != window.self) {
  244. console.log("Smoothscroll: ignoring iframe");
  245. return null;
  246. }
  247. if (window.Smoothscroll && window.Smoothscroll.Loaded) {
  248. console.log("Smoothscroll: already loaded");
  249. return null;
  250. }
  251. document.documentElement.addEventListener("wheel", function(e){
  252. WheelEvent(e);
  253. if (Smoothscroll.Debug) {
  254. console.log(e);
  255. }
  256. });
  257.  
  258. document.documentElement.addEventListener("mousedown", function(e){
  259. ClickEvent(e);
  260. if (Smoothscroll.Debug) {
  261. console.log(e);
  262. }
  263. });
  264. window.Smoothscroll = Smoothscroll;
  265. window.Smoothscroll.Loaded = true;
  266. console.log("Smoothscroll: loaded");
  267. }
  268. Init();