jquery.scrollify.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*!
  2. * jQuery Scrollify
  3. * Version 1.0.19
  4. *
  5. * Requires:
  6. * - jQuery 1.7 or higher
  7. *
  8. * https://github.com/lukehaas/Scrollify
  9. *
  10. * Copyright 2016, Luke Haas
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  12. * this software and associated documentation files (the "Software"), to deal in
  13. * the Software without restriction, including without limitation the rights to
  14. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  15. * the Software, and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  23. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  25. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. if touchScroll is false - update index
  28. */
  29. (function (global,factory) {
  30. "use strict";
  31. if (typeof define === 'function' && define.amd) {
  32. // AMD. Register as an anonymous module.
  33. define(['jquery'], function($) {
  34. return factory($, global, global.document);
  35. });
  36. } else if (typeof module === 'object' && module.exports) {
  37. // Node/CommonJS
  38. module.exports = factory(require('jquery'), global, global.document);
  39. } else {
  40. // Browser globals
  41. factory(jQuery, global, global.document);
  42. }
  43. }(typeof window !== 'undefined' ? window : this, function ($, window, document, undefined) {
  44. "use strict";
  45. var heights = [],
  46. names = [],
  47. elements = [],
  48. overflow = [],
  49. index = 0,
  50. currentIndex = 0,
  51. interstitialIndex = 1,
  52. hasLocation = false,
  53. timeoutId,
  54. timeoutId2,
  55. $window = $(window),
  56. portHeight,
  57. top = $window.scrollTop(),
  58. scrollable = false,
  59. locked = false,
  60. scrolled = false,
  61. manualScroll,
  62. swipeScroll,
  63. util,
  64. disabled = false,
  65. scrollSamples = [],
  66. scrollTime = new Date().getTime(),
  67. firstLoad = true,
  68. initialised = false,
  69. destination = 0,
  70. wheelEvent = 'onwheel' in document ? 'wheel' : document.onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll',
  71. settings = {
  72. //section should be an identifier that is the same for each section
  73. section: ".section",
  74. sectionName: "section-name",
  75. interstitialSection: "",
  76. easing: "easeOutExpo",
  77. scrollSpeed: 1100,
  78. offset: 0,
  79. scrollbars: true,
  80. target:"html,body",
  81. standardScrollElements: false,
  82. setHeights: true,
  83. overflowScroll:true,
  84. updateHash: true,
  85. touchScroll:true,
  86. before:function() {},
  87. after:function() {},
  88. afterResize:function() {},
  89. afterRender:function() {}
  90. };
  91. function getportHeight() {
  92. return ($window.height() + settings.offset);
  93. }
  94. function animateScroll(index,instant,callbacks,toTop) {
  95. if(currentIndex===index) {
  96. callbacks = false;
  97. }
  98. if(disabled===true) {
  99. return true;
  100. }
  101. if(names[index]) {
  102. scrollable = false;
  103. if(firstLoad===true) {
  104. settings.afterRender();
  105. firstLoad = false;
  106. }
  107. if(callbacks) {
  108. if( typeof settings.before == 'function' && settings.before(index,elements) === false ){
  109. return true;
  110. }
  111. }
  112. interstitialIndex = 1;
  113. destination = heights[index];
  114. if(firstLoad===false && currentIndex>index && toTop===false) {
  115. //We're going backwards
  116. if(overflow[index]) {
  117. portHeight = getportHeight();
  118. interstitialIndex = parseInt(elements[index].outerHeight()/portHeight);
  119. destination = parseInt(heights[index])+(elements[index].outerHeight()-portHeight);
  120. }
  121. }
  122. if(settings.updateHash && settings.sectionName && !(firstLoad===true && index===0)) {
  123. if(history.pushState) {
  124. try {
  125. history.replaceState(null, null, names[index]);
  126. } catch (e) {
  127. if(window.console) {
  128. console.warn("Scrollify warning: Page must be hosted to manipulate the hash value.");
  129. }
  130. }
  131. } else {
  132. window.location.hash = names[index];
  133. }
  134. }
  135. currentIndex = index;
  136. if(instant) {
  137. $(settings.target).stop().scrollTop(destination);
  138. if(callbacks) {
  139. settings.after(index,elements);
  140. }
  141. } else {
  142. locked = true;
  143. if( $().velocity ) {
  144. $(settings.target).stop().velocity('scroll', {
  145. duration: settings.scrollSpeed,
  146. easing: settings.easing,
  147. offset: destination,
  148. mobileHA: false
  149. });
  150. } else {
  151. $(settings.target).stop().animate({
  152. scrollTop: destination
  153. }, settings.scrollSpeed,settings.easing);
  154. }
  155. if(window.location.hash.length && settings.sectionName && window.console) {
  156. try {
  157. if($(window.location.hash).length) {
  158. console.warn("Scrollify warning: ID matches hash value - this will cause the page to anchor.");
  159. }
  160. } catch (e) {}
  161. }
  162. $(settings.target).promise().done(function(){
  163. locked = false;
  164. firstLoad = false;
  165. if(callbacks) {
  166. settings.after(index,elements);
  167. }
  168. });
  169. }
  170. }
  171. }
  172. function isAccelerating(samples) {
  173. function average(num) {
  174. var sum = 0;
  175. var lastElements = samples.slice(Math.max(samples.length - num, 1));
  176. for(var i = 0; i < lastElements.length; i++){
  177. sum += lastElements[i];
  178. }
  179. return Math.ceil(sum/num);
  180. }
  181. var avEnd = average(10);
  182. var avMiddle = average(70);
  183. if(avEnd >= avMiddle) {
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. var scrollify = function(options) {
  190. initialised = true;
  191. $.easing['easeOutExpo'] = function(x, t, b, c, d) {
  192. return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  193. };
  194. manualScroll = {
  195. handleMousedown:function() {
  196. if(disabled===true) {
  197. return true;
  198. }
  199. scrollable = false;
  200. scrolled = false;
  201. },
  202. handleMouseup:function() {
  203. if(disabled===true) {
  204. return true;
  205. }
  206. scrollable = true;
  207. if(scrolled) {
  208. //instant,callbacks
  209. manualScroll.calculateNearest(false,true);
  210. }
  211. },
  212. handleScroll:function() {
  213. if(disabled===true) {
  214. return true;
  215. }
  216. if(timeoutId){
  217. clearTimeout(timeoutId);
  218. }
  219. timeoutId = setTimeout(function(){
  220. scrolled = true;
  221. if(scrollable===false) {
  222. return false;
  223. }
  224. scrollable = false;
  225. //instant,callbacks
  226. manualScroll.calculateNearest(false,true);
  227. }, 200);
  228. },
  229. calculateNearest:function(instant,callbacks) {
  230. top = $window.scrollTop();
  231. var i =1,
  232. max = heights.length,
  233. closest = 0,
  234. prev = Math.abs(heights[0] - top),
  235. diff;
  236. for(;i<max;i++) {
  237. diff = Math.abs(heights[i] - top);
  238. if(diff < prev) {
  239. prev = diff;
  240. closest = i;
  241. }
  242. }
  243. if((atBottom() && closest>index) || atTop()) {
  244. index = closest;
  245. //index, instant, callbacks, toTop
  246. animateScroll(closest,instant,callbacks,false);
  247. }
  248. },
  249. wheelHandler:function(e) {
  250. if(disabled===true) {
  251. return true;
  252. } else if(settings.standardScrollElements) {
  253. if($(e.target).is(settings.standardScrollElements) || $(e.target).closest(settings.standardScrollElements).length) {
  254. return true;
  255. }
  256. }
  257. if(!overflow[index]) {
  258. e.preventDefault();
  259. }
  260. var currentScrollTime = new Date().getTime();
  261. e = e || window.event;
  262. var value = e.originalEvent.wheelDelta || -e.originalEvent.deltaY || -e.originalEvent.detail;
  263. var delta = Math.max(-1, Math.min(1, value));
  264. //delta = delta || -e.originalEvent.detail / 3 || e.originalEvent.wheelDelta / 120;
  265. if(scrollSamples.length > 149){
  266. scrollSamples.shift();
  267. }
  268. //scrollSamples.push(Math.abs(delta*10));
  269. scrollSamples.push(Math.abs(value));
  270. if((currentScrollTime-scrollTime) > 200){
  271. scrollSamples = [];
  272. }
  273. scrollTime = currentScrollTime;
  274. if(locked) {
  275. return false;
  276. }
  277. if(delta<0) {
  278. if(index<heights.length-1) {
  279. if(atBottom()) {
  280. if(isAccelerating(scrollSamples)) {
  281. e.preventDefault();
  282. index++;
  283. locked = true;
  284. //index, instant, callbacks, toTop
  285. animateScroll(index,false,true, false);
  286. } else {
  287. return false;
  288. }
  289. }
  290. }
  291. } else if(delta>0) {
  292. if(index>0) {
  293. if(atTop()) {
  294. if(isAccelerating(scrollSamples)) {
  295. e.preventDefault();
  296. index--;
  297. locked = true;
  298. //index, instant, callbacks, toTop
  299. animateScroll(index,false,true, false);
  300. } else {
  301. return false
  302. }
  303. }
  304. }
  305. }
  306. },
  307. keyHandler:function(e) {
  308. if(disabled===true || document.activeElement.readOnly===false) {
  309. return true;
  310. } else if(settings.standardScrollElements) {
  311. if($(e.target).is(settings.standardScrollElements) || $(e.target).closest(settings.standardScrollElements).length) {
  312. return true;
  313. }
  314. }
  315. if(locked===true) {
  316. return false;
  317. }
  318. if(e.keyCode==38 || e.keyCode==33) {
  319. if(index>0) {
  320. if(atTop()) {
  321. e.preventDefault();
  322. index--;
  323. //index, instant, callbacks, toTop
  324. animateScroll(index,false,true,false);
  325. }
  326. }
  327. } else if(e.keyCode==40 || e.keyCode==34) {
  328. if(index<heights.length-1) {
  329. if(atBottom()) {
  330. e.preventDefault();
  331. index++;
  332. //index, instant, callbacks, toTop
  333. animateScroll(index,false,true,false);
  334. }
  335. }
  336. }
  337. },
  338. init:function() {
  339. if(settings.scrollbars) {
  340. $window.on('mousedown', manualScroll.handleMousedown);
  341. $window.on('mouseup', manualScroll.handleMouseup);
  342. $window.on('scroll', manualScroll.handleScroll);
  343. } else {
  344. $("body").css({"overflow":"hidden"});
  345. }
  346. $window.on(wheelEvent,manualScroll.wheelHandler);
  347. //$(document).bind(wheelEvent,manualScroll.wheelHandler);
  348. $window.on('keydown', manualScroll.keyHandler);
  349. }
  350. };
  351. swipeScroll = {
  352. touches : {
  353. "touchstart": {"y":-1,"x":-1},
  354. "touchmove" : {"y":-1,"x":-1},
  355. "touchend" : false,
  356. "direction" : "undetermined"
  357. },
  358. options:{
  359. "distance" : 30,
  360. "timeGap" : 800,
  361. "timeStamp" : new Date().getTime()
  362. },
  363. touchHandler: function(event) {
  364. if(disabled===true) {
  365. return true;
  366. } else if(settings.standardScrollElements) {
  367. if($(event.target).is(settings.standardScrollElements) || $(event.target).closest(settings.standardScrollElements).length) {
  368. return true;
  369. }
  370. }
  371. var touch;
  372. if (typeof event !== 'undefined'){
  373. if (typeof event.touches !== 'undefined') {
  374. touch = event.touches[0];
  375. switch (event.type) {
  376. case 'touchstart':
  377. swipeScroll.touches.touchstart.y = touch.pageY;
  378. swipeScroll.touches.touchmove.y = -1;
  379. swipeScroll.touches.touchstart.x = touch.pageX;
  380. swipeScroll.touches.touchmove.x = -1;
  381. swipeScroll.options.timeStamp = new Date().getTime();
  382. swipeScroll.touches.touchend = false;
  383. case 'touchmove':
  384. swipeScroll.touches.touchmove.y = touch.pageY;
  385. swipeScroll.touches.touchmove.x = touch.pageX;
  386. if(swipeScroll.touches.touchstart.y!==swipeScroll.touches.touchmove.y && (Math.abs(swipeScroll.touches.touchstart.y-swipeScroll.touches.touchmove.y)>Math.abs(swipeScroll.touches.touchstart.x-swipeScroll.touches.touchmove.x))) {
  387. //if(!overflow[index]) {
  388. event.preventDefault();
  389. //}
  390. swipeScroll.touches.direction = "y";
  391. if((swipeScroll.options.timeStamp+swipeScroll.options.timeGap)<(new Date().getTime()) && swipeScroll.touches.touchend == false) {
  392. swipeScroll.touches.touchend = true;
  393. if (swipeScroll.touches.touchstart.y > -1) {
  394. if(Math.abs(swipeScroll.touches.touchmove.y-swipeScroll.touches.touchstart.y)>swipeScroll.options.distance) {
  395. if(swipeScroll.touches.touchstart.y < swipeScroll.touches.touchmove.y) {
  396. swipeScroll.up();
  397. } else {
  398. swipeScroll.down();
  399. }
  400. }
  401. }
  402. }
  403. }
  404. break;
  405. case 'touchend':
  406. if(swipeScroll.touches[event.type]===false) {
  407. swipeScroll.touches[event.type] = true;
  408. if (swipeScroll.touches.touchstart.y > -1 && swipeScroll.touches.touchmove.y > -1 && swipeScroll.touches.direction==="y") {
  409. if(Math.abs(swipeScroll.touches.touchmove.y-swipeScroll.touches.touchstart.y)>swipeScroll.options.distance) {
  410. if(swipeScroll.touches.touchstart.y < swipeScroll.touches.touchmove.y) {
  411. swipeScroll.up();
  412. } else {
  413. swipeScroll.down();
  414. }
  415. }
  416. swipeScroll.touches.touchstart.y = -1;
  417. swipeScroll.touches.touchstart.x = -1;
  418. swipeScroll.touches.direction = "undetermined";
  419. }
  420. }
  421. default:
  422. break;
  423. }
  424. }
  425. }
  426. },
  427. down: function() {
  428. if(index<heights.length) {
  429. if(atBottom() && index<heights.length-1) {
  430. index++;
  431. //index, instant, callbacks, toTop
  432. animateScroll(index,false,true,false);
  433. } else {
  434. portHeight = getportHeight();
  435. if(Math.floor(elements[index].height()/portHeight)>interstitialIndex) {
  436. interstitialScroll(parseInt(heights[index])+(portHeight*interstitialIndex));
  437. interstitialIndex += 1;
  438. } else {
  439. interstitialScroll(parseInt(heights[index])+(elements[index].outerHeight()-portHeight));
  440. }
  441. }
  442. }
  443. },
  444. up: function() {
  445. if(index>=0) {
  446. if(atTop() && index>0) {
  447. index--;
  448. //index, instant, callbacks, toTop
  449. animateScroll(index,false,true,false);
  450. } else {
  451. if(interstitialIndex>2) {
  452. portHeight = getportHeight();
  453. interstitialIndex -= 1;
  454. interstitialScroll(parseInt(heights[index])+(portHeight*interstitialIndex));
  455. } else {
  456. interstitialIndex = 1;
  457. interstitialScroll(parseInt(heights[index]));
  458. }
  459. }
  460. }
  461. },
  462. init: function() {
  463. if (document.addEventListener && settings.touchScroll) {
  464. var eventListenerOptions = {
  465. passive: false
  466. };
  467. document.addEventListener('touchstart', swipeScroll.touchHandler, eventListenerOptions);
  468. document.addEventListener('touchmove', swipeScroll.touchHandler, eventListenerOptions);
  469. document.addEventListener('touchend', swipeScroll.touchHandler, eventListenerOptions);
  470. }
  471. }
  472. };
  473. util = {
  474. refresh:function(withCallback,scroll) {
  475. clearTimeout(timeoutId2);
  476. timeoutId2 = setTimeout(function() {
  477. //retain position
  478. sizePanels(true);
  479. //scroll, firstLoad
  480. calculatePositions(scroll,false);
  481. if(withCallback) {
  482. settings.afterResize();
  483. }
  484. },400);
  485. },
  486. handleUpdate:function() {
  487. //callbacks, scroll
  488. //changed from false,true to false,false
  489. util.refresh(false,false);
  490. },
  491. handleResize:function() {
  492. //callbacks, scroll
  493. util.refresh(true,false);
  494. },
  495. handleOrientation:function() {
  496. //callbacks, scroll
  497. util.refresh(true,true);
  498. }
  499. };
  500. settings = $.extend(settings, options);
  501. //retain position
  502. sizePanels(false);
  503. calculatePositions(false,true);
  504. if(true===hasLocation) {
  505. //index, instant, callbacks, toTop
  506. animateScroll(index,false,true,true);
  507. } else {
  508. setTimeout(function() {
  509. //instant,callbacks
  510. manualScroll.calculateNearest(true,false);
  511. },200);
  512. }
  513. if(heights.length) {
  514. manualScroll.init();
  515. swipeScroll.init();
  516. $window.on("resize",util.handleResize);
  517. if (document.addEventListener) {
  518. window.addEventListener("orientationchange", util.handleOrientation, false);
  519. }
  520. }
  521. function interstitialScroll(pos) {
  522. if( $().velocity ) {
  523. $(settings.target).stop().velocity('scroll', {
  524. duration: settings.scrollSpeed,
  525. easing: settings.easing,
  526. offset: pos,
  527. mobileHA: false
  528. });
  529. } else {
  530. $(settings.target).stop().animate({
  531. scrollTop: pos
  532. }, settings.scrollSpeed,settings.easing);
  533. }
  534. }
  535. function sizePanels(keepPosition) {
  536. if(keepPosition) {
  537. top = $window.scrollTop();
  538. }
  539. var selector = settings.section;
  540. overflow = [];
  541. if(settings.interstitialSection.length) {
  542. selector += "," + settings.interstitialSection;
  543. }
  544. if(settings.scrollbars===false) {
  545. settings.overflowScroll = false;
  546. }
  547. portHeight = getportHeight();
  548. $(selector).each(function(i) {
  549. var $this = $(this);
  550. if(settings.setHeights) {
  551. if($this.is(settings.interstitialSection)) {
  552. overflow[i] = false;
  553. } else {
  554. if(($this.css("height","auto").outerHeight()<portHeight) || $this.css("overflow")==="hidden") {
  555. $this.css({"height":portHeight});
  556. overflow[i] = false;
  557. } else {
  558. $this.css({"height":$this.height()});
  559. if(settings.overflowScroll) {
  560. overflow[i] = true;
  561. } else {
  562. overflow[i] = false;
  563. }
  564. }
  565. }
  566. } else {
  567. if(($this.outerHeight()<portHeight) || (settings.overflowScroll===false)) {
  568. overflow[i] = false;
  569. } else {
  570. overflow[i] = true;
  571. }
  572. }
  573. });
  574. if(keepPosition) {
  575. $window.scrollTop(top);
  576. }
  577. }
  578. function calculatePositions(scroll,firstLoad) {
  579. var selector = settings.section;
  580. if(settings.interstitialSection.length) {
  581. selector += "," + settings.interstitialSection;
  582. }
  583. heights = [];
  584. names = [];
  585. elements = [];
  586. $(selector).each(function(i){
  587. var $this = $(this);
  588. if(i>0) {
  589. heights[i] = parseInt($this.offset().top) + settings.offset;
  590. } else {
  591. heights[i] = parseInt($this.offset().top);
  592. }
  593. if(settings.sectionName && $this.data(settings.sectionName)) {
  594. names[i] = "#" + $this.data(settings.sectionName).toString().replace(/ /g,"-");
  595. } else {
  596. if($this.is(settings.interstitialSection)===false) {
  597. names[i] = "#" + (i + 1);
  598. } else {
  599. names[i] = "#";
  600. if(i===$(selector).length-1 && i>1) {
  601. heights[i] = heights[i-1] + (parseInt($($(selector)[i-1]).outerHeight()) - parseInt($(window).height())) + parseInt($this.outerHeight());
  602. }
  603. }
  604. }
  605. elements[i] = $this;
  606. try {
  607. if($(names[i]).length && window.console) {
  608. console.warn("Scrollify warning: Section names can't match IDs - this will cause the browser to anchor.");
  609. }
  610. } catch (e) {}
  611. if(window.location.hash===names[i]) {
  612. index = i;
  613. hasLocation = true;
  614. }
  615. });
  616. if(true===scroll) {
  617. //index, instant, callbacks, toTop
  618. animateScroll(index,false,false,false);
  619. }
  620. }
  621. function atTop() {
  622. if(!overflow[index]) {
  623. return true;
  624. }
  625. top = $window.scrollTop();
  626. if(top>parseInt(heights[index])) {
  627. return false;
  628. } else {
  629. return true;
  630. }
  631. }
  632. function atBottom() {
  633. if(!overflow[index]) {
  634. return true;
  635. }
  636. top = $window.scrollTop();
  637. portHeight = getportHeight();
  638. if(top<parseInt(heights[index])+(elements[index].outerHeight()-portHeight)-28) {
  639. return false;
  640. } else {
  641. return true;
  642. }
  643. }
  644. }
  645. function move(panel,instant) {
  646. var z = names.length;
  647. for(;z>=0;z--) {
  648. if(typeof panel === 'string') {
  649. if (names[z]===panel) {
  650. index = z;
  651. //index, instant, callbacks, toTop
  652. animateScroll(z,instant,true,true);
  653. }
  654. } else {
  655. if(z===panel) {
  656. index = z;
  657. //index, instant, callbacks, toTop
  658. animateScroll(z,instant,true,true);
  659. }
  660. }
  661. }
  662. }
  663. scrollify.move = function(panel) {
  664. if(panel===undefined) {
  665. return false;
  666. }
  667. if(panel.originalEvent) {
  668. panel = $(this).attr("href");
  669. }
  670. move(panel,false);
  671. };
  672. scrollify.instantMove = function(panel) {
  673. if(panel===undefined) {
  674. return false;
  675. }
  676. move(panel,true);
  677. };
  678. scrollify.next = function() {
  679. if(index<names.length) {
  680. index += 1;
  681. //index, instant, callbacks, toTop
  682. animateScroll(index,false,true,true);
  683. }
  684. };
  685. scrollify.previous = function() {
  686. if(index>0) {
  687. index -= 1;
  688. //index, instant, callbacks, toTop
  689. animateScroll(index,false,true,true);
  690. }
  691. };
  692. scrollify.instantNext = function() {
  693. if(index<names.length) {
  694. index += 1;
  695. //index, instant, callbacks, toTop
  696. animateScroll(index,true,true,true);
  697. }
  698. };
  699. scrollify.instantPrevious = function() {
  700. if(index>0) {
  701. index -= 1;
  702. //index, instant, callbacks, toTop
  703. animateScroll(index,true,true,true);
  704. }
  705. };
  706. scrollify.destroy = function() {
  707. if(!initialised) {
  708. return false;
  709. }
  710. if(settings.setHeights) {
  711. $(settings.section).each(function() {
  712. $(this).css("height","auto");
  713. });
  714. }
  715. $window.off("resize",util.handleResize);
  716. if(settings.scrollbars) {
  717. $window.off('mousedown', manualScroll.handleMousedown);
  718. $window.off('mouseup', manualScroll.handleMouseup);
  719. $window.off('scroll', manualScroll.handleScroll);
  720. }
  721. $window.off(wheelEvent,manualScroll.wheelHandler);
  722. $window.off('keydown', manualScroll.keyHandler);
  723. if (document.addEventListener && settings.touchScroll) {
  724. document.removeEventListener('touchstart', swipeScroll.touchHandler, false);
  725. document.removeEventListener('touchmove', swipeScroll.touchHandler, false);
  726. document.removeEventListener('touchend', swipeScroll.touchHandler, false);
  727. }
  728. heights = [];
  729. names = [];
  730. elements = [];
  731. overflow = [];
  732. };
  733. scrollify.update = function() {
  734. if(!initialised) {
  735. return false;
  736. }
  737. util.handleUpdate();
  738. };
  739. scrollify.current = function() {
  740. return elements[index];
  741. };
  742. scrollify.currentIndex = function() {
  743. return index;
  744. };
  745. scrollify.disable = function() {
  746. disabled = true;
  747. };
  748. scrollify.enable = function() {
  749. disabled = false;
  750. if (initialised) {
  751. //instant,callbacks
  752. manualScroll.calculateNearest(false,false);
  753. }
  754. };
  755. scrollify.isDisabled = function() {
  756. return disabled;
  757. };
  758. scrollify.setOptions = function(updatedOptions) {
  759. if(!initialised) {
  760. return false;
  761. }
  762. if(typeof updatedOptions === "object") {
  763. settings = $.extend(settings, updatedOptions);
  764. util.handleUpdate();
  765. } else if(window.console) {
  766. console.warn("Scrollify warning: setOptions expects an object.");
  767. }
  768. };
  769. $.scrollify = scrollify;
  770. return scrollify;
  771. }));