Smoothscroll

Smooth scrolling on pages using javascript and jquery

目前为 2016-10-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Smoothscroll
  3. // @include http*
  4. // @author Creec Winceptor
  5. // @description Smooth scrolling on pages using javascript and jquery
  6. // @namespace https://greasyfork.org/users/3167
  7. // @run-at document-idle
  8. // @grant none
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @version 1.3
  13. // ==/UserScript==
  14.  
  15. if (window.top != window.self) //don't run on frames or iframes
  16. return;
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //DEFAULT SETTINGS HERE
  23. //DO NOT CHANGE ANYTHING HERE ANYMORE, USE SCRIPT COMMANDS -> CONFIGURE SMOOTHSCROLL
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. //Smoothness factor value (how strong the smoothing effect is)
  31. //values: 1-(infinite)
  32. var smoothness = 30;
  33.  
  34. //Scroll sensitivity
  35. //values: anything? (default 1.00)
  36. var sensitivity = 1;
  37.  
  38. //Acceleration sensitivity
  39. //values: anything? (default 1.50)
  40. var acceleration = 1;
  41.  
  42. //Refreshrate setting
  43. //values: 30-144 (default = 60/72/120/144 = same as your monitor hz)
  44. var baserefreshrate = 60;
  45.  
  46.  
  47. //Alternative scrolling multiplier
  48. //values: true/false (try to set this to true if scrolling is too slow/doesn't work)
  49. var alternative_sensitivity_multiplier = false;
  50.  
  51.  
  52. //CODE STARTS HERE
  53.  
  54. var minimal_jquery_version = 200;
  55.  
  56. //max retries
  57. var jquery_retry_max = 5;
  58. var jquery_retry_count = 0;
  59.  
  60.  
  61. var DEBUG = false;
  62.  
  63. var WEBKIT = false;
  64.  
  65. //this.$ = this.jQuery = jQuery.noConflict(true);
  66.  
  67. console.log("Loading smoothscroll...");
  68.  
  69. if (smoothness>100)
  70. {
  71. smoothness = 100;
  72. }
  73.  
  74. if (baserefreshrate <= 30 || baserefreshrate>144)
  75. {
  76. baserefreshrate = 144;
  77. }
  78. refreshrate = baserefreshrate;
  79.  
  80. var animationduration = Math.round(1000/refreshrate);
  81. //var relativeratio = Math.round(51-smoothness/2)/100;
  82. var relativeratio = Math.round(1/(1+smoothness*refreshrate/baserefreshrate)*100)/100;
  83. //var relativeratio = relativeratio;
  84.  
  85.  
  86.  
  87. var lastLoop = new Date;
  88. //var lastrefreshrate = 0;
  89. function gameLoop() {
  90. var thisLoop = new Date;
  91. var refreshrate0 = 1000 / (thisLoop - lastLoop + 1);
  92. lastLoop = thisLoop;
  93. refreshrate = refreshrate + (refreshrate0-refreshrate)*0.1;
  94. refreshrate = Math.round(refreshrate);
  95. if (DEBUG)
  96. {
  97. console.log(refreshrate);
  98. }
  99. //console.log(refreshrate);
  100. animationduration = Math.round(1000/(refreshrate));
  101. //var relativeratio = Math.round(51-smoothness/2)/100;
  102. relativeratio = Math.round(1/(1+smoothness*refreshrate/baserefreshrate)*100)/100;
  103. }
  104. gameLoop();
  105.  
  106.  
  107. function InitSmoothscroll()
  108. {
  109. LoadConfig();
  110.  
  111. InitConfigmenu();
  112.  
  113. var startposition = false;
  114. var targetposition = 0;
  115. var position = 0;
  116.  
  117. var scrollfocus = ss$('body');
  118. var mousemoved = true;
  119. function hasScrollBarVisible(element)
  120. {
  121. //return (document.documentElement.scrollHeight !== document.documentElement.clientHeight);
  122. // Get the computed style of the body element
  123. var cStyle = element.currentStyle||window.getComputedStyle(element, "");
  124. // Check the overflow and overflowY properties for "auto" and "visible" values
  125. var scrollbar1 = cStyle.overflow == "scroll" || cStyle.overflowY == "scroll";
  126.  
  127. var scrollbar2 = cStyle.overflow == "auto" || cStyle.overflowY == "auto";
  128. var scrollbar = scrollbar1 || scrollbar2;
  129. return scrollbar;
  130. }
  131.  
  132.  
  133. function hasscrollbars(scrollfocus)
  134. {
  135. var hasvisiblescrollbars = hasScrollBarVisible(scrollfocus);
  136. var parentelement = ss$(scrollfocus).parent();
  137. if ( ss$(parentelement))
  138. {
  139. if (ss$(parentelement).is("textarea") || ss$(scrollfocus).is("textarea") || ss$(parentelement).is("article") || ss$(parentelement).is("article"))
  140. {
  141. return true;
  142. }
  143. else
  144. {
  145. if (ss$(parentelement).hasClass( "yt-scrollbar" ) || ss$(scrollfocus).hasClass( "yt-scrollbar" ))
  146. {
  147. return true;
  148. }
  149. return hasvisiblescrollbars;
  150. }
  151. }
  152. else
  153. {
  154. return hasvisiblescrollbars;
  155. }
  156. return false;
  157. }
  158.  
  159. function UpdatePosition(element)
  160. {
  161. gameLoop();
  162. var positiondelta = scrollpositiondelta(element);
  163. var smoothdelta = Math.sqrt(positiondelta*positiondelta*relativeratio);
  164. if (positiondelta<0)
  165. {
  166. smoothdelta = smoothdelta*(-1);
  167. }
  168. if (Math.abs( (positiondelta-smoothdelta)) <= 1 )
  169. {
  170. ss$(element).stop();
  171. ss$(element).animate({
  172. scrollTop: '+=' + Math.round(positiondelta)
  173. }, animationduration, "linear", function() {
  174. scrollpositiondelta(element, 0);
  175. if (DEBUG)
  176. {
  177. ss$(element).css( "border", "1px solid red" );
  178. }
  179. });
  180. }
  181. else
  182. {
  183. ss$(element).stop();
  184. ss$(element).animate({
  185. scrollTop: '+=' + Math.round(smoothdelta)
  186. }, animationduration, "linear", function() {
  187. scrollpositiondelta(element, positiondelta-smoothdelta);
  188. UpdatePosition(element);
  189. if (DEBUG)
  190. {
  191. ss$(element).css( "border", "1px solid red" );
  192. }
  193. });
  194. }
  195. }
  196.  
  197.  
  198. function MouseScroll (e) {
  199. gameLoop();
  200. var mul = 1;
  201. if (!WEBKIT || alternative_sensitivity_multiplier)
  202. mul = 40;
  203. var x = e.deltaX*mul;
  204. var y = e.deltaY*mul;
  205. if (mousemoved)
  206. {
  207. scrollfocus = UpdateFocus(x,y);
  208. mousemoved = false;
  209. }
  210. //
  211. var positiondelta = 0;
  212. var lastscrolltop = 0;
  213.  
  214. var parentelement = ss$(scrollfocus).parent();
  215. var rolled = y;
  216. var lastscrolltop = ss$(scrollfocus).scrollTop();
  217. if (!canscroll(scrollfocus, rolled))
  218. {
  219. if (!ss$(scrollfocus).is("html"))
  220. {
  221. scrollfocus = UpdateFocus(x,y);
  222. return false;
  223. }
  224. else
  225. {
  226. //console.log("true");
  227. return false;
  228. }
  229. }
  230. else
  231. {
  232. e.preventDefault();
  233. }
  234. positiondelta = scrollpositiondelta(scrollfocus);
  235. var direction = rolled/Math.abs(rolled);
  236. var positiondeltadelta = Math.round(rolled*sensitivity + Math.sqrt(Math.abs(positiondelta-rolled*sensitivity))*acceleration*rolled*0.09/sensitivity);
  237. positiondelta = positiondelta + positiondeltadelta;
  238.  
  239. scrollpositiondelta(scrollfocus, positiondelta)
  240. UpdatePosition(ss$(scrollfocus));
  241. return true;
  242. }
  243.  
  244. function canscroll(element, dir)
  245. {
  246. //console.log(dir);
  247. if (dir>0)
  248. {
  249. dir = 1;
  250. }
  251. if (dir<0)
  252. {
  253. dir = -1;
  254. }
  255. var checkradius = 3; //pixels to try scrolling
  256. var canscroll0 = false;
  257. var scrollable = ss$(element);
  258. var lastscrolltop = ss$(scrollable).scrollTop();
  259. ss$(scrollable).scrollTop(lastscrolltop+dir*checkradius);
  260. if (ss$(scrollable).scrollTop()!=lastscrolltop)
  261. {
  262. canscroll0 = true;
  263. }
  264. ss$(scrollable).scrollTop(lastscrolltop);
  265. //console.log("canscroll0: " + ss$(element).prev().prop('nodeName') + " : " + canscroll0);
  266. return canscroll0;
  267. }
  268. function scrollpositiondelta(element, newdelta)
  269. {
  270. var target = ss$(element);
  271. var delta = 0;
  272. if (newdelta!=undefined)
  273. {
  274. //console.log(dir);
  275. var dir = 0;
  276. if (newdelta>0)
  277. {
  278. dir = 1;
  279. }
  280. if (newdelta<0)
  281. {
  282. dir = -1;
  283. }
  284. //ss$(target)[0].setAttribute("positiondelta", newdelta );
  285. ss$( target ).data( "positiondelta", newdelta );
  286. delta = newdelta;
  287. }
  288. else
  289. {
  290. //var olddelta = ss$(target)[0].getAttribute("positiondelta");
  291. var olddelta = ss$( target ).data( "positiondelta" );
  292. if (olddelta!=undefined)
  293. {
  294. delta = olddelta;
  295. }
  296. }
  297. return delta;
  298. }
  299.  
  300. function UpdateFocus(x,y) {
  301. if (scrollfocus)
  302. {
  303. //ss$(scrollfocus)[0].setAttribute( "positiondelta",0 );
  304. ss$(scrollfocus).stop();
  305. }
  306. var dir = y;
  307. var nodelist = document.querySelectorAll( ":hover" );
  308. //ss$(nodelist).stop();
  309. //console.log(nodelist);
  310. if (WEBKIT)
  311. {
  312. scrollfocus = ss$('body');
  313. }
  314. else
  315. {
  316. scrollfocus = ss$('html');
  317. }
  318. for (var i = nodelist.length-1; i >= 0 ; i--) {
  319. //var parent = nodelist[i-1];
  320. var newfocus = nodelist[i];
  321. if (DEBUG)
  322. {
  323. ss$(newfocus).css( "border", "1px solid blue" );
  324. //var debugtimer = setTimeout(function(){ ss$(newfocus).css( "border", "0px solid white" ); }, 1000);
  325. }
  326. //if (ss$(newfocus).hasScrollBar3() && hasScrollBarVisible(newfocus) && canscroll(newfocus, dir) && hasscrollbars(newfocus))
  327. if (canscroll(newfocus, dir) && hasscrollbars(newfocus))
  328. {
  329. scrollfocus = ss$(newfocus);
  330. return newfocus;
  331. }
  332. }
  333.  
  334. return scrollfocus;
  335. }
  336. ss$('html').bind({
  337. mousemove: function(e) {
  338. mousemoved = true;
  339. },
  340. mousedown: function(e) {
  341. if (DEBUG)
  342. {
  343. console.log(scrollfocus);
  344. }
  345. if (scrollfocus)
  346. {
  347. scrollpositiondelta(scrollfocus, 0);
  348. //ss$(scrollfocus).stop();
  349. }
  350. scrollfocus = UpdateFocus(0,0);
  351. //console.log("click");
  352. }
  353. });
  354.  
  355. document.documentElement.addEventListener("wheel", function(e){
  356. MouseScroll(e);
  357. //console.log("scrolling");
  358. });
  359.  
  360. //WEBKIT = 'webkitRequestAnimationFrame' in window;
  361. WEBKIT = document.compatMode == 'CSS1Compat' || document.compatMode == "BackCompat";
  362. //console.log("window: " + window);
  363. console.log("Smoothscroll initiated! Webkit: " + WEBKIT);
  364. }
  365.  
  366. var JQUERY = false;
  367. var OWNJQUERY = false;
  368. var OLDJQUERY = false;
  369.  
  370. var old$;
  371. var oldjQuery;
  372.  
  373. var ss$;
  374.  
  375. function jQueryVersion(temp$)
  376. {
  377. if (typeof temp$ == 'undefined' || typeof temp$.fn == 'undefined' || typeof temp$.fn.jquery == 'undefined')
  378. {
  379. return 0;
  380. }
  381.  
  382. var versiontable = temp$.fn.jquery.split('.');
  383. var version = 0;
  384. for (var i = versiontable.length-1; i >= 0; i--) {
  385. var power = versiontable.length-i;
  386. version += Math.pow(10,power-1)*versiontable[i];
  387. }
  388. return version;
  389. }
  390.  
  391.  
  392. function LoadConfig()
  393. {
  394. smoothness = GM_getValue( 'smoothness', smoothness );
  395. sensitivity = GM_getValue( 'sensitivity', sensitivity );
  396. acceleration = GM_getValue( 'acceleration', acceleration );
  397. baserefreshrate = GM_getValue( 'baserefreshrate', baserefreshrate );
  398. //alternative_sesitivity_multiplier = GM_getValue( 'alternative_sesitivity_multiplier', alternative_sesitivity_multiplier );
  399. console.log("Config for smoothscroll loaded!")
  400. }
  401.  
  402. function SaveConfig()
  403. {
  404. GM_setValue( 'smoothness', document.getElementById('ss-smoothness').value)
  405. GM_setValue( 'sensitivity', document.getElementById('ss-sensitivity').value)
  406. GM_setValue( 'acceleration', document.getElementById('ss-acceleration').value)
  407. GM_setValue( 'baserefreshrate', document.getElementById('ss-baserefreshrate').value)
  408. //console.log(document.getElementById('ss-alternative_sesitivity_multiplier').checked)
  409. console.log("Config for smoothscroll saved!")
  410. }
  411.  
  412. function InitConfigmenu()
  413. {
  414. console.log("Initiating smoothscroll config...");
  415. var configbar = document.createElement('div');
  416. configbar.setAttribute("id","ss-configbar");
  417. configbar.innerHTML = '<div style="display:block; width: 100%; height: auto; border: 0px solid #aaaaaa; background-color: grey;">Config page for smoothscroll (refresh page for changes to apply!) Made by: Creec Winceptor</div><div id="ss-config" style="margin:3px; display:block; width: auto; height: auto; border: 0px solid #554433;"><table style="width:auto;"><tr><td>Smoothness</td><td><input type="number" id="ss-smoothness" min="0" max="100" value="' + smoothness + '"></td><td> Smoothness factor value (how strong the smoothing effect is)</td></tr><tr><td>Sensitivity</td><td><input type="number" id="ss-sensitivity" min="0" max="100" value="' + sensitivity + '"></td><td> Scroll sensitivity (duh)</td></tr><tr><td>Acceleration</td><td><input type="number" id="ss-acceleration" min="0" max="100" value="' + acceleration + '"></td><td> Acceleration of continuous scroll action (saves your finger)</td></tr><tr><td>Refreshrate</td><td><input type="number" id="ss-baserefreshrate" min="1" max="100" value="' + baserefreshrate + '"></td><td>Refreshrate of scrollanimation (60Hz default)</td></tr></table></div><div style="width: 100%; height: auto; text-align: center; background-color: grey;"><input id="ss-save" type="button" value="Save config" style="width: 44%; height: auto; border: 0px solid #aaaaaa; margin: 3px"/><input id="ss-close" type="button" value="Close config" style="width: 44%; height: auto; border: 0px solid #aaaaaa; margin: 3px"/><div>';
  418. var configparent = document.getElementsByTagName("body")[0];
  419. configbar.style.width = '100%';
  420. configbar.style.display = 'none';
  421. configbar.style.position = 'absolute';
  422. configbar.style.zIndex = '9999';
  423. configbar.style.backgroundColor = 'white';
  424. configparent.insertBefore(configbar, configparent.childNodes[0]);
  425. ss$("#ss-close").click(function() {
  426. //ss$("#ss-config").animate({
  427. // height: '0'
  428. //}, 100);
  429. ss$("#ss-configbar").hide("slow");
  430. });
  431. ss$("#ss-save").click(function() {
  432. SaveConfig();
  433. });
  434. //ss$("#ss-configbar").hide();
  435. }
  436.  
  437. function ConfigSmoothscroll()
  438. {
  439. if (typeof ss$ == 'undefined'){
  440. alert("Smoothscroll is not running properly on this page!");
  441. return;
  442. }
  443. //ss$("#ss-config").animate({
  444. //height: '100px'
  445. //}, 100);
  446. ss$("#ss-configbar").show("slow");
  447. ss$("html, body").animate({ scrollTop: 0 }, "slow");
  448. console.log("opening config...");
  449. }
  450.  
  451. function LoadjQuery(loading)
  452. {
  453. if (loading)
  454. {
  455. console.log("Waiting for jQuery...");
  456. if (typeof $ == 'undefined' || typeof jQuery == 'undefined' || jQueryVersion($)<minimal_jquery_version)
  457. {
  458. if (jquery_retry_count<jquery_retry_max)
  459. {
  460. jquery_retry_count++;
  461. setTimeout(function() {
  462. LoadjQuery(true);
  463. }, 100);
  464. }
  465. else
  466. {
  467. console.log("Failed to load smoothscroll!");
  468. if (typeof old$ != 'undefined') {
  469. $ = old$;
  470. }
  471. if (typeof oldjQuery != 'undefined') {
  472. jQuery = oldjQuery;
  473. }
  474. }
  475. }
  476. else
  477. {
  478. ss$ = $;
  479. ssjQuery = jQuery;
  480. console.log("jQuery loaded!");
  481. if (typeof old$ != 'undefined') {
  482. $ = old$;
  483. }
  484. if (typeof oldjQuery != 'undefined') {
  485. jQuery = oldjQuery;
  486. }
  487. console.log("Page jQuery version: " + jQueryVersion(old$));
  488. console.log("Script jQuery version: " + jQueryVersion(ss$));
  489. InitSmoothscroll();
  490. }
  491. }
  492. else
  493. {
  494. console.log("Loading own jQuery...");
  495. jquery_retry_count = 0;
  496. var filename = "https://code.jquery.com/jquery-2.2.3.js";
  497. var fileref = document.createElement('script');
  498. fileref.setAttribute("type","text/javascript");
  499. fileref.setAttribute("src", filename);
  500.  
  501. if (typeof fileref!="undefined")
  502. {
  503. var scriptparent = document.getElementsByTagName("head")[0];
  504.  
  505. if (typeof scriptparent=="undefined")
  506. {
  507. var scriptparent = document.createElement('head');
  508. document.getElementsByTagName("html")[0].appendChild(scriptparent);
  509. }
  510. scriptparent.appendChild(fileref);
  511. }
  512. LoadjQuery(true);
  513. }
  514. }
  515.  
  516.  
  517. function Init()
  518. {
  519.  
  520. if (typeof ss$ == 'undefined' )
  521. {
  522. var sitejquery = !(typeof $ == 'undefined' || typeof jQuery == 'undefined');
  523. if (sitejquery && jQueryVersion($)>=minimal_jquery_version)
  524. {
  525. ss$ = $;
  526. ssjQuery = jQuery;
  527. console.log("Reusing page jQuery...");
  528. console.log("Page jQuery version: " + jQueryVersion($));
  529. InitSmoothscroll();
  530. }
  531. else
  532. {
  533. if (typeof $ != 'undefined' && typeof old$ == 'undefined')
  534. {
  535. old$ = $;
  536. }
  537. if (typeof jQuery != 'undefined' && typeof oldjQuery == 'undefined')
  538. {
  539. oldjQuery = jQuery;
  540. }
  541. LoadjQuery(false);
  542. }
  543. }
  544. }
  545.  
  546. if (typeof ss$ == 'undefined'){
  547. console.log("Initiating smoothscroll...");
  548. Init();
  549. //InitSmoothscroll();
  550. GM_registerMenuCommand("Configurate smoothscroll", ConfigSmoothscroll);
  551. }
  552. else
  553. {
  554. console.log("Smoothscroll already loaded!");
  555. }