Smoothscroll

Smooth scrolling on pages using javascript

当前为 2018-09-11 提交的版本,查看 最新版本

  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 7
  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. function CanScroll(element, dir) {
  134. if (dir<0)
  135. {
  136. return element.scrollTop>0;
  137. }
  138. if (dir>0)
  139. {
  140. return Math.round(element.clientHeight+element.scrollTop)<(element.scrollHeight-1)
  141. }
  142. }
  143. function HasScrollbar(element)
  144. {
  145. //TODO: problem with webkit, body not scrollable?
  146. if (element==window || element==document) {
  147. return false;
  148. }
  149. //THANK YOU TO: https://tylercipriani.com/blog/2014/07/12/crossbrowser-javascript-scrollbar-detection/
  150. if (element==document.documentElement) {
  151. return window.innerWidth > document.documentElement.clientWidth;
  152. } else {
  153. return (element.offsetWidth-element.clientWidth)>0;
  154. }
  155.  
  156. }
  157.  
  158. function Scrollable(element, dir)
  159. {
  160. //TODO: problem with webkit, body not scrollable?
  161. if (element==document.body) {
  162. return false;
  163. }
  164. var scrollablecheck = CanScroll(element, dir);
  165.  
  166. var scrollbarcheck = HasScrollbar(element);
  167.  
  168. if (Smoothscroll.Debug) {
  169. console.log("scrollablecheck: " + scrollablecheck);
  170. console.log("scrollbarcheck: " + scrollbarcheck);
  171. }
  172.  
  173. return scrollablecheck && scrollbarcheck;
  174. }
  175. function GetTarget(e) {
  176. var direction = e.deltaY;
  177. var nodes = e.path;
  178. if (Smoothscroll.Debug) {
  179. console.log("nodes: ");
  180. console.log(nodes);
  181. console.log("target: ");
  182. }
  183. for (var i=0; i<(nodes.length); i++) {
  184. var node = nodes[i];
  185. if (Smoothscroll.Debug) {
  186. console.log(node);
  187. }
  188. if (Scrollable(node, direction))
  189. {
  190. if (Smoothscroll.Debug) {
  191. console.log("true");
  192. }
  193. return node;
  194. }
  195. }
  196. if (Smoothscroll.Debug) {
  197. console.log("false");
  198.  
  199. }
  200.  
  201. return null;
  202. }
  203.  
  204.  
  205.  
  206. //mouse event scroll handlers
  207. function StopScroll(e) {
  208. var nodes = e.path;
  209. for (var i=0; i<nodes.length; i++) {
  210. var node = nodes[i];
  211. Smoothscroll.Stop(node);
  212. }
  213. }
  214. function StartScroll(e, target) {
  215.  
  216. if (e.defaultPrevented)
  217. {
  218. return true;
  219. }
  220. else
  221. {
  222. var direction = e.deltaY;
  223.  
  224. var scrollpixels = ScrollPixels(target);
  225.  
  226. var accelerationratio = Math.sqrt(Math.abs(scrollpixels/direction*Smoothscroll.Acceleration));
  227.  
  228. var acceleration = Math.round(direction*accelerationratio);
  229.  
  230. Smoothscroll.Start(target, scrollpixels + direction + acceleration);
  231.  
  232. e.preventDefault();
  233. }
  234. }
  235.  
  236.  
  237.  
  238. //mouse event call handlers
  239. function WheelEvent(e) {
  240. var target = GetTarget(e);
  241.  
  242. if (target) {
  243. StartScroll(e, target);
  244. }
  245. }
  246. function ClickEvent(e) {
  247. StopScroll(e);
  248. }
  249.  
  250.  
  251.  
  252. //init function
  253. function Init()
  254. {
  255.  
  256. if (window.top != window.self) {
  257. //console.log("Smoothscroll: ignoring iframe");
  258. return null;
  259. }
  260. if (window.Smoothscroll && window.Smoothscroll.Loaded) {
  261. //console.log("Smoothscroll: already loaded");
  262. return null;
  263. }
  264. document.documentElement.addEventListener("wheel", function(e){
  265. WheelEvent(e);
  266. if (Smoothscroll.Debug) {
  267. console.log(e);
  268. }
  269. });
  270.  
  271. document.documentElement.addEventListener("mousedown", function(e){
  272. ClickEvent(e);
  273. if (Smoothscroll.Debug) {
  274. console.log(e);
  275. }
  276. });
  277. window.Smoothscroll = Smoothscroll;
  278. window.Smoothscroll.Loaded = true;
  279. console.log("Smoothscroll: loaded");
  280. }
  281. Init();