jquery.fullPage.js 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079
  1. /*!
  2. * fullPage 2.9.2
  3. * https://github.com/alvarotrigo/fullPage.js
  4. * @license MIT licensed
  5. *
  6. * Copyright (C) 2015 alvarotrigo.com - A project by Alvaro Trigo
  7. */
  8. (function(global, factory) {
  9. 'use strict';
  10. if (typeof define === 'function' && define.amd) {
  11. define(['jquery'], function($) {
  12. return factory($, global, global.document, global.Math);
  13. });
  14. } else if (typeof exports === "object" && exports) {
  15. module.exports = factory(require('jquery'), global, global.document, global.Math);
  16. } else {
  17. factory(jQuery, global, global.document, global.Math);
  18. }
  19. })(typeof window !== 'undefined' ? window : this, function($, window, document, Math, undefined) {
  20. 'use strict';
  21. // keeping central set of classnames and selectors
  22. var WRAPPER = 'fullpage-wrapper';
  23. var WRAPPER_SEL = '.' + WRAPPER;
  24. // slimscroll
  25. var SCROLLABLE = 'fp-scrollable';
  26. var SCROLLABLE_SEL = '.' + SCROLLABLE;
  27. // util
  28. var RESPONSIVE = 'fp-responsive';
  29. var NO_TRANSITION = 'fp-notransition';
  30. var DESTROYED = 'fp-destroyed';
  31. var ENABLED = 'fp-enabled';
  32. var VIEWING_PREFIX = 'fp-viewing';
  33. var ACTIVE = 'active';
  34. var ACTIVE_SEL = '.' + ACTIVE;
  35. var COMPLETELY = 'fp-completely';
  36. var COMPLETELY_SEL = '.' + COMPLETELY;
  37. // section
  38. var SECTION_DEFAULT_SEL = '.section';
  39. var SECTION = 'fp-section';
  40. var SECTION_SEL = '.' + SECTION;
  41. var SECTION_ACTIVE_SEL = SECTION_SEL + ACTIVE_SEL;
  42. var SECTION_FIRST_SEL = SECTION_SEL + ':first';
  43. var SECTION_LAST_SEL = SECTION_SEL + ':last';
  44. var TABLE_CELL = 'fp-tableCell';
  45. var TABLE_CELL_SEL = '.' + TABLE_CELL;
  46. var AUTO_HEIGHT = 'fp-auto-height';
  47. var AUTO_HEIGHT_SEL = '.fp-auto-height';
  48. var NORMAL_SCROLL = 'fp-normal-scroll';
  49. var NORMAL_SCROLL_SEL = '.fp-normal-scroll';
  50. // section nav
  51. var SECTION_NAV = 'fp-nav';
  52. var SECTION_NAV_SEL = '#' + SECTION_NAV;
  53. var SECTION_NAV_TOOLTIP = 'fp-tooltip';
  54. var SECTION_NAV_TOOLTIP_SEL='.'+SECTION_NAV_TOOLTIP;
  55. var SHOW_ACTIVE_TOOLTIP = 'fp-show-active';
  56. // slide
  57. var SLIDE_DEFAULT_SEL = '.slide';
  58. var SLIDE = 'fp-slide';
  59. var SLIDE_SEL = '.' + SLIDE;
  60. var SLIDE_ACTIVE_SEL = SLIDE_SEL + ACTIVE_SEL;
  61. var SLIDES_WRAPPER = 'fp-slides';
  62. var SLIDES_WRAPPER_SEL = '.' + SLIDES_WRAPPER;
  63. var SLIDES_CONTAINER = 'fp-slidesContainer';
  64. var SLIDES_CONTAINER_SEL = '.' + SLIDES_CONTAINER;
  65. var TABLE = 'fp-table';
  66. // slide nav
  67. var SLIDES_NAV = 'fp-slidesNav';
  68. var SLIDES_NAV_SEL = '.' + SLIDES_NAV;
  69. var SLIDES_NAV_LINK_SEL = SLIDES_NAV_SEL + ' a';
  70. var SLIDES_ARROW = 'fp-controlArrow';
  71. var SLIDES_ARROW_SEL = '.' + SLIDES_ARROW;
  72. var SLIDES_PREV = 'fp-prev';
  73. var SLIDES_PREV_SEL = '.' + SLIDES_PREV;
  74. var SLIDES_ARROW_PREV = SLIDES_ARROW + ' ' + SLIDES_PREV;
  75. var SLIDES_ARROW_PREV_SEL = SLIDES_ARROW_SEL + SLIDES_PREV_SEL;
  76. var SLIDES_NEXT = 'fp-next';
  77. var SLIDES_NEXT_SEL = '.' + SLIDES_NEXT;
  78. var SLIDES_ARROW_NEXT = SLIDES_ARROW + ' ' + SLIDES_NEXT;
  79. var SLIDES_ARROW_NEXT_SEL = SLIDES_ARROW_SEL + SLIDES_NEXT_SEL;
  80. var $window = $(window);
  81. var $document = $(document);
  82. // Default options for iScroll.js used when using scrollOverflow
  83. var iscrollOptions = {
  84. scrollbars: true,
  85. mouseWheel: true,
  86. hideScrollbars: false,
  87. fadeScrollbars: false,
  88. disableMouse: true,
  89. interactiveScrollbars: true
  90. };
  91. $.fn.fullpage = function(options) {
  92. //only once my friend!
  93. if($('html').hasClass(ENABLED)){ displayWarnings(); return; }
  94. // common jQuery objects
  95. var $htmlBody = $('html, body');
  96. var $body = $('body');
  97. var FP = $.fn.fullpage;
  98. // Create some defaults, extending them with any options that were provided
  99. options = $.extend({
  100. //navigation
  101. menu: false,
  102. anchors:[],
  103. lockAnchors: false,
  104. navigation: false,
  105. navigationPosition: 'right',
  106. navigationTooltips: [],
  107. showActiveTooltip: false,
  108. slidesNavigation: false,
  109. slidesNavPosition: 'bottom',
  110. scrollBar: false,
  111. hybrid: false,
  112. //scrolling
  113. css3: true,
  114. scrollingSpeed: 700,
  115. autoScrolling: true,
  116. fitToSection: true,
  117. fitToSectionDelay: 1000,
  118. easing: 'easeInOutCubic',
  119. easingcss3: 'ease',
  120. loopBottom: false,
  121. loopTop: false,
  122. loopHorizontal: true,
  123. continuousVertical: false,
  124. continuousHorizontal: false,
  125. scrollHorizontally: false,
  126. interlockedSlides: false,
  127. dragAndMove: false,
  128. offsetSections: false,
  129. resetSliders: false,
  130. fadingEffect: false,
  131. normalScrollElements: null,
  132. scrollOverflow: false,
  133. scrollOverflowReset: false,
  134. scrollOverflowHandler: iscrollHandler,
  135. scrollOverflowOptions: null,
  136. touchSensitivity: 5,
  137. normalScrollElementTouchThreshold: 5,
  138. bigSectionsDestination: null,
  139. //Accessibility
  140. keyboardScrolling: true,
  141. animateAnchor: true,
  142. recordHistory: true,
  143. //design
  144. controlArrows: true,
  145. controlArrowColor: '#fff',
  146. verticalCentered: true,
  147. sectionsColor : [],
  148. paddingTop: 0,
  149. paddingBottom: 0,
  150. fixedElements: null,
  151. responsive: 0, //backwards compabitility with responsiveWiddth
  152. responsiveWidth: 0,
  153. responsiveHeight: 0,
  154. responsiveSlides: false,
  155. //Custom selectors
  156. sectionSelector: SECTION_DEFAULT_SEL,
  157. slideSelector: SLIDE_DEFAULT_SEL,
  158. //events
  159. afterLoad: null,
  160. onLeave: null,
  161. afterRender: null,
  162. afterResize: null,
  163. afterReBuild: null,
  164. afterSlideLoad: null,
  165. onSlideLeave: null,
  166. afterResponsive: null,
  167. lazyLoading: true
  168. }, options);
  169. //flag to avoid very fast sliding for landscape sliders
  170. var slideMoving = false;
  171. var isTouchDevice = navigator.userAgent.match(/(iPhone|iPod|iPad|Android|playbook|silk|BlackBerry|BB10|Windows Phone|Tizen|Bada|webOS|IEMobile|Opera Mini)/);
  172. var isTouch = (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0) || (navigator.maxTouchPoints));
  173. var container = $(this);
  174. var windowsHeight = $window.height();
  175. var isResizing = false;
  176. var isWindowFocused = true;
  177. var lastScrolledDestiny;
  178. var lastScrolledSlide;
  179. var canScroll = true;
  180. var scrollings = [];
  181. var controlPressed;
  182. var isScrollAllowed = {};
  183. isScrollAllowed.m = { 'up':true, 'down':true, 'left':true, 'right':true };
  184. isScrollAllowed.k = $.extend(true,{}, isScrollAllowed.m);
  185. //timeouts
  186. var resizeId;
  187. var afterSectionLoadsId;
  188. var afterSlideLoadsId;
  189. var scrollId;
  190. var scrollId2;
  191. var keydownId;
  192. var originals = $.extend(true, {}, options); //deep copy
  193. displayWarnings();
  194. //fixing bug in iScroll with links: https://github.com/cubiq/iscroll/issues/783
  195. iscrollOptions.click = isTouch; // see #2035
  196. //extending iScroll options with the user custom ones
  197. iscrollOptions = $.extend(iscrollOptions, options.scrollOverflowOptions);
  198. //easeInOutCubic animation included in the plugin
  199. $.extend($.easing,{ easeInOutCubic: function (x, t, b, c, d) {if ((t/=d/2) < 1) return c/2*t*t*t + b;return c/2*((t-=2)*t*t + 2) + b;}});
  200. /**
  201. * Sets the autoScroll option.
  202. * It changes the scroll bar visibility and the history of the site as a result.
  203. */
  204. function setAutoScrolling(value, type){
  205. //removing the transformation
  206. if(!value){
  207. silentScroll(0);
  208. }
  209. setVariableState('autoScrolling', value, type);
  210. var element = $(SECTION_ACTIVE_SEL);
  211. if(options.autoScrolling && !options.scrollBar){
  212. $htmlBody.css({
  213. 'overflow' : 'hidden',
  214. 'height' : '100%'
  215. });
  216. setRecordHistory(originals.recordHistory, 'internal');
  217. //for IE touch devices
  218. container.css({
  219. '-ms-touch-action': 'none',
  220. 'touch-action': 'none'
  221. });
  222. if(element.length){
  223. //moving the container up
  224. silentScroll(element.position().top);
  225. }
  226. }else{
  227. $htmlBody.css({
  228. 'overflow' : 'visible',
  229. 'height' : 'initial'
  230. });
  231. setRecordHistory(false, 'internal');
  232. //for IE touch devices
  233. container.css({
  234. '-ms-touch-action': '',
  235. 'touch-action': ''
  236. });
  237. //scrolling the page to the section with no animation
  238. if (element.length) {
  239. $htmlBody.scrollTop(element.position().top);
  240. }
  241. }
  242. }
  243. /**
  244. * Defines wheter to record the history for each hash change in the URL.
  245. */
  246. function setRecordHistory(value, type){
  247. setVariableState('recordHistory', value, type);
  248. }
  249. /**
  250. * Defines the scrolling speed
  251. */
  252. function setScrollingSpeed(value, type){
  253. setVariableState('scrollingSpeed', value, type);
  254. }
  255. /**
  256. * Sets fitToSection
  257. */
  258. function setFitToSection(value, type){
  259. setVariableState('fitToSection', value, type);
  260. }
  261. /**
  262. * Sets lockAnchors
  263. */
  264. function setLockAnchors(value){
  265. options.lockAnchors = value;
  266. }
  267. /**
  268. * Adds or remove the possiblity of scrolling through sections by using the mouse wheel or the trackpad.
  269. */
  270. function setMouseWheelScrolling(value){
  271. if(value){
  272. addMouseWheelHandler();
  273. addMiddleWheelHandler();
  274. }else{
  275. removeMouseWheelHandler();
  276. removeMiddleWheelHandler();
  277. }
  278. }
  279. /**
  280. * Adds or remove the possibility of scrolling through sections by using the mouse wheel/trackpad or touch gestures.
  281. * Optionally a second parameter can be used to specify the direction for which the action will be applied.
  282. *
  283. * @param directions string containing the direction or directions separated by comma.
  284. */
  285. function setAllowScrolling(value, directions){
  286. if(typeof directions !== 'undefined'){
  287. directions = directions.replace(/ /g,'').split(',');
  288. $.each(directions, function (index, direction){
  289. setIsScrollAllowed(value, direction, 'm');
  290. });
  291. }
  292. else if(value){
  293. setMouseWheelScrolling(true);
  294. addTouchHandler();
  295. }else{
  296. setMouseWheelScrolling(false);
  297. removeTouchHandler();
  298. }
  299. }
  300. /**
  301. * Adds or remove the possibility of scrolling through sections by using the keyboard arrow keys
  302. */
  303. function setKeyboardScrolling(value, directions){
  304. if(typeof directions !== 'undefined'){
  305. directions = directions.replace(/ /g,'').split(',');
  306. $.each(directions, function (index, direction){
  307. setIsScrollAllowed(value, direction, 'k');
  308. });
  309. }else{
  310. options.keyboardScrolling = value;
  311. }
  312. }
  313. /**
  314. * Moves the page up one section.
  315. */
  316. function moveSectionUp(){
  317. var prev = $(SECTION_ACTIVE_SEL).prev(SECTION_SEL);
  318. //looping to the bottom if there's no more sections above
  319. if (!prev.length && (options.loopTop || options.continuousVertical)) {
  320. prev = $(SECTION_SEL).last();
  321. }
  322. if (prev.length) {
  323. scrollPage(prev, null, true);
  324. }
  325. }
  326. /**
  327. * Moves the page down one section.
  328. */
  329. function moveSectionDown(){
  330. var next = $(SECTION_ACTIVE_SEL).next(SECTION_SEL);
  331. //looping to the top if there's no more sections below
  332. if(!next.length &&
  333. (options.loopBottom || options.continuousVertical)){
  334. next = $(SECTION_SEL).first();
  335. }
  336. if(next.length){
  337. scrollPage(next, null, false);
  338. }
  339. }
  340. /**
  341. * Moves the page to the given section and slide with no animation.
  342. * Anchors or index positions can be used as params.
  343. */
  344. function silentMoveTo(sectionAnchor, slideAnchor){
  345. setScrollingSpeed (0, 'internal');
  346. moveTo(sectionAnchor, slideAnchor);
  347. setScrollingSpeed (originals.scrollingSpeed, 'internal');
  348. }
  349. /**
  350. * Moves the page to the given section and slide.
  351. * Anchors or index positions can be used as params.
  352. */
  353. function moveTo(sectionAnchor, slideAnchor){
  354. var destiny = getSectionByAnchor(sectionAnchor);
  355. if (typeof slideAnchor !== 'undefined'){
  356. scrollPageAndSlide(sectionAnchor, slideAnchor);
  357. }else if(destiny.length > 0){
  358. scrollPage(destiny);
  359. }
  360. }
  361. /**
  362. * Slides right the slider of the active section.
  363. * Optional `section` param.
  364. */
  365. function moveSlideRight(section){
  366. moveSlide('right', section);
  367. }
  368. /**
  369. * Slides left the slider of the active section.
  370. * Optional `section` param.
  371. */
  372. function moveSlideLeft(section){
  373. moveSlide('left', section);
  374. }
  375. /**
  376. * When resizing is finished, we adjust the slides sizes and positions
  377. */
  378. function reBuild(resizing){
  379. if(container.hasClass(DESTROYED)){ return; } //nothing to do if the plugin was destroyed
  380. isResizing = true;
  381. windowsHeight = $window.height(); //updating global var
  382. $(SECTION_SEL).each(function(){
  383. var slidesWrap = $(this).find(SLIDES_WRAPPER_SEL);
  384. var slides = $(this).find(SLIDE_SEL);
  385. //adjusting the height of the table-cell for IE and Firefox
  386. if(options.verticalCentered){
  387. $(this).find(TABLE_CELL_SEL).css('height', getTableHeight($(this)) + 'px');
  388. }
  389. $(this).css('height', windowsHeight + 'px');
  390. //resizing the scrolling divs
  391. if(options.scrollOverflow){
  392. if(slides.length){
  393. slides.each(function(){
  394. createScrollBar($(this));
  395. });
  396. }else{
  397. createScrollBar($(this));
  398. }
  399. }
  400. //adjusting the position fo the FULL WIDTH slides...
  401. if (slides.length > 1) {
  402. landscapeScroll(slidesWrap, slidesWrap.find(SLIDE_ACTIVE_SEL));
  403. }
  404. });
  405. var activeSection = $(SECTION_ACTIVE_SEL);
  406. var sectionIndex = activeSection.index(SECTION_SEL);
  407. //isn't it the first section?
  408. if(sectionIndex){
  409. //adjusting the position for the current section
  410. silentMoveTo(sectionIndex + 1);
  411. }
  412. isResizing = false;
  413. $.isFunction( options.afterResize ) && resizing && options.afterResize.call(container);
  414. $.isFunction( options.afterReBuild ) && !resizing && options.afterReBuild.call(container);
  415. }
  416. /**
  417. * Turns fullPage.js to normal scrolling mode when the viewport `width` or `height`
  418. * are smaller than the set limit values.
  419. */
  420. function setResponsive(active){
  421. var isResponsive = $body.hasClass(RESPONSIVE);
  422. if(active){
  423. if(!isResponsive){
  424. setAutoScrolling(false, 'internal');
  425. setFitToSection(false, 'internal');
  426. $(SECTION_NAV_SEL).hide();
  427. $body.addClass(RESPONSIVE);
  428. $.isFunction( options.afterResponsive ) && options.afterResponsive.call( container, active);
  429. }
  430. }
  431. else if(isResponsive){
  432. setAutoScrolling(originals.autoScrolling, 'internal');
  433. setFitToSection(originals.autoScrolling, 'internal');
  434. $(SECTION_NAV_SEL).show();
  435. $body.removeClass(RESPONSIVE);
  436. $.isFunction( options.afterResponsive ) && options.afterResponsive.call( container, active);
  437. }
  438. }
  439. if($(this).length){
  440. //public functions
  441. FP.setAutoScrolling = setAutoScrolling;
  442. FP.setRecordHistory = setRecordHistory;
  443. FP.setScrollingSpeed = setScrollingSpeed;
  444. FP.setFitToSection = setFitToSection;
  445. FP.setLockAnchors = setLockAnchors;
  446. FP.setMouseWheelScrolling = setMouseWheelScrolling;
  447. FP.setAllowScrolling = setAllowScrolling;
  448. FP.setKeyboardScrolling = setKeyboardScrolling;
  449. FP.moveSectionUp = moveSectionUp;
  450. FP.moveSectionDown = moveSectionDown;
  451. FP.silentMoveTo = silentMoveTo;
  452. FP.moveTo = moveTo;
  453. FP.moveSlideRight = moveSlideRight;
  454. FP.moveSlideLeft = moveSlideLeft;
  455. FP.reBuild = reBuild;
  456. FP.setResponsive = setResponsive;
  457. FP.destroy = destroy;
  458. init();
  459. bindEvents();
  460. }
  461. function init(){
  462. //if css3 is not supported, it will use jQuery animations
  463. if(options.css3){
  464. options.css3 = support3d();
  465. }
  466. options.scrollBar = options.scrollBar || options.hybrid;
  467. setOptionsFromDOM();
  468. prepareDom();
  469. setAllowScrolling(true);
  470. setAutoScrolling(options.autoScrolling, 'internal');
  471. //the starting point is a slide?
  472. var activeSlide = $(SECTION_ACTIVE_SEL).find(SLIDE_ACTIVE_SEL);
  473. //the active section isn't the first one? Is not the first slide of the first section? Then we load that section/slide by default.
  474. if( activeSlide.length && ($(SECTION_ACTIVE_SEL).index(SECTION_SEL) !== 0 || ($(SECTION_ACTIVE_SEL).index(SECTION_SEL) === 0 && activeSlide.index() !== 0))){
  475. silentLandscapeScroll(activeSlide);
  476. }
  477. responsive();
  478. //setting the class for the body element
  479. setBodyClass();
  480. if(document.readyState === 'complete'){
  481. scrollToAnchor();
  482. }
  483. $window.on('load', scrollToAnchor);
  484. }
  485. function bindEvents(){
  486. $window
  487. //when scrolling...
  488. .on('scroll', scrollHandler)
  489. //detecting any change on the URL to scroll to the given anchor link
  490. //(a way to detect back history button as we play with the hashes on the URL)
  491. .on('hashchange', hashChangeHandler)
  492. //when opening a new tab (ctrl + t), `control` won't be pressed when coming back.
  493. .blur(blurHandler)
  494. //when resizing the site, we adjust the heights of the sections, slimScroll...
  495. .resize(resizeHandler);
  496. $document
  497. //Sliding with arrow keys, both, vertical and horizontal
  498. .keydown(keydownHandler)
  499. //to prevent scrolling while zooming
  500. .keyup(keyUpHandler)
  501. //Scrolls to the section when clicking the navigation bullet
  502. .on('click touchstart', SECTION_NAV_SEL + ' a', sectionBulletHandler)
  503. //Scrolls the slider to the given slide destination for the given section
  504. .on('click touchstart', SLIDES_NAV_LINK_SEL, slideBulletHandler)
  505. .on('click', SECTION_NAV_TOOLTIP_SEL, tooltipTextHandler);
  506. //Scrolling horizontally when clicking on the slider controls.
  507. $(SECTION_SEL).on('click touchstart', SLIDES_ARROW_SEL, slideArrowHandler);
  508. /**
  509. * Applying normalScroll elements.
  510. * Ignoring the scrolls over the specified selectors.
  511. */
  512. if(options.normalScrollElements){
  513. $document.on('mouseenter', options.normalScrollElements, function () {
  514. setMouseWheelScrolling(false);
  515. });
  516. $document.on('mouseleave', options.normalScrollElements, function(){
  517. setMouseWheelScrolling(true);
  518. });
  519. }
  520. }
  521. /**
  522. * Setting options from DOM elements if they are not provided.
  523. */
  524. function setOptionsFromDOM(){
  525. var sections = container.find(options.sectionSelector);
  526. //no anchors option? Checking for them in the DOM attributes
  527. if(!options.anchors.length){
  528. options.anchors = sections.filter('[data-anchor]').map(function(){
  529. return $(this).data('anchor').toString();
  530. }).get();
  531. }
  532. //no tooltips option? Checking for them in the DOM attributes
  533. if(!options.navigationTooltips.length){
  534. options.navigationTooltips = sections.filter('[data-tooltip]').map(function(){
  535. return $(this).data('tooltip').toString();
  536. }).get();
  537. }
  538. }
  539. /**
  540. * Works over the DOM structure to set it up for the current fullpage options.
  541. */
  542. function prepareDom(){
  543. container.css({
  544. 'height': '100%',
  545. 'position': 'relative'
  546. });
  547. //adding a class to recognize the container internally in the code
  548. container.addClass(WRAPPER);
  549. $('html').addClass(ENABLED);
  550. //due to https://github.com/alvarotrigo/fullPage.js/issues/1502
  551. windowsHeight = $window.height();
  552. container.removeClass(DESTROYED); //in case it was destroyed before initializing it again
  553. addInternalSelectors();
  554. //styling the sections / slides / menu
  555. $(SECTION_SEL).each(function(index){
  556. var section = $(this);
  557. var slides = section.find(SLIDE_SEL);
  558. var numSlides = slides.length;
  559. styleSection(section, index);
  560. styleMenu(section, index);
  561. // if there's any slide
  562. if (numSlides > 0) {
  563. styleSlides(section, slides, numSlides);
  564. }else{
  565. if(options.verticalCentered){
  566. addTableClass(section);
  567. }
  568. }
  569. });
  570. //fixed elements need to be moved out of the plugin container due to problems with CSS3.
  571. if(options.fixedElements && options.css3){
  572. $(options.fixedElements).appendTo($body);
  573. }
  574. //vertical centered of the navigation + active bullet
  575. if(options.navigation){
  576. addVerticalNavigation();
  577. }
  578. enableYoutubeAPI();
  579. if(options.scrollOverflow){
  580. if(document.readyState === 'complete'){
  581. createScrollBarHandler();
  582. }
  583. //after DOM and images are loaded
  584. $window.on('load', createScrollBarHandler);
  585. }else{
  586. afterRenderActions();
  587. }
  588. }
  589. /**
  590. * Styles the horizontal slides for a section.
  591. */
  592. function styleSlides(section, slides, numSlides){
  593. var sliderWidth = numSlides * 100;
  594. var slideWidth = 100 / numSlides;
  595. slides.wrapAll('<div class="' + SLIDES_CONTAINER + '" />');
  596. slides.parent().wrap('<div class="' + SLIDES_WRAPPER + '" />');
  597. section.find(SLIDES_CONTAINER_SEL).css('width', sliderWidth + '%');
  598. if(numSlides > 1){
  599. if(options.controlArrows){
  600. createSlideArrows(section);
  601. }
  602. if(options.slidesNavigation){
  603. addSlidesNavigation(section, numSlides);
  604. }
  605. }
  606. slides.each(function(index) {
  607. $(this).css('width', slideWidth + '%');
  608. if(options.verticalCentered){
  609. addTableClass($(this));
  610. }
  611. });
  612. var startingSlide = section.find(SLIDE_ACTIVE_SEL);
  613. //if the slide won't be an starting point, the default will be the first one
  614. //the active section isn't the first one? Is not the first slide of the first section? Then we load that section/slide by default.
  615. if( startingSlide.length && ($(SECTION_ACTIVE_SEL).index(SECTION_SEL) !== 0 || ($(SECTION_ACTIVE_SEL).index(SECTION_SEL) === 0 && startingSlide.index() !== 0))){
  616. silentLandscapeScroll(startingSlide);
  617. }else{
  618. slides.eq(0).addClass(ACTIVE);
  619. }
  620. }
  621. /**
  622. * Styling vertical sections
  623. */
  624. function styleSection(section, index){
  625. //if no active section is defined, the 1st one will be the default one
  626. if(!index && $(SECTION_ACTIVE_SEL).length === 0) {
  627. section.addClass(ACTIVE);
  628. }
  629. section.css('height', windowsHeight + 'px');
  630. if(options.paddingTop){
  631. section.css('padding-top', options.paddingTop);
  632. }
  633. if(options.paddingBottom){
  634. section.css('padding-bottom', options.paddingBottom);
  635. }
  636. if (typeof options.sectionsColor[index] !== 'undefined') {
  637. section.css('background-color', options.sectionsColor[index]);
  638. }
  639. if (typeof options.anchors[index] !== 'undefined') {
  640. section.attr('data-anchor', options.anchors[index]);
  641. }
  642. }
  643. /**
  644. * Sets the data-anchor attributes to the menu elements and activates the current one.
  645. */
  646. function styleMenu(section, index){
  647. if (typeof options.anchors[index] !== 'undefined') {
  648. //activating the menu / nav element on load
  649. if(section.hasClass(ACTIVE)){
  650. activateMenuAndNav(options.anchors[index], index);
  651. }
  652. }
  653. //moving the menu outside the main container if it is inside (avoid problems with fixed positions when using CSS3 tranforms)
  654. if(options.menu && options.css3 && $(options.menu).closest(WRAPPER_SEL).length){
  655. $(options.menu).appendTo($body);
  656. }
  657. }
  658. /**
  659. * Adds internal classes to be able to provide customizable selectors
  660. * keeping the link with the style sheet.
  661. */
  662. function addInternalSelectors(){
  663. container.find(options.sectionSelector).addClass(SECTION);
  664. container.find(options.slideSelector).addClass(SLIDE);
  665. }
  666. /**
  667. * Creates the control arrows for the given section
  668. */
  669. function createSlideArrows(section){
  670. section.find(SLIDES_WRAPPER_SEL).after('<div class="' + SLIDES_ARROW_PREV + '"></div><div class="' + SLIDES_ARROW_NEXT + '"></div>');
  671. if(options.controlArrowColor!='#fff'){
  672. section.find(SLIDES_ARROW_NEXT_SEL).css('border-color', 'transparent transparent transparent '+options.controlArrowColor);
  673. section.find(SLIDES_ARROW_PREV_SEL).css('border-color', 'transparent '+ options.controlArrowColor + ' transparent transparent');
  674. }
  675. if(!options.loopHorizontal){
  676. section.find(SLIDES_ARROW_PREV_SEL).hide();
  677. }
  678. }
  679. /**
  680. * Creates a vertical navigation bar.
  681. */
  682. function addVerticalNavigation(){
  683. $body.append('<div id="' + SECTION_NAV + '"><ul></ul></div>');
  684. var nav = $(SECTION_NAV_SEL);
  685. nav.addClass(function() {
  686. return options.showActiveTooltip ? SHOW_ACTIVE_TOOLTIP + ' ' + options.navigationPosition : options.navigationPosition;
  687. });
  688. for (var i = 0; i < $(SECTION_SEL).length; i++) {
  689. var link = '';
  690. if (options.anchors.length) {
  691. link = options.anchors[i];
  692. }
  693. var li = '<li><a href="#' + link + '"><span></span></a>';
  694. // Only add tooltip if needed (defined by user)
  695. var tooltip = options.navigationTooltips[i];
  696. if (typeof tooltip !== 'undefined' && tooltip !== '') {
  697. li += '<div class="' + SECTION_NAV_TOOLTIP + ' ' + options.navigationPosition + '">' + tooltip + '</div>';
  698. }
  699. li += '</li>';
  700. nav.find('ul').append(li);
  701. }
  702. //centering it vertically
  703. $(SECTION_NAV_SEL).css('margin-top', '-' + ($(SECTION_NAV_SEL).height()/2) + 'px');
  704. //activating the current active section
  705. $(SECTION_NAV_SEL).find('li').eq($(SECTION_ACTIVE_SEL).index(SECTION_SEL)).find('a').addClass(ACTIVE);
  706. }
  707. /**
  708. * Creates the slim scroll scrollbar for the sections and slides inside them.
  709. */
  710. function createScrollBarHandler(){
  711. $(SECTION_SEL).each(function(){
  712. var slides = $(this).find(SLIDE_SEL);
  713. if(slides.length){
  714. slides.each(function(){
  715. createScrollBar($(this));
  716. });
  717. }else{
  718. createScrollBar($(this));
  719. }
  720. });
  721. afterRenderActions();
  722. }
  723. /*
  724. * Enables the Youtube videos API so we can control their flow if necessary.
  725. */
  726. function enableYoutubeAPI(){
  727. container.find('iframe[src*="youtube.com/embed/"]').each(function(){
  728. addURLParam($(this), 'enablejsapi=1');
  729. });
  730. }
  731. /**
  732. * Adds a new parameter and its value to the `src` of a given element
  733. */
  734. function addURLParam(element, newParam){
  735. var originalSrc = element.attr('src');
  736. element.attr('src', originalSrc + getUrlParamSign(originalSrc) + newParam);
  737. }
  738. /*
  739. * Returns the prefix sign to use for a new parameter in an existen URL.
  740. *
  741. * @return {String} ? | &
  742. */
  743. function getUrlParamSign(url){
  744. return ( !/\?/.test( url ) ) ? '?' : '&';
  745. }
  746. /**
  747. * Actions and callbacks to fire afterRender
  748. */
  749. function afterRenderActions(){
  750. var section = $(SECTION_ACTIVE_SEL);
  751. section.addClass(COMPLETELY);
  752. if(options.scrollOverflowHandler.afterRender){
  753. options.scrollOverflowHandler.afterRender(section);
  754. }
  755. lazyLoad(section);
  756. playMedia(section);
  757. options.scrollOverflowHandler.afterLoad();
  758. $.isFunction( options.afterLoad ) && options.afterLoad.call(section, section.data('anchor'), (section.index(SECTION_SEL) + 1));
  759. $.isFunction( options.afterRender ) && options.afterRender.call(container);
  760. }
  761. var isScrolling = false;
  762. var lastScroll = 0;
  763. //when scrolling...
  764. function scrollHandler(){
  765. var currentSection;
  766. if(!options.autoScrolling || options.scrollBar){
  767. var currentScroll = $window.scrollTop();
  768. var scrollDirection = getScrollDirection(currentScroll);
  769. var visibleSectionIndex = 0;
  770. var screen_mid = currentScroll + ($window.height() / 2.0);
  771. var isAtBottom = $body.height() - $window.height() === currentScroll;
  772. var sections = document.querySelectorAll(SECTION_SEL);
  773. //when using `auto-height` for a small last section it won't be centered in the viewport
  774. if(isAtBottom){
  775. visibleSectionIndex = sections.length - 1;
  776. }
  777. //is at top? when using `auto-height` for a small first section it won't be centered in the viewport
  778. else if(!currentScroll){
  779. visibleSectionIndex = 0;
  780. }
  781. //taking the section which is showing more content in the viewport
  782. else{
  783. for (var i = 0; i < sections.length; ++i) {
  784. var section = sections[i];
  785. // Pick the the last section which passes the middle line of the screen.
  786. if (section.offsetTop <= screen_mid)
  787. {
  788. visibleSectionIndex = i;
  789. }
  790. }
  791. }
  792. if(isCompletelyInViewPort(scrollDirection)){
  793. if(!$(SECTION_ACTIVE_SEL).hasClass(COMPLETELY)){
  794. $(SECTION_ACTIVE_SEL).addClass(COMPLETELY).siblings().removeClass(COMPLETELY);
  795. }
  796. }
  797. //geting the last one, the current one on the screen
  798. currentSection = $(sections).eq(visibleSectionIndex);
  799. //setting the visible section as active when manually scrolling
  800. //executing only once the first time we reach the section
  801. if(!currentSection.hasClass(ACTIVE)){
  802. isScrolling = true;
  803. var leavingSection = $(SECTION_ACTIVE_SEL);
  804. var leavingSectionIndex = leavingSection.index(SECTION_SEL) + 1;
  805. var yMovement = getYmovement(currentSection);
  806. var anchorLink = currentSection.data('anchor');
  807. var sectionIndex = currentSection.index(SECTION_SEL) + 1;
  808. var activeSlide = currentSection.find(SLIDE_ACTIVE_SEL);
  809. var slideIndex;
  810. var slideAnchorLink;
  811. if(activeSlide.length){
  812. slideAnchorLink = activeSlide.data('anchor');
  813. slideIndex = activeSlide.index();
  814. }
  815. if(canScroll){
  816. currentSection.addClass(ACTIVE).siblings().removeClass(ACTIVE);
  817. $.isFunction( options.onLeave ) && options.onLeave.call( leavingSection, leavingSectionIndex, sectionIndex, yMovement);
  818. $.isFunction( options.afterLoad ) && options.afterLoad.call( currentSection, anchorLink, sectionIndex);
  819. stopMedia(leavingSection);
  820. lazyLoad(currentSection);
  821. playMedia(currentSection);
  822. activateMenuAndNav(anchorLink, sectionIndex - 1);
  823. if(options.anchors.length){
  824. //needed to enter in hashChange event when using the menu with anchor links
  825. lastScrolledDestiny = anchorLink;
  826. }
  827. setState(slideIndex, slideAnchorLink, anchorLink, sectionIndex);
  828. }
  829. //small timeout in order to avoid entering in hashChange event when scrolling is not finished yet
  830. clearTimeout(scrollId);
  831. scrollId = setTimeout(function(){
  832. isScrolling = false;
  833. }, 100);
  834. }
  835. if(options.fitToSection){
  836. //for the auto adjust of the viewport to fit a whole section
  837. clearTimeout(scrollId2);
  838. scrollId2 = setTimeout(function(){
  839. //checking fitToSection again in case it was set to false before the timeout delay
  840. if(canScroll && options.fitToSection){
  841. //allows to scroll to an active section and
  842. //if the section is already active, we prevent firing callbacks
  843. if($(SECTION_ACTIVE_SEL).is(currentSection)){
  844. isResizing = true;
  845. }
  846. scrollPage($(SECTION_ACTIVE_SEL));
  847. isResizing = false;
  848. }
  849. }, options.fitToSectionDelay);
  850. }
  851. }
  852. }
  853. /**
  854. * Determines whether the active section has seen in its whole or not.
  855. */
  856. function isCompletelyInViewPort(movement){
  857. var top = $(SECTION_ACTIVE_SEL).position().top;
  858. var bottom = top + $window.height();
  859. if(movement == 'up'){
  860. return bottom >= ($window.scrollTop() + $window.height());
  861. }
  862. return top <= $window.scrollTop();
  863. }
  864. /**
  865. * Gets the directon of the the scrolling fired by the scroll event.
  866. */
  867. function getScrollDirection(currentScroll){
  868. var direction = currentScroll > lastScroll ? 'down' : 'up';
  869. lastScroll = currentScroll;
  870. //needed for auto-height sections to determine if we want to scroll to the top or bottom of the destination
  871. previousDestTop = currentScroll;
  872. return direction;
  873. }
  874. /**
  875. * Determines the way of scrolling up or down:
  876. * by 'automatically' scrolling a section or by using the default and normal scrolling.
  877. */
  878. function scrolling(type, scrollable){
  879. if (!isScrollAllowed.m[type]){
  880. return;
  881. }
  882. var check = (type === 'down') ? 'bottom' : 'top';
  883. var scrollSection = (type === 'down') ? moveSectionDown : moveSectionUp;
  884. if(scrollable.length > 0 ){
  885. //is the scrollbar at the start/end of the scroll?
  886. if(options.scrollOverflowHandler.isScrolled(check, scrollable)){
  887. scrollSection();
  888. }else{
  889. return true;
  890. }
  891. }else{
  892. // moved up/down
  893. scrollSection();
  894. }
  895. }
  896. /*
  897. * Preventing bouncing in iOS #2285
  898. */
  899. function preventBouncing(event){
  900. var e = event.originalEvent;
  901. if(!checkParentForNormalScrollElement(event.target) && options.autoScrolling && isReallyTouch(e)){
  902. //preventing the easing on iOS devices
  903. event.preventDefault();
  904. }
  905. }
  906. var touchStartY = 0;
  907. var touchStartX = 0;
  908. var touchEndY = 0;
  909. var touchEndX = 0;
  910. /* Detecting touch events
  911. * As we are changing the top property of the page on scrolling, we can not use the traditional way to detect it.
  912. * This way, the touchstart and the touch moves shows an small difference between them which is the
  913. * used one to determine the direction.
  914. */
  915. function touchMoveHandler(event){
  916. var e = event.originalEvent;
  917. var activeSection = $(e.target).closest(SECTION_SEL);
  918. // additional: if one of the normalScrollElements isn't within options.normalScrollElementTouchThreshold hops up the DOM chain
  919. if (!checkParentForNormalScrollElement(event.target) && isReallyTouch(e) ) {
  920. if(options.autoScrolling){
  921. //preventing the easing on iOS devices
  922. event.preventDefault();
  923. }
  924. var scrollable = options.scrollOverflowHandler.scrollable(activeSection);
  925. var touchEvents = getEventsPage(e);
  926. touchEndY = touchEvents.y;
  927. touchEndX = touchEvents.x;
  928. //if movement in the X axys is greater than in the Y and the currect section has slides...
  929. if (activeSection.find(SLIDES_WRAPPER_SEL).length && Math.abs(touchStartX - touchEndX) > (Math.abs(touchStartY - touchEndY))) {
  930. //is the movement greater than the minimum resistance to scroll?
  931. if (!slideMoving && Math.abs(touchStartX - touchEndX) > ($window.outerWidth() / 100 * options.touchSensitivity)) {
  932. if (touchStartX > touchEndX) {
  933. if(isScrollAllowed.m.right){
  934. moveSlideRight(activeSection); //next
  935. }
  936. } else {
  937. if(isScrollAllowed.m.left){
  938. moveSlideLeft(activeSection); //prev
  939. }
  940. }
  941. }
  942. }
  943. //vertical scrolling (only when autoScrolling is enabled)
  944. else if(options.autoScrolling && canScroll){
  945. //is the movement greater than the minimum resistance to scroll?
  946. if (Math.abs(touchStartY - touchEndY) > ($window.height() / 100 * options.touchSensitivity)) {
  947. if (touchStartY > touchEndY) {
  948. scrolling('down', scrollable);
  949. } else if (touchEndY > touchStartY) {
  950. scrolling('up', scrollable);
  951. }
  952. }
  953. }
  954. }
  955. }
  956. /**
  957. * recursive function to loop up the parent nodes to check if one of them exists in options.normalScrollElements
  958. * Currently works well for iOS - Android might need some testing
  959. * @param {Element} el target element / jquery selector (in subsequent nodes)
  960. * @param {int} hop current hop compared to options.normalScrollElementTouchThreshold
  961. * @return {boolean} true if there is a match to options.normalScrollElements
  962. */
  963. function checkParentForNormalScrollElement (el, hop) {
  964. hop = hop || 0;
  965. var parent = $(el).parent();
  966. if (hop < options.normalScrollElementTouchThreshold &&
  967. parent.is(options.normalScrollElements) ) {
  968. return true;
  969. } else if (hop == options.normalScrollElementTouchThreshold) {
  970. return false;
  971. } else {
  972. return checkParentForNormalScrollElement(parent, ++hop);
  973. }
  974. }
  975. /**
  976. * As IE >= 10 fires both touch and mouse events when using a mouse in a touchscreen
  977. * this way we make sure that is really a touch event what IE is detecting.
  978. */
  979. function isReallyTouch(e){
  980. //if is not IE || IE is detecting `touch` or `pen`
  981. return typeof e.pointerType === 'undefined' || e.pointerType != 'mouse';
  982. }
  983. /**
  984. * Handler for the touch start event.
  985. */
  986. function touchStartHandler(event){
  987. var e = event.originalEvent;
  988. //stopping the auto scroll to adjust to a section
  989. if(options.fitToSection){
  990. $htmlBody.stop();
  991. }
  992. if(isReallyTouch(e)){
  993. var touchEvents = getEventsPage(e);
  994. touchStartY = touchEvents.y;
  995. touchStartX = touchEvents.x;
  996. }
  997. }
  998. /**
  999. * Gets the average of the last `number` elements of the given array.
  1000. */
  1001. function getAverage(elements, number){
  1002. var sum = 0;
  1003. //taking `number` elements from the end to make the average, if there are not enought, 1
  1004. var lastElements = elements.slice(Math.max(elements.length - number, 1));
  1005. for(var i = 0; i < lastElements.length; i++){
  1006. sum = sum + lastElements[i];
  1007. }
  1008. return Math.ceil(sum/number);
  1009. }
  1010. /**
  1011. * Detecting mousewheel scrolling
  1012. *
  1013. * http://blogs.sitepointstatic.com/examples/tech/mouse-wheel/index.html
  1014. * http://www.sitepoint.com/html5-javascript-mouse-wheel/
  1015. */
  1016. var prevTime = new Date().getTime();
  1017. function MouseWheelHandler(e) {
  1018. var curTime = new Date().getTime();
  1019. var isNormalScroll = $(COMPLETELY_SEL).hasClass(NORMAL_SCROLL);
  1020. //autoscrolling and not zooming?
  1021. if(options.autoScrolling && !controlPressed && !isNormalScroll){
  1022. // cross-browser wheel delta
  1023. e = e || window.event;
  1024. var value = e.wheelDelta || -e.deltaY || -e.detail;
  1025. var delta = Math.max(-1, Math.min(1, value));
  1026. var horizontalDetection = typeof e.wheelDeltaX !== 'undefined' || typeof e.deltaX !== 'undefined';
  1027. var isScrollingVertically = (Math.abs(e.wheelDeltaX) < Math.abs(e.wheelDelta)) || (Math.abs(e.deltaX ) < Math.abs(e.deltaY) || !horizontalDetection);
  1028. //Limiting the array to 150 (lets not waste memory!)
  1029. if(scrollings.length > 149){
  1030. scrollings.shift();
  1031. }
  1032. //keeping record of the previous scrollings
  1033. scrollings.push(Math.abs(value));
  1034. //preventing to scroll the site on mouse wheel when scrollbar is present
  1035. if(options.scrollBar){
  1036. e.preventDefault ? e.preventDefault() : e.returnValue = false;
  1037. }
  1038. var activeSection = $(SECTION_ACTIVE_SEL);
  1039. var scrollable = options.scrollOverflowHandler.scrollable(activeSection);
  1040. //time difference between the last scroll and the current one
  1041. var timeDiff = curTime-prevTime;
  1042. prevTime = curTime;
  1043. //haven't they scrolled in a while?
  1044. //(enough to be consider a different scrolling action to scroll another section)
  1045. if(timeDiff > 200){
  1046. //emptying the array, we dont care about old scrollings for our averages
  1047. scrollings = [];
  1048. }
  1049. if(canScroll){
  1050. var averageEnd = getAverage(scrollings, 10);
  1051. var averageMiddle = getAverage(scrollings, 70);
  1052. var isAccelerating = averageEnd >= averageMiddle;
  1053. //to avoid double swipes...
  1054. if(isAccelerating && isScrollingVertically){
  1055. //scrolling down?
  1056. if (delta < 0) {
  1057. scrolling('down', scrollable);
  1058. //scrolling up?
  1059. }else {
  1060. scrolling('up', scrollable);
  1061. }
  1062. }
  1063. }
  1064. return false;
  1065. }
  1066. if(options.fitToSection){
  1067. //stopping the auto scroll to adjust to a section
  1068. $htmlBody.stop();
  1069. }
  1070. }
  1071. /**
  1072. * Slides a slider to the given direction.
  1073. * Optional `section` param.
  1074. */
  1075. function moveSlide(direction, section){
  1076. var activeSection = typeof section === 'undefined' ? $(SECTION_ACTIVE_SEL) : section;
  1077. var slides = activeSection.find(SLIDES_WRAPPER_SEL);
  1078. var numSlides = slides.find(SLIDE_SEL).length;
  1079. // more than one slide needed and nothing should be sliding
  1080. if (!slides.length || slideMoving || numSlides < 2) {
  1081. return;
  1082. }
  1083. var currentSlide = slides.find(SLIDE_ACTIVE_SEL);
  1084. var destiny = null;
  1085. if(direction === 'left'){
  1086. destiny = currentSlide.prev(SLIDE_SEL);
  1087. }else{
  1088. destiny = currentSlide.next(SLIDE_SEL);
  1089. }
  1090. //isn't there a next slide in the secuence?
  1091. if(!destiny.length){
  1092. //respect loopHorizontal settin
  1093. if (!options.loopHorizontal) return;
  1094. if(direction === 'left'){
  1095. destiny = currentSlide.siblings(':last');
  1096. }else{
  1097. destiny = currentSlide.siblings(':first');
  1098. }
  1099. }
  1100. slideMoving = true;
  1101. landscapeScroll(slides, destiny, direction);
  1102. }
  1103. /**
  1104. * Maintains the active slides in the viewport
  1105. * (Because the `scroll` animation might get lost with some actions, such as when using continuousVertical)
  1106. */
  1107. function keepSlidesPosition(){
  1108. $(SLIDE_ACTIVE_SEL).each(function(){
  1109. silentLandscapeScroll($(this), 'internal');
  1110. });
  1111. }
  1112. var previousDestTop = 0;
  1113. /**
  1114. * Returns the destination Y position based on the scrolling direction and
  1115. * the height of the section.
  1116. */
  1117. function getDestinationPosition(element){
  1118. var elemPosition = element.position();
  1119. //top of the desination will be at the top of the viewport
  1120. var position = elemPosition.top;
  1121. var isScrollingDown = elemPosition.top > previousDestTop;
  1122. var sectionBottom = position - windowsHeight + element.outerHeight();
  1123. var bigSectionsDestination = options.bigSectionsDestination;
  1124. //is the destination element bigger than the viewport?
  1125. if(element.outerHeight() > windowsHeight){
  1126. //scrolling up?
  1127. if(!isScrollingDown && !bigSectionsDestination || bigSectionsDestination === 'bottom' ){
  1128. position = sectionBottom;
  1129. }
  1130. }
  1131. //sections equal or smaller than the viewport height && scrolling down? || is resizing and its in the last section
  1132. else if(isScrollingDown || (isResizing && element.is(':last-child')) ){
  1133. //The bottom of the destination will be at the bottom of the viewport
  1134. position = sectionBottom;
  1135. }
  1136. /*
  1137. Keeping record of the last scrolled position to determine the scrolling direction.
  1138. No conventional methods can be used as the scroll bar might not be present
  1139. AND the section might not be active if it is auto-height and didnt reach the middle
  1140. of the viewport.
  1141. */
  1142. previousDestTop = position;
  1143. return position;
  1144. }
  1145. /**
  1146. * Scrolls the site to the given element and scrolls to the slide if a callback is given.
  1147. */
  1148. function scrollPage(element, callback, isMovementUp){
  1149. if(typeof element === 'undefined'){ return; } //there's no element to scroll, leaving the function
  1150. var dtop = getDestinationPosition(element);
  1151. var slideAnchorLink;
  1152. var slideIndex;
  1153. //local variables
  1154. var v = {
  1155. element: element,
  1156. callback: callback,
  1157. isMovementUp: isMovementUp,
  1158. dtop: dtop,
  1159. yMovement: getYmovement(element),
  1160. anchorLink: element.data('anchor'),
  1161. sectionIndex: element.index(SECTION_SEL),
  1162. activeSlide: element.find(SLIDE_ACTIVE_SEL),
  1163. activeSection: $(SECTION_ACTIVE_SEL),
  1164. leavingSection: $(SECTION_ACTIVE_SEL).index(SECTION_SEL) + 1,
  1165. //caching the value of isResizing at the momment the function is called
  1166. //because it will be checked later inside a setTimeout and the value might change
  1167. localIsResizing: isResizing
  1168. };
  1169. //quiting when destination scroll is the same as the current one
  1170. if((v.activeSection.is(element) && !isResizing) || (options.scrollBar && $window.scrollTop() === v.dtop && !element.hasClass(AUTO_HEIGHT) )){ return; }
  1171. if(v.activeSlide.length){
  1172. slideAnchorLink = v.activeSlide.data('anchor');
  1173. slideIndex = v.activeSlide.index();
  1174. }
  1175. // If continuousVertical && we need to wrap around
  1176. if (options.autoScrolling && options.continuousVertical && typeof (v.isMovementUp) !== "undefined" &&
  1177. ((!v.isMovementUp && v.yMovement == 'up') || // Intending to scroll down but about to go up or
  1178. (v.isMovementUp && v.yMovement == 'down'))) { // intending to scroll up but about to go down
  1179. v = createInfiniteSections(v);
  1180. }
  1181. //callback (onLeave) if the site is not just resizing and readjusting the slides
  1182. if($.isFunction(options.onLeave) && !v.localIsResizing){
  1183. if(options.onLeave.call(v.activeSection, v.leavingSection, (v.sectionIndex + 1), v.yMovement) === false){
  1184. return;
  1185. }
  1186. }
  1187. stopMedia(v.activeSection);
  1188. options.scrollOverflowHandler.beforeLeave();
  1189. element.addClass(ACTIVE).siblings().removeClass(ACTIVE);
  1190. lazyLoad(element);
  1191. options.scrollOverflowHandler.onLeave();
  1192. //preventing from activating the MouseWheelHandler event
  1193. //more than once if the page is scrolling
  1194. canScroll = false;
  1195. setState(slideIndex, slideAnchorLink, v.anchorLink, v.sectionIndex);
  1196. performMovement(v);
  1197. //flag to avoid callingn `scrollPage()` twice in case of using anchor links
  1198. lastScrolledDestiny = v.anchorLink;
  1199. //avoid firing it twice (as it does also on scroll)
  1200. activateMenuAndNav(v.anchorLink, v.sectionIndex);
  1201. }
  1202. /**
  1203. * Performs the vertical movement (by CSS3 or by jQuery)
  1204. */
  1205. function performMovement(v){
  1206. // using CSS3 translate functionality
  1207. if (options.css3 && options.autoScrolling && !options.scrollBar) {
  1208. // The first section can have a negative value in iOS 10. Not quite sure why: -0.0142822265625
  1209. // that's why we round it to 0.
  1210. var translate3d = 'translate3d(0px, -' + Math.round(v.dtop) + 'px, 0px)';
  1211. transformContainer(translate3d, true);
  1212. //even when the scrollingSpeed is 0 there's a little delay, which might cause the
  1213. //scrollingSpeed to change in case of using silentMoveTo();
  1214. if(options.scrollingSpeed){
  1215. clearTimeout(afterSectionLoadsId);
  1216. afterSectionLoadsId = setTimeout(function () {
  1217. afterSectionLoads(v);
  1218. }, options.scrollingSpeed);
  1219. }else{
  1220. afterSectionLoads(v);
  1221. }
  1222. }
  1223. // using jQuery animate
  1224. else{
  1225. var scrollSettings = getScrollSettings(v);
  1226. $(scrollSettings.element).animate(
  1227. scrollSettings.options,
  1228. options.scrollingSpeed, options.easing).promise().done(function () { //only one single callback in case of animating `html, body`
  1229. if(options.scrollBar){
  1230. /* Hack!
  1231. The timeout prevents setting the most dominant section in the viewport as "active" when the user
  1232. scrolled to a smaller section by using the mousewheel (auto scrolling) rather than draging the scroll bar.
  1233. When using scrollBar:true It seems like the scroll events still getting propagated even after the scrolling animation has finished.
  1234. */
  1235. setTimeout(function(){
  1236. afterSectionLoads(v);
  1237. },30);
  1238. }else{
  1239. afterSectionLoads(v);
  1240. }
  1241. });
  1242. }
  1243. }
  1244. /**
  1245. * Gets the scrolling settings depending on the plugin autoScrolling option
  1246. */
  1247. function getScrollSettings(v){
  1248. var scroll = {};
  1249. if(options.autoScrolling && !options.scrollBar){
  1250. scroll.options = { 'top': -v.dtop};
  1251. scroll.element = WRAPPER_SEL;
  1252. }else{
  1253. scroll.options = { 'scrollTop': v.dtop};
  1254. scroll.element = 'html, body';
  1255. }
  1256. return scroll;
  1257. }
  1258. /**
  1259. * Adds sections before or after the current one to create the infinite effect.
  1260. */
  1261. function createInfiniteSections(v){
  1262. // Scrolling down
  1263. if (!v.isMovementUp) {
  1264. // Move all previous sections to after the active section
  1265. $(SECTION_ACTIVE_SEL).after(v.activeSection.prevAll(SECTION_SEL).get().reverse());
  1266. }
  1267. else { // Scrolling up
  1268. // Move all next sections to before the active section
  1269. $(SECTION_ACTIVE_SEL).before(v.activeSection.nextAll(SECTION_SEL));
  1270. }
  1271. // Maintain the displayed position (now that we changed the element order)
  1272. silentScroll($(SECTION_ACTIVE_SEL).position().top);
  1273. // Maintain the active slides visible in the viewport
  1274. keepSlidesPosition();
  1275. // save for later the elements that still need to be reordered
  1276. v.wrapAroundElements = v.activeSection;
  1277. // Recalculate animation variables
  1278. v.dtop = v.element.position().top;
  1279. v.yMovement = getYmovement(v.element);
  1280. return v;
  1281. }
  1282. /**
  1283. * Fix section order after continuousVertical changes have been animated
  1284. */
  1285. function continuousVerticalFixSectionOrder (v) {
  1286. // If continuousVertical is in effect (and autoScrolling would also be in effect then),
  1287. // finish moving the elements around so the direct navigation will function more simply
  1288. if (!v.wrapAroundElements || !v.wrapAroundElements.length) {
  1289. return;
  1290. }
  1291. if (v.isMovementUp) {
  1292. $(SECTION_FIRST_SEL).before(v.wrapAroundElements);
  1293. }
  1294. else {
  1295. $(SECTION_LAST_SEL).after(v.wrapAroundElements);
  1296. }
  1297. silentScroll($(SECTION_ACTIVE_SEL).position().top);
  1298. // Maintain the active slides visible in the viewport
  1299. keepSlidesPosition();
  1300. }
  1301. /**
  1302. * Actions to do once the section is loaded.
  1303. */
  1304. function afterSectionLoads (v){
  1305. continuousVerticalFixSectionOrder(v);
  1306. //callback (afterLoad) if the site is not just resizing and readjusting the slides
  1307. $.isFunction(options.afterLoad) && !v.localIsResizing && options.afterLoad.call(v.element, v.anchorLink, (v.sectionIndex + 1));
  1308. options.scrollOverflowHandler.afterLoad();
  1309. if(!v.localIsResizing){
  1310. playMedia(v.element);
  1311. }
  1312. v.element.addClass(COMPLETELY).siblings().removeClass(COMPLETELY);
  1313. canScroll = true;
  1314. $.isFunction(v.callback) && v.callback.call(this);
  1315. }
  1316. /**
  1317. * Lazy loads image, video and audio elements.
  1318. */
  1319. function lazyLoad(destiny){
  1320. if (!options.lazyLoading){
  1321. return;
  1322. }
  1323. var panel = getSlideOrSection(destiny);
  1324. var element;
  1325. panel.find('img[data-src], source[data-src], audio[data-src], iframe[data-src]').each(function(){
  1326. element = $(this);
  1327. element.attr('src', element.data('src'));
  1328. element.removeAttr('data-src');
  1329. if(element.is('source')){
  1330. element.closest('video').get(0).load();
  1331. }
  1332. });
  1333. }
  1334. /**
  1335. * Plays video and audio elements.
  1336. */
  1337. function playMedia(destiny){
  1338. var panel = getSlideOrSection(destiny);
  1339. //playing HTML5 media elements
  1340. panel.find('video, audio').each(function(){
  1341. var element = $(this).get(0);
  1342. if( element.hasAttribute('data-autoplay') && typeof element.play === 'function' ) {
  1343. element.play();
  1344. }
  1345. });
  1346. //youtube videos
  1347. panel.find('iframe[src*="youtube.com/embed/"]').each(function(){
  1348. var element = $(this).get(0);
  1349. if ( element.hasAttribute('data-autoplay') ){
  1350. playYoutube(element);
  1351. }
  1352. //in case the URL was not loaded yet. On page load we need time for the new URL (with the API string) to load.
  1353. element.onload = function() {
  1354. if ( element.hasAttribute('data-autoplay') ){
  1355. playYoutube(element);
  1356. }
  1357. };
  1358. });
  1359. }
  1360. /**
  1361. * Plays a youtube video
  1362. */
  1363. function playYoutube(element){
  1364. element.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
  1365. }
  1366. /**
  1367. * Stops video and audio elements.
  1368. */
  1369. function stopMedia(destiny){
  1370. var panel = getSlideOrSection(destiny);
  1371. //stopping HTML5 media elements
  1372. panel.find('video, audio').each(function(){
  1373. var element = $(this).get(0);
  1374. if( !element.hasAttribute('data-keepplaying') && typeof element.pause === 'function' ) {
  1375. element.pause();
  1376. }
  1377. });
  1378. //youtube videos
  1379. panel.find('iframe[src*="youtube.com/embed/"]').each(function(){
  1380. var element = $(this).get(0);
  1381. if( /youtube\.com\/embed\//.test($(this).attr('src')) && !element.hasAttribute('data-keepplaying')){
  1382. $(this).get(0).contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}','*');
  1383. }
  1384. });
  1385. }
  1386. /**
  1387. * Gets the active slide (or section) for the given section
  1388. */
  1389. function getSlideOrSection(destiny){
  1390. var slide = destiny.find(SLIDE_ACTIVE_SEL);
  1391. if( slide.length ) {
  1392. destiny = $(slide);
  1393. }
  1394. return destiny;
  1395. }
  1396. /**
  1397. * Scrolls to the anchor in the URL when loading the site
  1398. */
  1399. function scrollToAnchor(){
  1400. //getting the anchor link in the URL and deleting the `#`
  1401. var value = window.location.hash.replace('#', '').split('/');
  1402. var sectionAnchor = decodeURIComponent(value[0]);
  1403. var slideAnchor = decodeURIComponent(value[1]);
  1404. if(sectionAnchor){ //if theres any #
  1405. if(options.animateAnchor){
  1406. scrollPageAndSlide(sectionAnchor, slideAnchor);
  1407. }else{
  1408. silentMoveTo(sectionAnchor, slideAnchor);
  1409. }
  1410. }
  1411. }
  1412. /**
  1413. * Detecting any change on the URL to scroll to the given anchor link
  1414. * (a way to detect back history button as we play with the hashes on the URL)
  1415. */
  1416. function hashChangeHandler(){
  1417. if(!isScrolling && !options.lockAnchors){
  1418. var value = window.location.hash.replace('#', '').split('/');
  1419. var sectionAnchor = decodeURIComponent(value[0]);
  1420. var slideAnchor = decodeURIComponent(value[1]);
  1421. //when moving to a slide in the first section for the first time (first time to add an anchor to the URL)
  1422. var isFirstSlideMove = (typeof lastScrolledDestiny === 'undefined');
  1423. var isFirstScrollMove = (typeof lastScrolledDestiny === 'undefined' && typeof slideAnchor === 'undefined' && !slideMoving);
  1424. if(sectionAnchor.length){
  1425. /*in order to call scrollpage() only once for each destination at a time
  1426. It is called twice for each scroll otherwise, as in case of using anchorlinks `hashChange`
  1427. event is fired on every scroll too.*/
  1428. if ((sectionAnchor && sectionAnchor !== lastScrolledDestiny) && !isFirstSlideMove || isFirstScrollMove || (!slideMoving && lastScrolledSlide != slideAnchor )) {
  1429. scrollPageAndSlide(sectionAnchor, slideAnchor);
  1430. }
  1431. }
  1432. }
  1433. }
  1434. //Sliding with arrow keys, both, vertical and horizontal
  1435. function keydownHandler(e) {
  1436. clearTimeout(keydownId);
  1437. var activeElement = $(':focus');
  1438. if(!activeElement.is('textarea') && !activeElement.is('input') && !activeElement.is('select') &&
  1439. activeElement.attr('contentEditable') !== "true" && activeElement.attr('contentEditable') !== '' &&
  1440. options.keyboardScrolling && options.autoScrolling){
  1441. var keyCode = e.which;
  1442. //preventing the scroll with arrow keys & spacebar & Page Up & Down keys
  1443. var keyControls = [40, 38, 32, 33, 34];
  1444. if($.inArray(keyCode, keyControls) > -1){
  1445. e.preventDefault();
  1446. }
  1447. controlPressed = e.ctrlKey;
  1448. keydownId = setTimeout(function(){
  1449. onkeydown(e);
  1450. },150);
  1451. }
  1452. }
  1453. function tooltipTextHandler(){
  1454. $(this).prev().trigger('click');
  1455. }
  1456. //to prevent scrolling while zooming
  1457. function keyUpHandler(e){
  1458. if(isWindowFocused){ //the keyup gets fired on new tab ctrl + t in Firefox
  1459. controlPressed = e.ctrlKey;
  1460. }
  1461. }
  1462. //binding the mousemove when the mouse's middle button is released
  1463. function mouseDownHandler(e){
  1464. //middle button
  1465. if (e.which == 2){
  1466. oldPageY = e.pageY;
  1467. container.on('mousemove', mouseMoveHandler);
  1468. }
  1469. }
  1470. //unbinding the mousemove when the mouse's middle button is released
  1471. function mouseUpHandler(e){
  1472. //middle button
  1473. if (e.which == 2){
  1474. container.off('mousemove');
  1475. }
  1476. }
  1477. //Scrolling horizontally when clicking on the slider controls.
  1478. function slideArrowHandler(){
  1479. var section = $(this).closest(SECTION_SEL);
  1480. if ($(this).hasClass(SLIDES_PREV)) {
  1481. if(isScrollAllowed.m.left){
  1482. moveSlideLeft(section);
  1483. }
  1484. } else {
  1485. if(isScrollAllowed.m.right){
  1486. moveSlideRight(section);
  1487. }
  1488. }
  1489. }
  1490. //when opening a new tab (ctrl + t), `control` won't be pressed when coming back.
  1491. function blurHandler(){
  1492. isWindowFocused = false;
  1493. controlPressed = false;
  1494. }
  1495. //Scrolls to the section when clicking the navigation bullet
  1496. function sectionBulletHandler(e){
  1497. e.preventDefault();
  1498. var index = $(this).parent().index();
  1499. scrollPage($(SECTION_SEL).eq(index));
  1500. }
  1501. //Scrolls the slider to the given slide destination for the given section
  1502. function slideBulletHandler(e){
  1503. e.preventDefault();
  1504. var slides = $(this).closest(SECTION_SEL).find(SLIDES_WRAPPER_SEL);
  1505. var destiny = slides.find(SLIDE_SEL).eq($(this).closest('li').index());
  1506. landscapeScroll(slides, destiny);
  1507. }
  1508. /**
  1509. * Keydown event
  1510. */
  1511. function onkeydown(e){
  1512. var shiftPressed = e.shiftKey;
  1513. //do nothing if we can not scroll or we are not using horizotnal key arrows.
  1514. if(!canScroll && [37,39].indexOf(e.which) < 0){
  1515. return;
  1516. }
  1517. switch (e.which) {
  1518. //up
  1519. case 38:
  1520. case 33:
  1521. if(isScrollAllowed.k.up){
  1522. moveSectionUp();
  1523. }
  1524. break;
  1525. //down
  1526. case 32: //spacebar
  1527. if(shiftPressed && isScrollAllowed.k.up){
  1528. moveSectionUp();
  1529. break;
  1530. }
  1531. /* falls through */
  1532. case 40:
  1533. case 34:
  1534. if(isScrollAllowed.k.down){
  1535. moveSectionDown();
  1536. }
  1537. break;
  1538. //Home
  1539. case 36:
  1540. if(isScrollAllowed.k.up){
  1541. moveTo(1);
  1542. }
  1543. break;
  1544. //End
  1545. case 35:
  1546. if(isScrollAllowed.k.down){
  1547. moveTo( $(SECTION_SEL).length );
  1548. }
  1549. break;
  1550. //left
  1551. case 37:
  1552. if(isScrollAllowed.k.left){
  1553. moveSlideLeft();
  1554. }
  1555. break;
  1556. //right
  1557. case 39:
  1558. if(isScrollAllowed.k.right){
  1559. moveSlideRight();
  1560. }
  1561. break;
  1562. default:
  1563. return; // exit this handler for other keys
  1564. }
  1565. }
  1566. /**
  1567. * Detecting the direction of the mouse movement.
  1568. * Used only for the middle button of the mouse.
  1569. */
  1570. var oldPageY = 0;
  1571. function mouseMoveHandler(e){
  1572. if(canScroll){
  1573. // moving up
  1574. if (e.pageY < oldPageY && isScrollAllowed.m.up){
  1575. moveSectionUp();
  1576. }
  1577. // moving down
  1578. else if(e.pageY > oldPageY && isScrollAllowed.m.down){
  1579. moveSectionDown();
  1580. }
  1581. }
  1582. oldPageY = e.pageY;
  1583. }
  1584. /**
  1585. * Scrolls horizontal sliders.
  1586. */
  1587. function landscapeScroll(slides, destiny, direction){
  1588. var section = slides.closest(SECTION_SEL);
  1589. var v = {
  1590. slides: slides,
  1591. destiny: destiny,
  1592. direction: direction,
  1593. destinyPos: destiny.position(),
  1594. slideIndex: destiny.index(),
  1595. section: section,
  1596. sectionIndex: section.index(SECTION_SEL),
  1597. anchorLink: section.data('anchor'),
  1598. slidesNav: section.find(SLIDES_NAV_SEL),
  1599. slideAnchor: getAnchor(destiny),
  1600. prevSlide: section.find(SLIDE_ACTIVE_SEL),
  1601. prevSlideIndex: section.find(SLIDE_ACTIVE_SEL).index(),
  1602. //caching the value of isResizing at the momment the function is called
  1603. //because it will be checked later inside a setTimeout and the value might change
  1604. localIsResizing: isResizing
  1605. };
  1606. v.xMovement = getXmovement(v.prevSlideIndex, v.slideIndex);
  1607. //important!! Only do it when not resizing
  1608. if(!v.localIsResizing){
  1609. //preventing from scrolling to the next/prev section when using scrollHorizontally
  1610. canScroll = false;
  1611. }
  1612. if(options.onSlideLeave){
  1613. //if the site is not just resizing and readjusting the slides
  1614. if(!v.localIsResizing && v.xMovement!=='none'){
  1615. if($.isFunction( options.onSlideLeave )){
  1616. if(options.onSlideLeave.call( v.prevSlide, v.anchorLink, (v.sectionIndex + 1), v.prevSlideIndex, v.xMovement, v.slideIndex ) === false){
  1617. slideMoving = false;
  1618. return;
  1619. }
  1620. }
  1621. }
  1622. }
  1623. destiny.addClass(ACTIVE).siblings().removeClass(ACTIVE);
  1624. if(!v.localIsResizing){
  1625. stopMedia(v.prevSlide);
  1626. lazyLoad(destiny);
  1627. }
  1628. if(!options.loopHorizontal && options.controlArrows){
  1629. //hidding it for the fist slide, showing for the rest
  1630. section.find(SLIDES_ARROW_PREV_SEL).toggle(v.slideIndex!==0);
  1631. //hidding it for the last slide, showing for the rest
  1632. section.find(SLIDES_ARROW_NEXT_SEL).toggle(!destiny.is(':last-child'));
  1633. }
  1634. //only changing the URL if the slides are in the current section (not for resize re-adjusting)
  1635. if(section.hasClass(ACTIVE)){
  1636. setState(v.slideIndex, v.slideAnchor, v.anchorLink, v.sectionIndex);
  1637. }
  1638. performHorizontalMove(slides, v, true);
  1639. }
  1640. function afterSlideLoads(v){
  1641. activeSlidesNavigation(v.slidesNav, v.slideIndex);
  1642. //if the site is not just resizing and readjusting the slides
  1643. if(!v.localIsResizing){
  1644. $.isFunction( options.afterSlideLoad ) && options.afterSlideLoad.call( v.destiny, v.anchorLink, (v.sectionIndex + 1), v.slideAnchor, v.slideIndex);
  1645. //needs to be inside the condition to prevent problems with continuousVertical and scrollHorizontally
  1646. //and to prevent double scroll right after a windows resize
  1647. canScroll = true;
  1648. playMedia(v.destiny);
  1649. }
  1650. //letting them slide again
  1651. slideMoving = false;
  1652. }
  1653. /**
  1654. * Performs the horizontal movement. (CSS3 or jQuery)
  1655. *
  1656. * @param fireCallback {Bool} - determines whether or not to fire the callback
  1657. */
  1658. function performHorizontalMove(slides, v, fireCallback){
  1659. var destinyPos = v.destinyPos;
  1660. if(options.css3){
  1661. var translate3d = 'translate3d(-' + Math.round(destinyPos.left) + 'px, 0px, 0px)';
  1662. addAnimation(slides.find(SLIDES_CONTAINER_SEL)).css(getTransforms(translate3d));
  1663. afterSlideLoadsId = setTimeout(function(){
  1664. fireCallback && afterSlideLoads(v);
  1665. }, options.scrollingSpeed, options.easing);
  1666. }else{
  1667. slides.animate({
  1668. scrollLeft : Math.round(destinyPos.left)
  1669. }, options.scrollingSpeed, options.easing, function() {
  1670. fireCallback && afterSlideLoads(v);
  1671. });
  1672. }
  1673. }
  1674. /**
  1675. * Sets the state for the horizontal bullet navigations.
  1676. */
  1677. function activeSlidesNavigation(slidesNav, slideIndex){
  1678. slidesNav.find(ACTIVE_SEL).removeClass(ACTIVE);
  1679. slidesNav.find('li').eq(slideIndex).find('a').addClass(ACTIVE);
  1680. }
  1681. var previousHeight = windowsHeight;
  1682. //when resizing the site, we adjust the heights of the sections, slimScroll...
  1683. function resizeHandler(){
  1684. //checking if it needs to get responsive
  1685. responsive();
  1686. // rebuild immediately on touch devices
  1687. if (isTouchDevice) {
  1688. var activeElement = $(document.activeElement);
  1689. //if the keyboard is NOT visible
  1690. if (!activeElement.is('textarea') && !activeElement.is('input') && !activeElement.is('select')) {
  1691. var currentHeight = $window.height();
  1692. //making sure the change in the viewport size is enough to force a rebuild. (20 % of the window to avoid problems when hidding scroll bars)
  1693. if( Math.abs(currentHeight - previousHeight) > (20 * Math.max(previousHeight, currentHeight) / 100) ){
  1694. reBuild(true);
  1695. previousHeight = currentHeight;
  1696. }
  1697. }
  1698. }else{
  1699. //in order to call the functions only when the resize is finished
  1700. //http://stackoverflow.com/questions/4298612/jquery-how-to-call-resize-event-only-once-its-finished-resizing
  1701. clearTimeout(resizeId);
  1702. resizeId = setTimeout(function(){
  1703. reBuild(true);
  1704. }, 350);
  1705. }
  1706. }
  1707. /**
  1708. * Checks if the site needs to get responsive and disables autoScrolling if so.
  1709. * A class `fp-responsive` is added to the plugin's container in case the user wants to use it for his own responsive CSS.
  1710. */
  1711. function responsive(){
  1712. var widthLimit = options.responsive || options.responsiveWidth; //backwards compatiblity
  1713. var heightLimit = options.responsiveHeight;
  1714. //only calculating what we need. Remember its called on the resize event.
  1715. var isBreakingPointWidth = widthLimit && $window.outerWidth() < widthLimit;
  1716. var isBreakingPointHeight = heightLimit && $window.height() < heightLimit;
  1717. if(widthLimit && heightLimit){
  1718. setResponsive(isBreakingPointWidth || isBreakingPointHeight);
  1719. }
  1720. else if(widthLimit){
  1721. setResponsive(isBreakingPointWidth);
  1722. }
  1723. else if(heightLimit){
  1724. setResponsive(isBreakingPointHeight);
  1725. }
  1726. }
  1727. /**
  1728. * Adds transition animations for the given element
  1729. */
  1730. function addAnimation(element){
  1731. var transition = 'all ' + options.scrollingSpeed + 'ms ' + options.easingcss3;
  1732. element.removeClass(NO_TRANSITION);
  1733. return element.css({
  1734. '-webkit-transition': transition,
  1735. 'transition': transition
  1736. });
  1737. }
  1738. /**
  1739. * Remove transition animations for the given element
  1740. */
  1741. function removeAnimation(element){
  1742. return element.addClass(NO_TRANSITION);
  1743. }
  1744. /**
  1745. * Activating the vertical navigation bullets according to the given slide name.
  1746. */
  1747. function activateNavDots(name, sectionIndex){
  1748. if(options.navigation){
  1749. $(SECTION_NAV_SEL).find(ACTIVE_SEL).removeClass(ACTIVE);
  1750. if(name){
  1751. $(SECTION_NAV_SEL).find('a[href="#' + name + '"]').addClass(ACTIVE);
  1752. }else{
  1753. $(SECTION_NAV_SEL).find('li').eq(sectionIndex).find('a').addClass(ACTIVE);
  1754. }
  1755. }
  1756. }
  1757. /**
  1758. * Activating the website main menu elements according to the given slide name.
  1759. */
  1760. function activateMenuElement(name){
  1761. if(options.menu){
  1762. $(options.menu).find(ACTIVE_SEL).removeClass(ACTIVE);
  1763. $(options.menu).find('[data-menuanchor="'+name+'"]').addClass(ACTIVE);
  1764. }
  1765. }
  1766. /**
  1767. * Sets to active the current menu and vertical nav items.
  1768. */
  1769. function activateMenuAndNav(anchor, index){
  1770. activateMenuElement(anchor);
  1771. activateNavDots(anchor, index);
  1772. }
  1773. /**
  1774. * Retuns `up` or `down` depending on the scrolling movement to reach its destination
  1775. * from the current section.
  1776. */
  1777. function getYmovement(destiny){
  1778. var fromIndex = $(SECTION_ACTIVE_SEL).index(SECTION_SEL);
  1779. var toIndex = destiny.index(SECTION_SEL);
  1780. if( fromIndex == toIndex){
  1781. return 'none';
  1782. }
  1783. if(fromIndex > toIndex){
  1784. return 'up';
  1785. }
  1786. return 'down';
  1787. }
  1788. /**
  1789. * Retuns `right` or `left` depending on the scrolling movement to reach its destination
  1790. * from the current slide.
  1791. */
  1792. function getXmovement(fromIndex, toIndex){
  1793. if( fromIndex == toIndex){
  1794. return 'none';
  1795. }
  1796. if(fromIndex > toIndex){
  1797. return 'left';
  1798. }
  1799. return 'right';
  1800. }
  1801. /**
  1802. * Checks if the element needs scrollbar and if the user wants to apply it.
  1803. * If so it creates it.
  1804. *
  1805. * @param {Object} element jQuery object of the section or slide
  1806. */
  1807. function createScrollBar(element){
  1808. //User doesn't want scrollbar here? Sayonara baby!
  1809. if(element.hasClass('fp-noscroll')) return;
  1810. //needed to make `scrollHeight` work under Opera 12
  1811. element.css('overflow', 'hidden');
  1812. var scrollOverflowHandler = options.scrollOverflowHandler;
  1813. var wrap = scrollOverflowHandler.wrapContent();
  1814. //in case element is a slide
  1815. var section = element.closest(SECTION_SEL);
  1816. var scrollable = scrollOverflowHandler.scrollable(element);
  1817. var contentHeight;
  1818. //if there was scroll, the contentHeight will be the one in the scrollable section
  1819. if(scrollable.length){
  1820. contentHeight = scrollOverflowHandler.scrollHeight(element);
  1821. }else{
  1822. contentHeight = element.get(0).scrollHeight;
  1823. if(options.verticalCentered){
  1824. contentHeight = element.find(TABLE_CELL_SEL).get(0).scrollHeight;
  1825. }
  1826. }
  1827. var scrollHeight = windowsHeight - parseInt(section.css('padding-bottom')) - parseInt(section.css('padding-top'));
  1828. //needs scroll?
  1829. if ( contentHeight > scrollHeight) {
  1830. //did we already have an scrollbar ? Updating it
  1831. if(scrollable.length){
  1832. scrollOverflowHandler.update(element, scrollHeight);
  1833. }
  1834. //creating the scrolling
  1835. else{
  1836. if(options.verticalCentered){
  1837. element.find(TABLE_CELL_SEL).wrapInner(wrap);
  1838. }else{
  1839. element.wrapInner(wrap);
  1840. }
  1841. scrollOverflowHandler.create(element, scrollHeight);
  1842. }
  1843. }
  1844. //removing the scrolling when it is not necessary anymore
  1845. else{
  1846. scrollOverflowHandler.remove(element);
  1847. }
  1848. //undo
  1849. element.css('overflow', '');
  1850. }
  1851. function addTableClass(element){
  1852. //In case we are styling for the 2nd time as in with reponsiveSlides
  1853. if(!element.hasClass(TABLE)){
  1854. element.addClass(TABLE).wrapInner('<div class="' + TABLE_CELL + '" style="height:' + getTableHeight(element) + 'px;" />');
  1855. }
  1856. }
  1857. function getTableHeight(element){
  1858. var sectionHeight = windowsHeight;
  1859. if(options.paddingTop || options.paddingBottom){
  1860. var section = element;
  1861. if(!section.hasClass(SECTION)){
  1862. section = element.closest(SECTION_SEL);
  1863. }
  1864. var paddings = parseInt(section.css('padding-top')) + parseInt(section.css('padding-bottom'));
  1865. sectionHeight = (windowsHeight - paddings);
  1866. }
  1867. return sectionHeight;
  1868. }
  1869. /**
  1870. * Adds a css3 transform property to the container class with or without animation depending on the animated param.
  1871. */
  1872. function transformContainer(translate3d, animated){
  1873. if(animated){
  1874. addAnimation(container);
  1875. }else{
  1876. removeAnimation(container);
  1877. }
  1878. container.css(getTransforms(translate3d));
  1879. //syncronously removing the class after the animation has been applied.
  1880. setTimeout(function(){
  1881. container.removeClass(NO_TRANSITION);
  1882. },10);
  1883. }
  1884. /**
  1885. * Gets a section by its anchor / index
  1886. */
  1887. function getSectionByAnchor(sectionAnchor){
  1888. //section
  1889. var section = container.find(SECTION_SEL + '[data-anchor="'+sectionAnchor+'"]');
  1890. if(!section.length){
  1891. section = $(SECTION_SEL).eq( (sectionAnchor -1) );
  1892. }
  1893. return section;
  1894. }
  1895. /**
  1896. * Gets a slide inside a given section by its anchor / index
  1897. */
  1898. function getSlideByAnchor(slideAnchor, section){
  1899. var slides = section.find(SLIDES_WRAPPER_SEL);
  1900. var slide = slides.find(SLIDE_SEL + '[data-anchor="'+slideAnchor+'"]');
  1901. if(!slide.length){
  1902. slide = slides.find(SLIDE_SEL).eq(slideAnchor);
  1903. }
  1904. return slide;
  1905. }
  1906. /**
  1907. * Scrolls to the given section and slide anchors
  1908. */
  1909. function scrollPageAndSlide(destiny, slide){
  1910. var section = getSectionByAnchor(destiny);
  1911. //do nothing if there's no section with the given anchor name
  1912. if(!section.length) return;
  1913. //default slide
  1914. if (typeof slide === 'undefined') {
  1915. slide = 0;
  1916. }
  1917. //we need to scroll to the section and then to the slide
  1918. if (destiny !== lastScrolledDestiny && !section.hasClass(ACTIVE)){
  1919. scrollPage(section, function(){
  1920. scrollSlider(section, slide);
  1921. });
  1922. }
  1923. //if we were already in the section
  1924. else{
  1925. scrollSlider(section, slide);
  1926. }
  1927. }
  1928. /**
  1929. * Scrolls the slider to the given slide destination for the given section
  1930. */
  1931. function scrollSlider(section, slideAnchor){
  1932. if(typeof slideAnchor !== 'undefined'){
  1933. var slides = section.find(SLIDES_WRAPPER_SEL);
  1934. var destiny = getSlideByAnchor(slideAnchor, section);
  1935. if(destiny.length){
  1936. landscapeScroll(slides, destiny);
  1937. }
  1938. }
  1939. }
  1940. /**
  1941. * Creates a landscape navigation bar with dots for horizontal sliders.
  1942. */
  1943. function addSlidesNavigation(section, numSlides){
  1944. section.append('<div class="' + SLIDES_NAV + '"><ul></ul></div>');
  1945. var nav = section.find(SLIDES_NAV_SEL);
  1946. //top or bottom
  1947. nav.addClass(options.slidesNavPosition);
  1948. for(var i=0; i< numSlides; i++){
  1949. nav.find('ul').append('<li><a href="#"><span></span></a></li>');
  1950. }
  1951. //centering it
  1952. nav.css('margin-left', '-' + (nav.width()/2) + 'px');
  1953. nav.find('li').first().find('a').addClass(ACTIVE);
  1954. }
  1955. /**
  1956. * Sets the state of the website depending on the active section/slide.
  1957. * It changes the URL hash when needed and updates the body class.
  1958. */
  1959. function setState(slideIndex, slideAnchor, anchorLink, sectionIndex){
  1960. var sectionHash = '';
  1961. if(options.anchors.length && !options.lockAnchors){
  1962. //isn't it the first slide?
  1963. if(slideIndex){
  1964. if(typeof anchorLink !== 'undefined'){
  1965. sectionHash = anchorLink;
  1966. }
  1967. //slide without anchor link? We take the index instead.
  1968. if(typeof slideAnchor === 'undefined'){
  1969. slideAnchor = slideIndex;
  1970. }
  1971. lastScrolledSlide = slideAnchor;
  1972. setUrlHash(sectionHash + '/' + slideAnchor);
  1973. //first slide won't have slide anchor, just the section one
  1974. }else if(typeof slideIndex !== 'undefined'){
  1975. lastScrolledSlide = slideAnchor;
  1976. setUrlHash(anchorLink);
  1977. }
  1978. //section without slides
  1979. else{
  1980. setUrlHash(anchorLink);
  1981. }
  1982. }
  1983. setBodyClass();
  1984. }
  1985. /**
  1986. * Sets the URL hash.
  1987. */
  1988. function setUrlHash(url){
  1989. if(options.recordHistory){
  1990. location.hash = url;
  1991. }else{
  1992. //Mobile Chrome doesn't work the normal way, so... lets use HTML5 for phones :)
  1993. if(isTouchDevice || isTouch){
  1994. window.history.replaceState(undefined, undefined, '#' + url);
  1995. }else{
  1996. var baseUrl = window.location.href.split('#')[0];
  1997. window.location.replace( baseUrl + '#' + url );
  1998. }
  1999. }
  2000. }
  2001. /**
  2002. * Gets the anchor for the given slide / section. Its index will be used if there's none.
  2003. */
  2004. function getAnchor(element){
  2005. var anchor = element.data('anchor');
  2006. var index = element.index();
  2007. //Slide without anchor link? We take the index instead.
  2008. if(typeof anchor === 'undefined'){
  2009. anchor = index;
  2010. }
  2011. return anchor;
  2012. }
  2013. /**
  2014. * Sets a class for the body of the page depending on the active section / slide
  2015. */
  2016. function setBodyClass(){
  2017. var section = $(SECTION_ACTIVE_SEL);
  2018. var slide = section.find(SLIDE_ACTIVE_SEL);
  2019. var sectionAnchor = getAnchor(section);
  2020. var slideAnchor = getAnchor(slide);
  2021. var text = String(sectionAnchor);
  2022. if(slide.length){
  2023. text = text + '-' + slideAnchor;
  2024. }
  2025. //changing slash for dash to make it a valid CSS style
  2026. text = text.replace('/', '-').replace('#','');
  2027. //removing previous anchor classes
  2028. var classRe = new RegExp('\\b\\s?' + VIEWING_PREFIX + '-[^\\s]+\\b', "g");
  2029. $body[0].className = $body[0].className.replace(classRe, '');
  2030. //adding the current anchor
  2031. $body.addClass(VIEWING_PREFIX + '-' + text);
  2032. }
  2033. /**
  2034. * Checks for translate3d support
  2035. * @return boolean
  2036. * http://stackoverflow.com/questions/5661671/detecting-transform-translate3d-support
  2037. */
  2038. function support3d() {
  2039. var el = document.createElement('p'),
  2040. has3d,
  2041. transforms = {
  2042. 'webkitTransform':'-webkit-transform',
  2043. 'OTransform':'-o-transform',
  2044. 'msTransform':'-ms-transform',
  2045. 'MozTransform':'-moz-transform',
  2046. 'transform':'transform'
  2047. };
  2048. // Add it to the body to get the computed style.
  2049. document.body.insertBefore(el, null);
  2050. for (var t in transforms) {
  2051. if (el.style[t] !== undefined) {
  2052. el.style[t] = 'translate3d(1px,1px,1px)';
  2053. has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]);
  2054. }
  2055. }
  2056. document.body.removeChild(el);
  2057. return (has3d !== undefined && has3d.length > 0 && has3d !== 'none');
  2058. }
  2059. /**
  2060. * Removes the auto scrolling action fired by the mouse wheel and trackpad.
  2061. * After this function is called, the mousewheel and trackpad movements won't scroll through sections.
  2062. */
  2063. function removeMouseWheelHandler(){
  2064. if (document.addEventListener) {
  2065. document.removeEventListener('mousewheel', MouseWheelHandler, false); //IE9, Chrome, Safari, Oper
  2066. document.removeEventListener('wheel', MouseWheelHandler, false); //Firefox
  2067. document.removeEventListener('MozMousePixelScroll', MouseWheelHandler, false); //old Firefox
  2068. } else {
  2069. document.detachEvent('onmousewheel', MouseWheelHandler); //IE 6/7/8
  2070. }
  2071. }
  2072. /**
  2073. * Adds the auto scrolling action for the mouse wheel and trackpad.
  2074. * After this function is called, the mousewheel and trackpad movements will scroll through sections
  2075. * https://developer.mozilla.org/en-US/docs/Web/Events/wheel
  2076. */
  2077. function addMouseWheelHandler(){
  2078. var prefix = '';
  2079. var _addEventListener;
  2080. if (window.addEventListener){
  2081. _addEventListener = "addEventListener";
  2082. }else{
  2083. _addEventListener = "attachEvent";
  2084. prefix = 'on';
  2085. }
  2086. // detect available wheel event
  2087. var support = 'onwheel' in document.createElement('div') ? 'wheel' : // Modern browsers support "wheel"
  2088. document.onmousewheel !== undefined ? 'mousewheel' : // Webkit and IE support at least "mousewheel"
  2089. 'DOMMouseScroll'; // let's assume that remaining browsers are older Firefox
  2090. if(support == 'DOMMouseScroll'){
  2091. document[ _addEventListener ](prefix + 'MozMousePixelScroll', MouseWheelHandler, false);
  2092. }
  2093. //handle MozMousePixelScroll in older Firefox
  2094. else{
  2095. document[ _addEventListener ](prefix + support, MouseWheelHandler, false);
  2096. }
  2097. }
  2098. /**
  2099. * Binding the mousemove when the mouse's middle button is pressed
  2100. */
  2101. function addMiddleWheelHandler(){
  2102. container
  2103. .on('mousedown', mouseDownHandler)
  2104. .on('mouseup', mouseUpHandler);
  2105. }
  2106. /**
  2107. * Unbinding the mousemove when the mouse's middle button is released
  2108. */
  2109. function removeMiddleWheelHandler(){
  2110. container
  2111. .off('mousedown', mouseDownHandler)
  2112. .off('mouseup', mouseUpHandler);
  2113. }
  2114. /**
  2115. * Adds the possibility to auto scroll through sections on touch devices.
  2116. */
  2117. function addTouchHandler(){
  2118. if(isTouchDevice || isTouch){
  2119. //Microsoft pointers
  2120. var MSPointer = getMSPointer();
  2121. if(options.autoScrolling){
  2122. $body.off('touchmove ' + MSPointer.move).on('touchmove ' + MSPointer.move, preventBouncing);
  2123. }
  2124. $(WRAPPER_SEL)
  2125. .off('touchstart ' + MSPointer.down).on('touchstart ' + MSPointer.down, touchStartHandler)
  2126. .off('touchmove ' + MSPointer.move).on('touchmove ' + MSPointer.move, touchMoveHandler);
  2127. }
  2128. }
  2129. /**
  2130. * Removes the auto scrolling for touch devices.
  2131. */
  2132. function removeTouchHandler(){
  2133. if(isTouchDevice || isTouch){
  2134. //Microsoft pointers
  2135. var MSPointer = getMSPointer();
  2136. $(WRAPPER_SEL)
  2137. .off('touchstart ' + MSPointer.down)
  2138. .off('touchmove ' + MSPointer.move);
  2139. }
  2140. }
  2141. /*
  2142. * Returns and object with Microsoft pointers (for IE<11 and for IE >= 11)
  2143. * http://msdn.microsoft.com/en-us/library/ie/dn304886(v=vs.85).aspx
  2144. */
  2145. function getMSPointer(){
  2146. var pointer;
  2147. //IE >= 11 & rest of browsers
  2148. if(window.PointerEvent){
  2149. pointer = { down: 'pointerdown', move: 'pointermove'};
  2150. }
  2151. //IE < 11
  2152. else{
  2153. pointer = { down: 'MSPointerDown', move: 'MSPointerMove'};
  2154. }
  2155. return pointer;
  2156. }
  2157. /**
  2158. * Gets the pageX and pageY properties depending on the browser.
  2159. * https://github.com/alvarotrigo/fullPage.js/issues/194#issuecomment-34069854
  2160. */
  2161. function getEventsPage(e){
  2162. var events = [];
  2163. events.y = (typeof e.pageY !== 'undefined' && (e.pageY || e.pageX) ? e.pageY : e.touches[0].pageY);
  2164. events.x = (typeof e.pageX !== 'undefined' && (e.pageY || e.pageX) ? e.pageX : e.touches[0].pageX);
  2165. //in touch devices with scrollBar:true, e.pageY is detected, but we have to deal with touch events. #1008
  2166. if(isTouch && isReallyTouch(e) && options.scrollBar){
  2167. events.y = e.touches[0].pageY;
  2168. events.x = e.touches[0].pageX;
  2169. }
  2170. return events;
  2171. }
  2172. /**
  2173. * Slides silently (with no animation) the active slider to the given slide.
  2174. * @param noCallback {bool} true or defined -> no callbacks
  2175. */
  2176. function silentLandscapeScroll(activeSlide, noCallbacks){
  2177. setScrollingSpeed (0, 'internal');
  2178. if(typeof noCallbacks !== 'undefined'){
  2179. //preventing firing callbacks afterSlideLoad etc.
  2180. isResizing = true;
  2181. }
  2182. landscapeScroll(activeSlide.closest(SLIDES_WRAPPER_SEL), activeSlide);
  2183. if(typeof noCallbacks !== 'undefined'){
  2184. isResizing = false;
  2185. }
  2186. setScrollingSpeed(originals.scrollingSpeed, 'internal');
  2187. }
  2188. /**
  2189. * Scrolls silently (with no animation) the page to the given Y position.
  2190. */
  2191. function silentScroll(top){
  2192. // The first section can have a negative value in iOS 10. Not quite sure why: -0.0142822265625
  2193. // that's why we round it to 0.
  2194. var roundedTop = Math.round(top);
  2195. if (options.css3 && options.autoScrolling && !options.scrollBar){
  2196. var translate3d = 'translate3d(0px, -' + roundedTop + 'px, 0px)';
  2197. transformContainer(translate3d, false);
  2198. }
  2199. else if(options.autoScrolling && !options.scrollBar){
  2200. container.css('top', -roundedTop);
  2201. }
  2202. else{
  2203. $htmlBody.scrollTop(roundedTop);
  2204. }
  2205. }
  2206. /**
  2207. * Returns the cross-browser transform string.
  2208. */
  2209. function getTransforms(translate3d){
  2210. return {
  2211. '-webkit-transform': translate3d,
  2212. '-moz-transform': translate3d,
  2213. '-ms-transform':translate3d,
  2214. 'transform': translate3d
  2215. };
  2216. }
  2217. /**
  2218. * Allowing or disallowing the mouse/swipe scroll in a given direction. (not for keyboard)
  2219. * @type m (mouse) or k (keyboard)
  2220. */
  2221. function setIsScrollAllowed(value, direction, type){
  2222. switch (direction){
  2223. case 'up': isScrollAllowed[type].up = value; break;
  2224. case 'down': isScrollAllowed[type].down = value; break;
  2225. case 'left': isScrollAllowed[type].left = value; break;
  2226. case 'right': isScrollAllowed[type].right = value; break;
  2227. case 'all':
  2228. if(type == 'm'){
  2229. setAllowScrolling(value);
  2230. }else{
  2231. setKeyboardScrolling(value);
  2232. }
  2233. }
  2234. }
  2235. /*
  2236. * Destroys fullpage.js plugin events and optinally its html markup and styles
  2237. */
  2238. function destroy(all){
  2239. setAutoScrolling(false, 'internal');
  2240. setAllowScrolling(false);
  2241. setKeyboardScrolling(false);
  2242. container.addClass(DESTROYED);
  2243. clearTimeout(afterSlideLoadsId);
  2244. clearTimeout(afterSectionLoadsId);
  2245. clearTimeout(resizeId);
  2246. clearTimeout(scrollId);
  2247. clearTimeout(scrollId2);
  2248. $window
  2249. .off('scroll', scrollHandler)
  2250. .off('hashchange', hashChangeHandler)
  2251. .off('resize', resizeHandler);
  2252. $document
  2253. .off('click touchstart', SECTION_NAV_SEL + ' a')
  2254. .off('mouseenter', SECTION_NAV_SEL + ' li')
  2255. .off('mouseleave', SECTION_NAV_SEL + ' li')
  2256. .off('click touchstart', SLIDES_NAV_LINK_SEL)
  2257. .off('mouseover', options.normalScrollElements)
  2258. .off('mouseout', options.normalScrollElements);
  2259. $(SECTION_SEL)
  2260. .off('click touchstart', SLIDES_ARROW_SEL);
  2261. clearTimeout(afterSlideLoadsId);
  2262. clearTimeout(afterSectionLoadsId);
  2263. //lets make a mess!
  2264. if(all){
  2265. destroyStructure();
  2266. }
  2267. }
  2268. /*
  2269. * Removes inline styles added by fullpage.js
  2270. */
  2271. function destroyStructure(){
  2272. //reseting the `top` or `translate` properties to 0
  2273. silentScroll(0);
  2274. //loading all the lazy load content
  2275. container.find('img[data-src], source[data-src], audio[data-src], iframe[data-src]').each(function(){
  2276. $(this).attr('src', $(this).data('src'));
  2277. $(this).removeAttr('data-src');
  2278. });
  2279. $(SECTION_NAV_SEL + ', ' + SLIDES_NAV_SEL + ', ' + SLIDES_ARROW_SEL).remove();
  2280. //removing inline styles
  2281. $(SECTION_SEL).css( {
  2282. 'height': '',
  2283. 'background-color' : '',
  2284. 'padding': ''
  2285. });
  2286. $(SLIDE_SEL).css( {
  2287. 'width': ''
  2288. });
  2289. container.css({
  2290. 'height': '',
  2291. 'position': '',
  2292. '-ms-touch-action': '',
  2293. 'touch-action': ''
  2294. });
  2295. $htmlBody.css({
  2296. 'overflow': '',
  2297. 'height': ''
  2298. });
  2299. // remove .fp-enabled class
  2300. $('html').removeClass(ENABLED);
  2301. // remove .fp-responsive class
  2302. $body.removeClass(RESPONSIVE);
  2303. // remove all of the .fp-viewing- classes
  2304. $.each($body.get(0).className.split(/\s+/), function (index, className) {
  2305. if (className.indexOf(VIEWING_PREFIX) === 0) {
  2306. $body.removeClass(className);
  2307. }
  2308. });
  2309. //removing added classes
  2310. $(SECTION_SEL + ', ' + SLIDE_SEL).each(function(){
  2311. options.scrollOverflowHandler.remove($(this));
  2312. $(this).removeClass(TABLE + ' ' + ACTIVE);
  2313. });
  2314. removeAnimation(container);
  2315. //Unwrapping content
  2316. container.find(TABLE_CELL_SEL + ', ' + SLIDES_CONTAINER_SEL + ', ' + SLIDES_WRAPPER_SEL).each(function(){
  2317. //unwrap not being use in case there's no child element inside and its just text
  2318. $(this).replaceWith(this.childNodes);
  2319. });
  2320. //scrolling the page to the top with no animation
  2321. $htmlBody.scrollTop(0);
  2322. //removing selectors
  2323. var usedSelectors = [SECTION, SLIDE, SLIDES_CONTAINER];
  2324. $.each(usedSelectors, function(index, value){
  2325. $('.' + value).removeClass(value);
  2326. });
  2327. }
  2328. /*
  2329. * Sets the state for a variable with multiple states (original, and temporal)
  2330. * Some variables such as `autoScrolling` or `recordHistory` might change automatically its state when using `responsive` or `autoScrolling:false`.
  2331. * This function is used to keep track of both states, the original and the temporal one.
  2332. * If type is not 'internal', then we assume the user is globally changing the variable.
  2333. */
  2334. function setVariableState(variable, value, type){
  2335. options[variable] = value;
  2336. if(type !== 'internal'){
  2337. originals[variable] = value;
  2338. }
  2339. }
  2340. /**
  2341. * Displays warnings
  2342. */
  2343. function displayWarnings(){
  2344. var extensions = ['fadingEffect', 'continuousHorizontal', 'scrollHorizontally', 'interlockedSlides', 'resetSliders', 'responsiveSlides', 'offsetSections', 'dragAndMove', 'scrollOverflowReset'];
  2345. if($('html').hasClass(ENABLED)){
  2346. showError('error', 'Fullpage.js can only be initialized once and you are doing it multiple times!');
  2347. return;
  2348. }
  2349. // Disable mutually exclusive settings
  2350. if (options.continuousVertical &&
  2351. (options.loopTop || options.loopBottom)) {
  2352. options.continuousVertical = false;
  2353. showError('warn', 'Option `loopTop/loopBottom` is mutually exclusive with `continuousVertical`; `continuousVertical` disabled');
  2354. }
  2355. if(options.scrollBar && options.scrollOverflow){
  2356. showError('warn', 'Option `scrollBar` is mutually exclusive with `scrollOverflow`. Sections with scrollOverflow might not work well in Firefox');
  2357. }
  2358. if(options.continuousVertical && (options.scrollBar || !options.autoScrolling)){
  2359. options.continuousVertical = false;
  2360. showError('warn', 'Scroll bars (`scrollBar:true` or `autoScrolling:false`) are mutually exclusive with `continuousVertical`; `continuousVertical` disabled');
  2361. }
  2362. //using extensions? Wrong file!
  2363. $.each(extensions, function(index, extension){
  2364. //is the option set to true?
  2365. if(options[extension]){
  2366. showError('warn', 'fullpage.js extensions require jquery.fullpage.extensions.min.js file instead of the usual jquery.fullpage.js. Requested: '+ extension);
  2367. }
  2368. });
  2369. //anchors can not have the same value as any element ID or NAME
  2370. $.each(options.anchors, function(index, name){
  2371. //case insensitive selectors (http://stackoverflow.com/a/19465187/1081396)
  2372. var nameAttr = $document.find('[name]').filter(function() {
  2373. return $(this).attr('name') && $(this).attr('name').toLowerCase() == name.toLowerCase();
  2374. });
  2375. var idAttr = $document.find('[id]').filter(function() {
  2376. return $(this).attr('id') && $(this).attr('id').toLowerCase() == name.toLowerCase();
  2377. });
  2378. if(idAttr.length || nameAttr.length ){
  2379. showError('error', 'data-anchor tags can not have the same value as any `id` element on the site (or `name` element for IE).');
  2380. idAttr.length && showError('error', '"' + name + '" is is being used by another element `id` property');
  2381. nameAttr.length && showError('error', '"' + name + '" is is being used by another element `name` property');
  2382. }
  2383. });
  2384. }
  2385. /**
  2386. * Shows a message in the console of the given type.
  2387. */
  2388. function showError(type, text){
  2389. console && console[type] && console[type]('fullPage: ' + text);
  2390. }
  2391. }; //end of $.fn.fullpage
  2392. if(typeof IScroll !== 'undefined'){
  2393. /*
  2394. * Turns iScroll `mousewheel` option off dynamically
  2395. * https://github.com/cubiq/iscroll/issues/1036
  2396. */
  2397. IScroll.prototype.wheelOn = function () {
  2398. this.wrapper.addEventListener('wheel', this);
  2399. this.wrapper.addEventListener('mousewheel', this);
  2400. this.wrapper.addEventListener('DOMMouseScroll', this);
  2401. };
  2402. /*
  2403. * Turns iScroll `mousewheel` option on dynamically
  2404. * https://github.com/cubiq/iscroll/issues/1036
  2405. */
  2406. IScroll.prototype.wheelOff = function () {
  2407. this.wrapper.removeEventListener('wheel', this);
  2408. this.wrapper.removeEventListener('mousewheel', this);
  2409. this.wrapper.removeEventListener('DOMMouseScroll', this);
  2410. };
  2411. }
  2412. /**
  2413. * An object to handle overflow scrolling.
  2414. * This uses jquery.slimScroll to accomplish overflow scrolling.
  2415. * It is possible to pass in an alternate scrollOverflowHandler
  2416. * to the fullpage.js option that implements the same functions
  2417. * as this handler.
  2418. *
  2419. * @type {Object}
  2420. */
  2421. var iscrollHandler = {
  2422. refreshId: null,
  2423. iScrollInstances: [],
  2424. // Enables or disables the mouse wheel for the active section or all slides in it
  2425. toggleWheel: function(value){
  2426. var scrollable = $(SECTION_ACTIVE_SEL).find(SCROLLABLE_SEL);
  2427. scrollable.each(function(){
  2428. var iScrollInstance = $(this).data('iscrollInstance');
  2429. if(typeof iScrollInstance !== 'undefined' && iScrollInstance){
  2430. if(value){
  2431. iScrollInstance.wheelOn();
  2432. }
  2433. else{
  2434. iScrollInstance.wheelOff();
  2435. }
  2436. }
  2437. });
  2438. },
  2439. /**
  2440. * Turns off iScroll for the destination section.
  2441. * When scrolling very fast on some trackpads (and Apple laptops) the inertial scrolling would
  2442. * scroll the destination section/slide before the sections animations ends.
  2443. */
  2444. onLeave: function(){
  2445. iscrollHandler.toggleWheel(false);
  2446. },
  2447. // Turns off iScroll for the leaving section
  2448. beforeLeave: function(){
  2449. iscrollHandler.onLeave()
  2450. },
  2451. // Turns on iScroll on section load
  2452. afterLoad: function(){
  2453. iscrollHandler.toggleWheel(true);
  2454. },
  2455. /**
  2456. * Called when overflow scrolling is needed for a section.
  2457. *
  2458. * @param {Object} element jQuery object containing current section
  2459. * @param {Number} scrollHeight Current window height in pixels
  2460. */
  2461. create: function(element, scrollHeight) {
  2462. var scrollable = element.find(SCROLLABLE_SEL);
  2463. scrollable.height(scrollHeight);
  2464. scrollable.each(function() {
  2465. var $this = $(this);
  2466. var iScrollInstance = $this.data('iscrollInstance');
  2467. if (iScrollInstance) {
  2468. $.each(iscrollHandler.iScrollInstances, function(){
  2469. $(this).destroy();
  2470. });
  2471. }
  2472. iScrollInstance = new IScroll($this.get(0), iscrollOptions);
  2473. iscrollHandler.iScrollInstances.push(iScrollInstance);
  2474. //off by default until the section gets active
  2475. iScrollInstance.wheelOff();
  2476. $this.data('iscrollInstance', iScrollInstance);
  2477. });
  2478. },
  2479. /**
  2480. * Return a boolean depending on whether the scrollable element is a
  2481. * the end or at the start of the scrolling depending on the given type.
  2482. *
  2483. * @param {String} type Either 'top' or 'bottom'
  2484. * @param {Object} scrollable jQuery object for the scrollable element
  2485. * @return {Boolean}
  2486. */
  2487. isScrolled: function(type, scrollable) {
  2488. var scroller = scrollable.data('iscrollInstance');
  2489. //no scroller?
  2490. if (!scroller) {
  2491. return true;
  2492. }
  2493. if (type === 'top') {
  2494. return scroller.y >= 0 && !scrollable.scrollTop();
  2495. } else if (type === 'bottom') {
  2496. return (0 - scroller.y) + scrollable.scrollTop() + 1 + scrollable.innerHeight() >= scrollable[0].scrollHeight;
  2497. }
  2498. },
  2499. /**
  2500. * Returns the scrollable element for the given section.
  2501. * If there are landscape slides, will only return a scrollable element
  2502. * if it is in the active slide.
  2503. *
  2504. * @param {Object} activeSection jQuery object containing current section
  2505. * @return {Boolean}
  2506. */
  2507. scrollable: function(activeSection){
  2508. // if there are landscape slides, we check if the scrolling bar is in the current one or not
  2509. if (activeSection.find(SLIDES_WRAPPER_SEL).length) {
  2510. return activeSection.find(SLIDE_ACTIVE_SEL).find(SCROLLABLE_SEL);
  2511. }
  2512. return activeSection.find(SCROLLABLE_SEL);
  2513. },
  2514. /**
  2515. * Returns the scroll height of the wrapped content.
  2516. * If this is larger than the window height minus section padding,
  2517. * overflow scrolling is needed.
  2518. *
  2519. * @param {Object} element jQuery object containing current section
  2520. * @return {Number}
  2521. */
  2522. scrollHeight: function(element) {
  2523. return element.find(SCROLLABLE_SEL).children().first().get(0).scrollHeight;
  2524. },
  2525. /**
  2526. * Called when overflow scrolling is no longer needed for a section.
  2527. *
  2528. * @param {Object} element jQuery object containing current section
  2529. */
  2530. remove: function(element) {
  2531. var scrollable = element.find(SCROLLABLE_SEL);
  2532. if (scrollable.length) {
  2533. var iScrollInstance = scrollable.data('iscrollInstance');
  2534. iScrollInstance.destroy();
  2535. scrollable.data('iscrollInstance', null);
  2536. }
  2537. element.find(SCROLLABLE_SEL).children().first().children().first().unwrap().unwrap();
  2538. },
  2539. /**
  2540. * Called when overflow scrolling has already been setup but the
  2541. * window height has potentially changed.
  2542. *
  2543. * @param {Object} element jQuery object containing current section
  2544. * @param {Number} scrollHeight Current window height in pixels
  2545. */
  2546. update: function(element, scrollHeight) {
  2547. //using a timeout in order to execute the refresh function only once when `update` is called multiple times in a
  2548. //short period of time.
  2549. //it also comes on handy because iScroll requires the use of timeout when using `refresh`.
  2550. clearTimeout(iscrollHandler.refreshId);
  2551. iscrollHandler.refreshId = setTimeout(function(){
  2552. $.each(iscrollHandler.iScrollInstances, function(){
  2553. $(this).get(0).refresh();
  2554. });
  2555. }, 150);
  2556. //updating the wrappers height
  2557. element.find(SCROLLABLE_SEL).css('height', scrollHeight + 'px').parent().css('height', scrollHeight + 'px');
  2558. },
  2559. /**
  2560. * Called to get any additional elements needed to wrap the section
  2561. * content in order to facilitate overflow scrolling.
  2562. *
  2563. * @return {String|Object} Can be a string containing HTML,
  2564. * a DOM element, or jQuery object.
  2565. */
  2566. wrapContent: function() {
  2567. return '<div class="' + SCROLLABLE + '"><div class="fp-scroller"></div></div>';
  2568. }
  2569. };
  2570. });