Smoothscroll

Smooth scrolling on pages using javascript

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

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