Smoothscroll

Smooth scrolling on pages using javascript

当前为 2019-05-19 提交的版本,查看 最新版本

  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 9.1
  8. // ==/UserScript==
  9.  
  10. var Smoothscroll = {};
  11.  
  12.  
  13. //settings
  14. Smoothscroll.Smoothness = 0.5;
  15. Smoothscroll.Acceleration = 0.5;
  16.  
  17.  
  18. //debug
  19. Smoothscroll.Debug = false;
  20. //autodetected
  21. Smoothscroll.Refreshrate = 60;
  22.  
  23. //scrolling and animation
  24. function ScrollSubpixels(element, newvalue)
  25. {
  26. if (newvalue!=undefined)
  27. {
  28. element.scrollsubpixels = newvalue;
  29. return newvalue;
  30. }
  31. else
  32. {
  33. var olddelta = element.scrollsubpixels;
  34. if (olddelta!=undefined)
  35. {
  36. return olddelta;
  37. }
  38. return 0;
  39. }
  40. }
  41. function ScrollPixels(element, newvalue)
  42. {
  43. if (newvalue!=undefined)
  44. {
  45. element.scrollpixels = newvalue;
  46. ScrollSubpixels(element, 0);
  47. return newvalue;
  48. }
  49. else
  50. {
  51. var olddelta = element.scrollpixels;
  52. if (olddelta!=undefined)
  53. {
  54. return olddelta;
  55. }
  56. return 0;
  57. }
  58. }
  59.  
  60. var last = 0;
  61. function AnimateScroll(target, now) {
  62. var scrollsubpixels = ScrollSubpixels(target);
  63. var scrollpixels = ScrollPixels(target);
  64.  
  65. var scrolldirection = 0;
  66. if (scrollpixels>0) {
  67. scrolldirection = 1;
  68. }
  69. if (scrollpixels<0) {
  70. scrolldirection = -1;
  71. }
  72.  
  73. var scrollratio = 1-Math.pow( Smoothscroll.Refreshrate, -1/(Smoothscroll.Refreshrate*Smoothscroll.Smoothness));
  74. var scrollrate = scrollpixels*scrollratio;
  75. if (Math.abs(scrollpixels)>1) {
  76. var fullscrolls = Math.floor(Math.abs(scrollrate))*scrolldirection;
  77. var scrollsubpixelsadded = scrollrate - fullscrolls;
  78.  
  79. var additionalscrolls = Math.floor(Math.abs(scrollsubpixels + scrollsubpixelsadded))*scrolldirection;
  80. var scrollsubpixelsleft = scrollsubpixels + scrollsubpixelsadded - additionalscrolls;
  81.  
  82. ScrollPixels(target, scrollpixels-fullscrolls-additionalscrolls);
  83. ScrollSubpixels(target, scrollsubpixelsleft);
  84. target.scrollTop += fullscrolls + additionalscrolls;
  85.  
  86. target.scrollanimated = true;
  87. window.requestAnimationFrame(function() {
  88. AnimateScroll(target);
  89. });
  90. } else
  91. {
  92. window.requestAnimationFrame(function() {
  93. ScrollPixels(target, 0);
  94. });
  95. target.scrollanimated = false;
  96. }
  97. }
  98.  
  99. Smoothscroll.Stop = function(target) {
  100. if (target) {
  101. ScrollPixels(target, 0);
  102. }
  103. }
  104. Smoothscroll.Start = function(target, scrollamount) {
  105. if (target) {
  106. var scrollpixels = ScrollPixels(target);
  107.  
  108. ScrollPixels(target, scrollamount);
  109. if (!target.scrollanimated) {
  110. AnimateScroll(target);
  111. }
  112. }
  113. }
  114.  
  115. if (typeof module !== 'undefined') {
  116. module.exports = Smoothscroll;
  117. }
  118.  
  119.  
  120. function CanScroll(element, dir) {
  121.  
  122. if (dir<0)
  123. {
  124. return element.scrollTop>0;
  125. }
  126. if (dir>0)
  127. {
  128. if (element==document.body) {
  129. if (element.scrollTop==0) {
  130. element.scrollTop = 3;
  131. if (element.scrollTop==0) {
  132. return false;
  133. }
  134. element.scrollTop = 0;
  135. }
  136. return Math.round(element.clientHeight+element.scrollTop)<(element.offsetHeight);
  137. }
  138. return Math.round(element.clientHeight+element.scrollTop)<(element.scrollHeight);
  139. }
  140. }
  141. function HasScrollbar(element)
  142. {
  143. //TODO: problem with webkit, body not scrollable?
  144. if (element==window || element==document) {
  145. return false;
  146. }
  147. if (element==document.body) {
  148. return window.getComputedStyle(document.body)['overflow-y']!="hidden";
  149. }
  150. //THANK YOU TO: https://tylercipriani.com/blog/2014/07/12/crossbrowser-javascript-scrollbar-detection/
  151. if (element==document.documentElement) {
  152. return window.innerWidth > document.documentElement.clientWidth;
  153. } else {
  154. //return (element.clientWidth-element.clientWidth)>0;
  155. var style = window.getComputedStyle(element);
  156. return style['overflow-y']!="hidden" && style['overflow-y']!="visible";
  157. }
  158.  
  159. }
  160.  
  161. function Scrollable(element, dir)
  162. {
  163. //TODO: problem with webkit, body not scrollable?
  164. if (element==document.body) {
  165. //return false;
  166. }
  167. var scrollablecheck = CanScroll(element, dir);
  168. if (!scrollablecheck) {
  169. if (Smoothscroll.Debug) {
  170. console.log("scrollablecheck: " + scrollablecheck);
  171. }
  172. return false;
  173. }
  174. var scrollbarcheck = HasScrollbar(element);
  175. if (!scrollbarcheck) {
  176. if (Smoothscroll.Debug) {
  177. console.log("scrollbarcheck: " + scrollbarcheck);
  178. }
  179. return false;
  180. }
  181.  
  182. if (Smoothscroll.Debug) {
  183. console.log("scrollablecheck: " + scrollablecheck);
  184. console.log("scrollbarcheck: " + scrollbarcheck);
  185. }
  186. return true;
  187. }
  188. function GetTarget(e) {
  189. var direction = e.deltaY;
  190. var nodes = e.path;
  191. if (Smoothscroll.Debug) {
  192. console.log("nodes: ");
  193. console.log(nodes);
  194. console.log("target: ");
  195. }
  196. for (var i=0; i<(nodes.length); i++) {
  197. var node = nodes[i];
  198. if (Smoothscroll.Debug) {
  199. console.log(node);
  200. }
  201. if (Scrollable(node, direction))
  202. {
  203. if (Smoothscroll.Debug) {
  204. console.log("true");
  205. }
  206. return node;
  207. }
  208. }
  209. if (Smoothscroll.Debug) {
  210. console.log("false");
  211.  
  212. }
  213.  
  214. return null;
  215. }
  216.  
  217.  
  218.  
  219. //mouse event scroll handlers
  220. function StopScroll(e) {
  221. var nodes = e.path;
  222.  
  223. for (var i=0; i<(nodes.length); i++) {
  224. var node = nodes[i];
  225. Smoothscroll.Stop(node);
  226. }
  227. }
  228. function StartScroll(e, target) {
  229.  
  230. if (e.defaultPrevented)
  231. {
  232. return true;
  233. }
  234. else
  235. {
  236. var direction = e.deltaY;
  237.  
  238. var scrollpixels = ScrollPixels(target);
  239.  
  240. var accelerationratio = Math.sqrt(Math.abs(scrollpixels/direction*Smoothscroll.Acceleration));
  241.  
  242. var acceleration = Math.round(direction*accelerationratio);
  243.  
  244. Smoothscroll.Start(target, scrollpixels + direction + acceleration);
  245.  
  246. e.preventDefault();
  247. }
  248. }
  249.  
  250. //mouse event call handlers
  251. function WheelEvent(e) {
  252. e.path = e.path || (e.composedPath && e.composedPath());
  253. var target = GetTarget(e);
  254.  
  255. if (target && e.path) {
  256. StartScroll(e, target);
  257. }
  258. }
  259. function ClickEvent(e) {
  260. e.path = e.path || (e.composedPath && e.composedPath());
  261. if (e.path) {
  262. StopScroll(e);
  263. }
  264. }
  265.  
  266. var now0 = null;
  267. function Fps(now) {
  268. if (now0 != null) {
  269. Smoothscroll.Refreshrate = 1000 / (now - now0);
  270. }
  271. now0 = now;
  272.  
  273. window.requestAnimationFrame(Fps);
  274. };
  275.  
  276. //init function
  277. function Init()
  278. {
  279.  
  280. if (window.top != window.self) {
  281. //console.log("Smoothscroll: ignoring iframe");
  282. return null;
  283. }
  284. if (window.Smoothscroll && window.Smoothscroll.Loaded) {
  285. //console.log("Smoothscroll: already loaded");
  286. return null;
  287. }
  288.  
  289. if (!window.requestAnimationFrame) {
  290. window.requestAnimationFrame =
  291. window.mozRequestAnimationFrame ||
  292. window.webkitRequestAnimationFrame;
  293. }
  294.  
  295. document.documentElement.addEventListener("mousewheel", function(e){
  296. WheelEvent(e);
  297. if (Smoothscroll.Debug) {
  298. console.log(e);
  299. }
  300. },{ passive: false });
  301.  
  302. document.documentElement.addEventListener("mousedown", function(e){
  303. ClickEvent(e);
  304. if (Smoothscroll.Debug) {
  305. console.log(e);
  306. }
  307. });
  308. window.Smoothscroll = Smoothscroll;
  309. window.Smoothscroll.Loaded = true;
  310. window.requestAnimationFrame(Fps);
  311. console.log("Smoothscroll: loaded");
  312. }
  313. Init();