Smoothscroll

Smooth scrolling on pages using javascript and jquery

当前为 2016-07-17 提交的版本,查看 最新版本

  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-load
  8. // @grant none
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @version 1.0.9.1
  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 = ss$(element)[0].getAttribute( "positiondelta" );
  163. //var smoothdelta = Math.sqrt(positiondelta*positiondelta*relativeratio);
  164. var smoothdelta = Math.sqrt(positiondelta*positiondelta*relativeratio);
  165. if (positiondelta<0)
  166. {
  167. smoothdelta = smoothdelta*(-1);
  168. }
  169. //var relative = position - ss$(element).scrollTop();
  170. //console.log("smoothdelta:" + smoothdelta);
  171. if (Math.abs( (positiondelta-smoothdelta)) <= 1 )
  172. {
  173. ss$(element).stop();
  174. ss$(element).animate({
  175. scrollTop: '+=' + Math.round(positiondelta)
  176. }, animationduration, "linear", function() {
  177. ss$(element).attr( "positiondelta",0 );
  178. ss$(element)[0].setAttribute( "positiondelta",0 );
  179. if (DEBUG)
  180. {
  181. ss$(element).css( "border", "1px solid red" );
  182. }
  183. });
  184. }
  185. else
  186. {
  187. ss$(element).stop();
  188. ss$(element).animate({
  189. scrollTop: '+=' + Math.round(smoothdelta)
  190. }, animationduration, "linear", function() {
  191. ss$(element).attr( "positiondelta",positiondelta-smoothdelta );
  192. ss$(element)[0].setAttribute("positiondelta",positiondelta-smoothdelta );
  193. UpdatePosition(element);
  194. if (DEBUG)
  195. {
  196. ss$(element).css( "border", "1px solid red" );
  197. }
  198. });
  199. }
  200. }
  201.  
  202.  
  203. function MouseScroll (e) {
  204. gameLoop();
  205. var mul = 1;
  206. if (!WEBKIT || alternative_sensitivity_multiplier)
  207. mul = 40;
  208. var x = e.deltaX*mul;
  209. var y = e.deltaY*mul;
  210. if (mousemoved)
  211. {
  212. scrollfocus = UpdateFocus(x,y);
  213. mousemoved = false;
  214. }
  215. //
  216. var positiondelta = 0;
  217. var lastscrolltop = 0;
  218.  
  219. var parentelement = ss$(scrollfocus).parent();
  220. var rolled = y;
  221. var lastscrolltop = ss$(scrollfocus).scrollTop();
  222. ss$(scrollfocus).scrollTop(lastscrolltop+rolled);
  223. if (ss$(scrollfocus).scrollTop()==lastscrolltop)
  224. {
  225. if (!ss$(scrollfocus).is("html"))
  226. {
  227. focus = parentelement;
  228. //focus = UpdateFocus(event);
  229. //return MouseScroll (event);
  230. //console.log("false");
  231. //return false;
  232. }
  233. else
  234. {
  235. //console.log("true");
  236. //return false;
  237. }
  238. }
  239. else
  240. {
  241. e.preventDefault();
  242. }
  243. ss$(scrollfocus).scrollTop(lastscrolltop);
  244. if (ss$(scrollfocus)[0].getAttribute("positiondelta")==undefined)
  245. {
  246. positiondelta = 0;
  247. //console.log("positiondelta: undefined");
  248. }
  249. else
  250. {
  251. positiondelta = ss$(scrollfocus)[0].getAttribute("positiondelta");
  252. //console.log("positiondelta: " + positiondelta);
  253. }
  254. positiondelta = positiondelta*1;
  255. var direction = rolled/Math.abs(rolled);
  256. //var positiondeltadelta = rolled*sensitivity + Math.sqrt(Math.abs(positiondelta/rolled))*acceleration*rolled;
  257. //var positiondeltadelta = rolled*(sensitivity+Math.sqrt(Math.abs(positiondelta))*acceleration);
  258. var positiondeltadelta = Math.round(rolled*sensitivity + Math.sqrt(Math.abs(positiondelta-rolled*sensitivity))*acceleration*rolled*0.05/sensitivity);
  259. positiondelta = positiondelta + positiondeltadelta;
  260. ss$(scrollfocus)[0].setAttribute("positiondelta",positiondelta );
  261. UpdatePosition(ss$(scrollfocus));
  262. return true;
  263. }
  264.  
  265. function canscroll(element, dir)
  266. {
  267. var scrollable = ss$(element);
  268. var lastscrolltop = ss$(scrollable).scrollTop();
  269. ss$(scrollable).scrollTop(lastscrolltop+dir);
  270. if (ss$(scrollable).scrollTop()==lastscrolltop)
  271. {
  272. ss$(scrollable).scrollTop(lastscrolltop);
  273. return false;
  274. }
  275. else
  276. {
  277. ss$(scrollable).scrollTop(lastscrolltop);
  278. return true;
  279. }
  280. }
  281.  
  282. function UpdateFocus(x,y) {
  283. var dir = y;
  284. var nodelist = document.querySelectorAll( ":hover" );
  285. //ss$(nodelist).stop();
  286. //console.log(nodelist);
  287. if (WEBKIT)
  288. {
  289. scrollfocus = ss$('body');
  290. }
  291. else
  292. {
  293. scrollfocus = ss$('html');
  294. }
  295. for (var i = nodelist.length-1; i >= 0 ; i--) {
  296. //var parent = nodelist[i-1];
  297. var newfocus = nodelist[i];
  298. if (DEBUG)
  299. {
  300. ss$(newfocus).css( "border", "1px solid blue" );
  301. //var debugtimer = setTimeout(function(){ ss$(newfocus).css( "border", "0px solid white" ); }, 1000);
  302. }
  303. //if (ss$(newfocus).hasScrollBar3() && hasScrollBarVisible(newfocus) && canscroll(newfocus, dir) && hasscrollbars(newfocus))
  304. if (canscroll(newfocus, dir) && hasscrollbars(newfocus))
  305. {
  306. scrollfocus = ss$(newfocus);
  307. return newfocus;
  308. }
  309. }
  310.  
  311. return scrollfocus;
  312. }
  313. ss$('html').bind({
  314. //mousewheel: function(e) {
  315. //if (DEBUG)
  316. //{
  317. // console.log(scrollfocus);
  318. //}
  319. //console.log("scrolling");
  320. //MouseScroll(e.originalEvent);
  321. //},
  322. mousemove: function(e) {
  323. mousemoved = true;
  324. },
  325.  
  326. mousedown: function(e) {
  327. if (DEBUG)
  328. {
  329. console.log(scrollfocus);
  330. }
  331. if (scrollfocus)
  332. {
  333. ss$(scrollfocus)[0].setAttribute( "positiondelta",0 );
  334. ss$(scrollfocus).stop();
  335. }
  336. scrollfocus = UpdateFocus(0,0);
  337. //console.log("click");
  338. }
  339. });
  340. //Init(window);
  341.  
  342. document.documentElement.addEventListener("wheel", function(e){
  343. MouseScroll(e);
  344. //console.log("scrolling");
  345. });
  346. /*var oldpos = ss$('body').scrollTop();
  347. ss$('body').scrollTop(1);
  348. var iswebkit = ss$('body').scrollTop()>0;
  349. ss$('body').scrollTop(oldpos);*/
  350.  
  351. //WEBKIT = 'webkitRequestAnimationFrame' in window;
  352. WEBKIT = document.compatMode == 'CSS1Compat'
  353. //console.log("window: " + window);
  354. console.log("Smoothscroll initiated! Webkit: " + WEBKIT);
  355. }
  356.  
  357. var JQUERY = false;
  358. var OWNJQUERY = false;
  359. var OLDJQUERY = false;
  360.  
  361. var old$;
  362. var oldjQuery;
  363.  
  364. var ss$;
  365.  
  366. function jQueryVersion(temp$)
  367. {
  368. if (typeof temp$ == 'undefined' || typeof temp$.fn == 'undefined' || typeof temp$.fn.jquery == 'undefined')
  369. {
  370. return 0;
  371. }
  372.  
  373. var versiontable = temp$.fn.jquery.split('.');
  374. var version = 0;
  375. for (var i = versiontable.length-1; i >= 0; i--) {
  376. var power = versiontable.length-i;
  377. version += Math.pow(10,power-1)*versiontable[i];
  378. }
  379. return version;
  380. }
  381.  
  382.  
  383. function LoadConfig()
  384. {
  385. smoothness = GM_getValue( 'smoothness', smoothness );
  386. sensitivity = GM_getValue( 'sensitivity', sensitivity );
  387. acceleration = GM_getValue( 'acceleration', acceleration );
  388. baserefreshrate = GM_getValue( 'baserefreshrate', baserefreshrate );
  389. //alternative_sesitivity_multiplier = GM_getValue( 'alternative_sesitivity_multiplier', alternative_sesitivity_multiplier );
  390. console.log("Config for smoothscroll loaded!")
  391. }
  392.  
  393. function SaveConfig()
  394. {
  395. GM_setValue( 'smoothness', document.getElementById('ss-smoothness').value)
  396. GM_setValue( 'sensitivity', document.getElementById('ss-sensitivity').value)
  397. GM_setValue( 'acceleration', document.getElementById('ss-acceleration').value)
  398. GM_setValue( 'baserefreshrate', document.getElementById('ss-baserefreshrate').value)
  399. //console.log(document.getElementById('ss-alternative_sesitivity_multiplier').checked)
  400. console.log("Config for smoothscroll saved!")
  401. }
  402.  
  403. function InitConfigmenu()
  404. {
  405. console.log("Initiating smoothscroll config...");
  406. var configbar = document.createElement('div');
  407. configbar.setAttribute("id","ss-configbar");
  408. 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>';
  409. var configparent = document.getElementsByTagName("body")[0];
  410. configbar.style.width = '100%';
  411. configbar.style.display = 'none';
  412. configbar.style.position = 'absolute';
  413. configbar.style.zIndex = '9999';
  414. configbar.style.backgroundColor = 'white';
  415. configparent.insertBefore(configbar, configparent.childNodes[0]);
  416. ss$("#ss-close").click(function() {
  417. //ss$("#ss-config").animate({
  418. // height: '0'
  419. //}, 100);
  420. ss$("#ss-configbar").hide("slow");
  421. });
  422. ss$("#ss-save").click(function() {
  423. SaveConfig();
  424. });
  425. //ss$("#ss-configbar").hide();
  426. }
  427.  
  428. function ConfigSmoothscroll()
  429. {
  430. if (typeof ss$ == 'undefined'){
  431. alert("Smoothscroll is not running properly on this page!");
  432. return;
  433. }
  434. //ss$("#ss-config").animate({
  435. // height: '100px'
  436. //}, 100);
  437. ss$("#ss-configbar").show("slow");
  438. ss$("html, body").animate({ scrollTop: 0 }, "slow");
  439. }
  440.  
  441. function LoadjQuery(loading)
  442. {
  443. if (loading)
  444. {
  445. console.log("Waiting for jQuery...");
  446. if (typeof $ == 'undefined' || typeof jQuery == 'undefined' || jQueryVersion($)<minimal_jquery_version)
  447. {
  448. if (jquery_retry_count<jquery_retry_max)
  449. {
  450. jquery_retry_count++;
  451. setTimeout(function() {
  452. LoadjQuery(true);
  453. }, 100);
  454. }
  455. else
  456. {
  457. console.log("Failed to load smoothscroll!");
  458. if (typeof old$ != 'undefined') {
  459. $ = old$;
  460. }
  461. if (typeof oldjQuery != 'undefined') {
  462. jQuery = oldjQuery;
  463. }
  464. }
  465. }
  466. else
  467. {
  468. ss$ = $;
  469. ssjQuery = jQuery;
  470. console.log("jQuery loaded!");
  471. if (typeof old$ != 'undefined') {
  472. $ = old$;
  473. }
  474. if (typeof oldjQuery != 'undefined') {
  475. jQuery = oldjQuery;
  476. }
  477. console.log("Page jQuery version: " + jQueryVersion(old$));
  478. console.log("Script jQuery version: " + jQueryVersion(ss$));
  479. InitSmoothscroll();
  480. }
  481. }
  482. else
  483. {
  484. console.log("Loading own jQuery...");
  485. jquery_retry_count = 0;
  486. var filename = "https://code.jquery.com/jquery-2.2.3.js";
  487. var fileref = document.createElement('script');
  488. fileref.setAttribute("type","text/javascript");
  489. fileref.setAttribute("src", filename);
  490.  
  491. if (typeof fileref!="undefined")
  492. {
  493. var scriptparent = document.getElementsByTagName("head")[0];
  494.  
  495. if (typeof scriptparent=="undefined")
  496. {
  497. var scriptparent = document.createElement('head');
  498. document.getElementsByTagName("html")[0].appendChild(scriptparent);
  499. }
  500. scriptparent.appendChild(fileref);
  501. }
  502. LoadjQuery(true);
  503. }
  504. }
  505.  
  506.  
  507. function Init()
  508. {
  509.  
  510. if (typeof ss$ == 'undefined' )
  511. {
  512. var sitejquery = !(typeof $ == 'undefined' || typeof jQuery == 'undefined');
  513. if (sitejquery && jQueryVersion($)>=minimal_jquery_version)
  514. {
  515. ss$ = $;
  516. ssjQuery = jQuery;
  517. console.log("Reusing page jQuery...");
  518. console.log("Page jQuery version: " + jQueryVersion($));
  519. InitSmoothscroll();
  520. }
  521. else
  522. {
  523. if (typeof $ != 'undefined' && typeof old$ == 'undefined')
  524. {
  525. old$ = $;
  526. }
  527. if (typeof jQuery != 'undefined' && typeof oldjQuery == 'undefined')
  528. {
  529. oldjQuery = jQuery;
  530. }
  531. LoadjQuery(false);
  532. }
  533. }
  534. }
  535.  
  536. if (typeof ss$ == 'undefined'){
  537. console.log("Initiating smoothscroll...");
  538. Init();
  539. //InitSmoothscroll();
  540. GM_registerMenuCommand("Configurate smoothscroll", ConfigSmoothscroll);
  541. }
  542. else
  543. {
  544. console.log("Smoothscroll already loaded!");
  545. }