Path: blob/main/website/GAUSS/js/jquery.scrollmagic.js
2941 views
/*1ScrollMagic v1.3.02The jQuery plugin for doing magical scroll interactions.3(c) 2014 Jan Paepke (@janpaepke)4License & Info: http://janpaepke.github.io/ScrollMagic56Inspired by and partially based on SUPERSCROLLORAMA by John Polacek (@johnpolacek)7http://johnpolacek.github.com/superscrollorama/89Powered by the Greensock Tweening Platform (GSAP): http://www.greensock.com/js10Greensock License info at http://www.greensock.com/licensing/11*/12/**13@overview ##Info14@version 1.3.015@license Dual licensed under MIT license and GPL.16@author Jan Paepke - [email protected]1718@todo: enhancement: remove dependencies and move to plugins -> 2.019@todo: bug: when cascading pins (pinning one element multiple times) and later removing them without reset, positioning errors occur.20@todo: bug: having multiple scroll directions with cascaded pins doesn't work (one scroll vertical, one horizontal)21@todo: feature: optimize performance on debug plugin (huge drawbacks, when using many scenes)22*/23(function (root, factory) {24if (typeof define === 'function' && define.amd) {25// AMD. Register as an anonymous module.26define(['jquery', 'TweenMax', 'TimelineMax'], factory);27} else {28// Browser globals29var sm = factory(root.jQuery, root.TweenMax, root.TimelineMax);30root.ScrollMagic = sm.Controller;31root.ScrollScene = sm.Scene;32}33}(this, function ($, TweenMax, TimelineMax) {3435/**36* The main class that is needed once per scroll container.37*38* @class39* @global40*41* @example42* // basic initialization43* var controller = new ScrollMagic();44*45* // passing options46* var controller = new ScrollMagic({container: "#myContainer", loglevel: 3});47*48* @param {object} [options] - An object containing one or more options for the controller.49* @param {(string|object)} [options.container=window] - A selector, DOM object or a jQuery object that references the main container for scrolling.50* @param {boolean} [options.vertical=true] - Sets the scroll mode to vertical (`true`) or horizontal (`false`) scrolling.51* @param {object} [options.globalSceneOptions={}] - These options will be passed to every Scene that is added to the controller using the addScene method. For more information on Scene options see {@link ScrollScene}.52* @param {number} [options.loglevel=2] Loglevel for debugging. Note that logging is disabled in the minified version of ScrollMagic.53** `0` => silent54** `1` => errors55** `2` => errors, warnings56** `3` => errors, warnings, debuginfo57* @param {boolean} [options.refreshInterval=100] - Some changes don't call events by default, like changing the container size or moving a scene trigger element.58This interval polls these parameters to fire the necessary events.59If you don't use custom containers, trigger elements or have static layouts, where the positions of the trigger elements don't change, you can set this to 0 disable interval checking and improve performance.60*61*/62var ScrollMagic = function(options) {6364/*65* ----------------------------------------------------------------66* settings67* ----------------------------------------------------------------68*/69var70NAMESPACE = "ScrollMagic",71DEFAULT_OPTIONS = {72container: window,73vertical: true,74globalSceneOptions: {},75loglevel: 2,76refreshInterval: 10077};7879/*80* ----------------------------------------------------------------81* private vars82* ----------------------------------------------------------------83*/8485var86ScrollMagic = this,87_options = $.extend({}, DEFAULT_OPTIONS, options),88_sceneObjects = [],89_updateScenesOnNextCycle = false, // can be boolean (true => all scenes) or an array of scenes to be updated90_scrollPos = 0,91_scrollDirection = "PAUSED",92_isDocument = true,93_viewPortSize = 0,94_enabled = true,95_updateCycle,96_refreshInterval;9798/*99* ----------------------------------------------------------------100* private functions101* ----------------------------------------------------------------102*/103104/**105* Internal constructor function of ScrollMagic106* @private107*/108var construct = function () {109ScrollMagic.version = ScrollMagic.constructor.version;110$.each(_options, function (key, value) {111if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {112log(2, "WARNING: Unknown option \"" + key + "\"");113delete _options[key];114}115});116_options.container = $(_options.container).first();117// check ScrollContainer118if (_options.container.length === 0) {119log(1, "ERROR creating object " + NAMESPACE + ": No valid scroll container supplied");120throw NAMESPACE + " init failed."; // cancel121}122_isDocument = !$.contains(document, _options.container.get(0));123// prevent bubbling of fake resize event to window124if (!_isDocument) {125_options.container.on('resize', function ( e ) {126e.stopPropagation();127});128}129// update container size immediately130_viewPortSize = _options.vertical ? _options.container.height() : _options.container.width();131// set event handlers132_options.container.on("scroll resize", onChange);133134_options.refreshInterval = parseInt(_options.refreshInterval);135if (_options.refreshInterval > 0) {136_refreshInterval = window.setInterval(refresh, _options.refreshInterval);137}138139// start checking for changes140_updateCycle = animationFrameCallback(updateScenes);141log(3, "added new " + NAMESPACE + " controller (v" + ScrollMagic.version + ")");142};143144/**145* Default function to get scroll pos - overwriteable using `ScrollMagic.scrollPos(newFunction)`146* @private147*/148var getScrollPos = function () {149return _options.vertical ? _options.container.scrollTop() : _options.container.scrollLeft();150};151/**152* Default function to set scroll pos - overwriteable using `ScrollMagic.scrollTo(newFunction)`153* @private154*/155var setScrollPos = function (pos) {156if (_options.vertical) {157_options.container.scrollTop(pos);158} else {159_options.container.scrollLeft(pos);160}161};162163/**164* Handle updates in cycles instead of on scroll (performance)165* @private166*/167var updateScenes = function () {168_updateCycle = animationFrameCallback(updateScenes);169if (_enabled && _updateScenesOnNextCycle) {170var171scenesToUpdate = $.isArray(_updateScenesOnNextCycle) ? _updateScenesOnNextCycle : _sceneObjects.slice(0),172oldScrollPos = _scrollPos;173// update scroll pos & direction174_scrollPos = ScrollMagic.scrollPos();175var deltaScroll = _scrollPos - oldScrollPos;176_scrollDirection = (deltaScroll === 0) ? "PAUSED" : (deltaScroll > 0) ? "FORWARD" : "REVERSE";177if (deltaScroll < 0) { // reverse order if scrolling reverse178scenesToUpdate.reverse();179}180// update scenes181$.each(scenesToUpdate, function (index, scene) {182log(3, "updating Scene " + (index + 1) + "/" + scenesToUpdate.length + " (" + _sceneObjects.length + " total)");183scene.update(true);184});185if (scenesToUpdate.length === 0 && _options.loglevel >= 3) {186log(3, "updating 0 Scenes (nothing added to controller)");187}188_updateScenesOnNextCycle = false;189}190};191192/**193* Handles Container changes194* @private195*/196var onChange = function (e) {197if (e.type == "resize") {198_viewPortSize = _options.vertical ? _options.container.height() : _options.container.width();199}200_updateScenesOnNextCycle = true;201};202203var refresh = function () {204if (!_isDocument) {205// simulate resize event. Only works for viewport relevant param206if (_viewPortSize != (_options.vertical ? _options.container.height() : _options.container.width())) {207_options.container.trigger("resize");208}209}210$.each(_sceneObjects, function (index, scene) {// refresh all scenes211scene.refresh();212});213};214215/**216* Send a debug message to the console.217* @private218*219* @param {number} loglevel - The loglevel required to initiate output for the message.220* @param {...mixed} output - One or more variables that should be passed to the console.221*/222var log = function (loglevel, output) {223if (_options.loglevel >= loglevel) {224var225prefix = "(" + NAMESPACE + ") ->",226args = Array.prototype.splice.call(arguments, 1);227args.unshift(loglevel, prefix);228debug.apply(window, args);229}230};231232/**233* Sort scenes in ascending order of their start offset.234* @private235*236* @param {array} ScrollScenesArray - an array of ScrollScenes that should be sorted237* @return {array} The sorted array of ScrollScenes.238*/239var sortScenes = function (ScrollScenesArray) {240if (ScrollScenesArray.length <= 1) {241return ScrollScenesArray;242} else {243var scenes = ScrollScenesArray.slice(0);244scenes.sort(function(a, b) {245return a.scrollOffset() > b.scrollOffset() ? 1 : -1;246});247return scenes;248}249};250251/*252* ----------------------------------------------------------------253* public functions254* ----------------------------------------------------------------255*/256257/**258* Add one ore more scene(s) to the controller.259* This is the equivalent to `ScrollScene.addTo(controller)`.260* @public261* @example262* // with a previously defined scene263* controller.addScene(scene);264*265* // with a newly created scene.266* controller.addScene(new ScrollScene({duration : 0}));267*268* // adding multiple scenes269* controller.addScene([scene, scene2, new ScrollScene({duration : 0})]);270*271* @param {(ScrollScene|array)} ScrollScene - ScrollScene or Array of ScrollScenes to be added to the controller.272* @return {ScrollMagic} Parent object for chaining.273*/274this.addScene = function (newScene) {275if ($.isArray(newScene)) {276$.each(newScene, function (index, scene) {277ScrollMagic.addScene(scene);278});279} else if (newScene instanceof ScrollScene) {280if (newScene.parent() != ScrollMagic) {281newScene.addTo(ScrollMagic);282} else if ($.inArray(newScene, _sceneObjects) < 0){283// new scene284_sceneObjects.push(newScene); // add to array285_sceneObjects = sortScenes(_sceneObjects); // sort286newScene.on("shift." + NAMESPACE + "_sort", function() { // resort whenever scene moves287_sceneObjects = sortScenes(_sceneObjects);288});289// insert Global defaults.290$.each(_options.globalSceneOptions, function (key, value) {291if (newScene[key]) {292newScene[key].call(newScene, value);293}294});295log(3, "added Scene (" + _sceneObjects.length + " total)");296}297} else {298log(1, "ERROR: invalid argument supplied for '.addScene()'");299}300return ScrollMagic;301};302303/**304* Remove one ore more scene(s) from the controller.305* This is the equivalent to `ScrollScene.remove()`.306* @public307* @example308* // remove a scene from the controller309* controller.removeScene(scene);310*311* // remove multiple scenes from the controller312* controller.removeScene([scene, scene2, scene3]);313*314* @param {(ScrollScene|array)} ScrollScene - ScrollScene or Array of ScrollScenes to be removed from the controller.315* @returns {ScrollMagic} Parent object for chaining.316*/317this.removeScene = function (ScrollScene) {318if ($.isArray(ScrollScene)) {319$.each(ScrollScene, function (index, scene) {320ScrollMagic.removeScene(scene);321});322} else {323var index = $.inArray(ScrollScene, _sceneObjects);324if (index > -1) {325ScrollScene.off("shift." + NAMESPACE + "_sort");326_sceneObjects.splice(index, 1);327ScrollScene.remove();328log(3, "removed Scene (" + _sceneObjects.length + " total)");329}330}331return ScrollMagic;332};333334/**335* Update one ore more scene(s) according to the scroll position of the container.336* This is the equivalent to `ScrollScene.update()`.337* The update method calculates the scene's start and end position (based on the trigger element, trigger hook, duration and offset) and checks it against the current scroll position of the container.338* It then updates the current scene state accordingly (or does nothing, if the state is already correct) – Pins will be set to their correct position and tweens will be updated to their correct progress.339* _**Note:** This method gets called constantly whenever ScrollMagic detects a change. The only application for you is if you change something outside of the realm of ScrollMagic, like moving the trigger or changing tween parameters._340* @public341* @example342* // update a specific scene on next cycle343* controller.updateScene(scene);344*345* // update a specific scene immediately346* controller.updateScene(scene, true);347*348* // update multiple scenes scene on next cycle349* controller.updateScene([scene1, scene2, scene3]);350*351* @param {ScrollScene} ScrollScene - ScrollScene or Array of ScrollScenes that is/are supposed to be updated.352* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next update cycle.353This is useful when changing multiple properties of the scene - this way it will only be updated once all new properties are set (updateScenes).354* @return {ScrollMagic} Parent object for chaining.355*/356this.updateScene = function (ScrollScene, immediately) {357if ($.isArray(ScrollScene)) {358$.each(ScrollScene, function (index, scene) {359ScrollMagic.updateScene(scene, immediately);360});361} else {362if (immediately) {363ScrollScene.update(true);364} else {365// prep array for next update cycle366if (!$.isArray(_updateScenesOnNextCycle)) {367_updateScenesOnNextCycle = [];368}369if ($.inArray(ScrollScene, _updateScenesOnNextCycle) == -1) {370_updateScenesOnNextCycle.push(ScrollScene);371}372_updateScenesOnNextCycle = sortScenes(_updateScenesOnNextCycle); // sort373}374}375return ScrollMagic;376};377378/**379* Updates the controller params and calls updateScene on every scene, that is attached to the controller.380* See `ScrollMagic.updateScene()` for more information about what this means.381* In most cases you will not need this function, as it is called constantly, whenever ScrollMagic detects a state change event, like resize or scroll.382* The only application for this method is when ScrollMagic fails to detect these events.383* One application is with some external scroll libraries (like iScroll) that move an internal container to a negative offset instead of actually scrolling. In this case the update on the controller needs to be called whenever the child container's position changes.384* For this case there will also be the need to provide a custom function to calculate the correct scroll position. See `ScrollMagic.scrollPos()` for details.385* @public386* @example387* // update the controller on next cycle (saves performance due to elimination of redundant updates)388* controller.update();389*390* // update the controller immediately391* controller.update(true);392*393* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next update cycle (better performance)394* @return {ScrollMagic} Parent object for chaining.395*/396this.update = function (immediately) {397onChange({type: "resize"}); // will update size and set _updateScenesOnNextCycle to true398if (immediately) {399updateScenes();400}401return ScrollMagic;402};403404/**405* Scroll to a numeric scroll offset, a DOM element, the start of a scene or provide an alternate method for scrolling.406* For vertical controllers it will change the top scroll offset and for horizontal applications it will change the left offset.407* @public408*409* @since 1.1.0410* @example411* // scroll to an offset of 100412* controller.scrollTo(100);413*414* // scroll to a DOM element415* controller.scrollTo("#anchor");416*417* // scroll to the beginning of a scene418* var scene = new ScrollScene({offset: 200});419* controller.scrollTo(scene);420*421* // define a new scroll position modification function (animate instead of jump)422* controller.scrollTo(function (newScrollPos) {423* $("body").animate({scrollTop: newScrollPos});424* });425*426* @param {mixed} [scrollTarget] - The supplied argument can be one of these types:427* 1. `number` -> The container will scroll to this new scroll offset.428* 2. `string` or `object` -> Can be a selector, a DOM object or a jQuery element.429* The container will scroll to the position of this element.430* 3. `ScrollScene` -> The container will scroll to the start of this scene.431* 4. `function` -> This function will be used as a callback for future scroll position modifications.432* This provides a way for you to change the behaviour of scrolling and adding new behaviour like animation. The callback receives the new scroll position as a parameter and a reference to the container element using `this`.433* _**NOTE:** All other options will still work as expected, using the new function to scroll._434* @returns {ScrollMagic} Parent object for chaining.435*/436this.scrollTo = function (scrollTarget) {437if (scrollTarget instanceof ScrollScene) {438if (scrollTarget.parent() === ScrollMagic) { // check if this controller is the parent439ScrollMagic.scrollTo(scrollTarget.scrollOffset());440} else {441log (2, "scrollTo(): The supplied scene does not belong to this controller. Scroll cancelled.", scrollTarget);442}443} else if ($.type(scrollTarget) === "string" || isDomElement(scrollTarget) || scrollTarget instanceof $) {444var $elm = $(scrollTarget).first();445if ($elm[0]) {446var447param = _options.vertical ? "top" : "left", // which param is of interest ?448containerOffset = getOffset(_options.container), // container position is needed because element offset is returned in relation to document, not in relation to container.449elementOffset = getOffset($elm);450451if (!_isDocument) { // container is not the document root, so substract scroll Position to get correct trigger element position relative to scrollcontent452containerOffset[param] -= ScrollMagic.scrollPos();453}454455ScrollMagic.scrollTo(elementOffset[param] - containerOffset[param]);456} else {457log (2, "scrollTo(): The supplied element could not be found. Scroll cancelled.", scrollTarget);458}459} else if ($.isFunction(scrollTarget)) {460setScrollPos = scrollTarget;461} else {462setScrollPos.call(_options.container[0], scrollTarget);463}464return ScrollMagic;465};466467/**468* **Get** the current scrollPosition or **Set** a new method to calculate it.469* -> **GET**:470* When used as a getter this function will return the current scroll position.471* To get a cached value use ScrollMagic.info("scrollPos"), which will be updated in the update cycle.472* For vertical controllers it will return the top scroll offset and for horizontal applications it will return the left offset.473*474* -> **SET**:475* When used as a setter this method prodes a way to permanently overwrite the controller's scroll position calculation.476* A typical usecase is when the scroll position is not reflected by the containers scrollTop or scrollLeft values, but for example by the inner offset of a child container.477* Moving a child container inside a parent is a commonly used method for several scrolling frameworks, including iScroll.478* By providing an alternate calculation function you can make sure ScrollMagic receives the correct scroll position.479* Please also bear in mind that your function should return y values for vertical scrolls an x for horizontals.480*481* To change the current scroll position please use `ScrollMagic.scrollTo()`.482* @public483*484* @example485* // get the current scroll Position486* var scrollPos = controller.scrollPos();487*488* // set a new scroll position calculation method489* controller.scrollPos(function () {490* return this.info("vertical") ? -$mychildcontainer.y : -$mychildcontainer.x491* });492*493* @param {function} [scrollPosMethod] - The function to be used for the scroll position calculation of the container.494* @returns {(number|ScrollMagic)} Current scroll position or parent object for chaining.495*/496this.scrollPos = function (scrollPosMethod) {497if (!arguments.length) { // get498return getScrollPos.call(ScrollMagic);499} else { // set500if ($.isFunction(scrollPosMethod)) {501getScrollPos = scrollPosMethod;502} else {503log(2, "Provided value for method 'scrollPos' is not a function. To change the current scroll position use 'scrollTo()'.");504}505}506return ScrollMagic;507};508509/**510* **Get** all infos or one in particular about the controller.511* @public512* @example513* // returns the current scroll position (number)514* var scrollPos = controller.info("scrollPos");515*516* // returns all infos as an object517* var infos = controller.info();518*519* @param {string} [about] - If passed only this info will be returned instead of an object containing all.520Valid options are:521** `"size"` => the current viewport size of the container522** `"vertical"` => true if vertical scrolling, otherwise false523** `"scrollPos"` => the current scroll position524** `"scrollDirection"` => the last known direction of the scroll525** `"container"` => the container element526** `"isDocument"` => true if container element is the document.527* @returns {(mixed|object)} The requested info(s).528*/529this.info = function (about) {530var values = {531size: _viewPortSize, // contains height or width (in regard to orientation);532vertical: _options.vertical,533scrollPos: _scrollPos,534scrollDirection: _scrollDirection,535container: _options.container,536isDocument: _isDocument537};538if (!arguments.length) { // get all as an object539return values;540} else if (values[about] !== undefined) {541return values[about];542} else {543log(1, "ERROR: option \"" + about + "\" is not available");544return;545}546};547548/**549* **Get** or **Set** the current loglevel option value.550* @public551*552* @example553* // get the current value554* var loglevel = controller.loglevel();555*556* // set a new value557* controller.loglevel(3);558*559* @param {number} [newLoglevel] - The new loglevel setting of the ScrollMagic controller. `[0-3]`560* @returns {(number|ScrollMagic)} Current loglevel or parent object for chaining.561*/562this.loglevel = function (newLoglevel) {563if (!arguments.length) { // get564return _options.loglevel;565} else if (_options.loglevel != newLoglevel) { // set566_options.loglevel = newLoglevel;567}568return ScrollMagic;569};570571/**572* **Get** or **Set** the current enabled state of the controller.573* This can be used to disable all Scenes connected to the controller without destroying or removing them.574* @public575*576* @example577* // get the current value578* var enabled = controller.enabled();579*580* // disable the controller581* controller.enabled(false);582*583* @param {boolean} [newState] - The new enabled state of the controller `true` or `false`.584* @returns {(boolean|ScrollMagic)} Current enabled state or parent object for chaining.585*/586this.enabled = function (newState) {587if (!arguments.length) { // get588return _enabled;589} else if (_enabled != newState) { // set590_enabled = !!newState;591ScrollMagic.updateScene(_sceneObjects, true);592}593return ScrollMagic;594};595596/**597* Destroy the Controller, all Scenes and everything.598* @public599*600* @example601* // without resetting the scenes602* controller = controller.destroy();603*604* // with scene reset605* controller = controller.destroy(true);606*607* @param {boolean} [resetScenes=false] - If `true` the pins and tweens (if existent) of all scenes will be reset.608* @returns {null} Null to unset handler variables.609*/610this.destroy = function (resetScenes) {611window.clearTimeout(_refreshInterval);612var i = _sceneObjects.length;613while (i--) {614_sceneObjects[i].destroy(resetScenes);615}616_options.container.off("scroll resize", onChange);617animationFrameCancelCallback(_updateCycle);618log(3, "destroyed " + NAMESPACE + " (reset: " + (resetScenes ? "true" : "false") + ")");619return null;620};621622// INIT623construct();624return ScrollMagic;625};626ScrollMagic.version = "1.3.0"; // version number for browser global627628/**629* A ScrollScene defines where the controller should react and how.630*631* @class632* @global633*634* @example635* // create a standard scene and add it to a controller636* new ScrollScene()637* .addTo(controller);638*639* // create a scene with custom options and assign a handler to it.640* var scene = new ScrollScene({641* duration: 100,642* offset: 200,643* triggerHook: "onEnter",644* reverse: false645* });646*647* @param {object} [options] - Options for the Scene. The options can be updated at any time.648Instead of setting the options for each scene individually you can also set them globally in the controller as the controllers `globalSceneOptions` option. The object accepts the same properties as the ones below.649When a scene is added to the controller the options defined using the ScrollScene constructor will be overwritten by those set in `globalSceneOptions`.650* @param {(number|function)} [options.duration=0] - The duration of the scene.651If `0` tweens will auto-play when reaching the scene start point, pins will be pinned indefinetly starting at the start position.652A function retuning the duration value is also supported. Please see `ScrollScene.duration()` for details.653* @param {number} [options.offset=0] - Offset Value for the Trigger Position. If no triggerElement is defined this will be the scroll distance from the start of the page, after which the scene will start.654* @param {(string|object)} [options.triggerElement=null] - Selector, DOM object or jQuery Object that defines the start of the scene. If undefined the scene will start right at the start of the page (unless an offset is set).655* @param {(number|string)} [options.triggerHook="onCenter"] - Can be a number between 0 and 1 defining the position of the trigger Hook in relation to the viewport.656Can also be defined using a string:657** `"onEnter"` => `1`658** `"onCenter"` => `0.5`659** `"onLeave"` => `0`660* @param {boolean} [options.reverse=true] - Should the scene reverse, when scrolling up?661* @param {boolean} [options.tweenChanges=false] - Tweens Animation to the progress target instead of setting it.662Does not affect animations where duration is `0`.663* @param {number} [options.loglevel=2] - Loglevel for debugging. Note that logging is disabled in the minified version of ScrollMagic.664** `0` => silent665** `1` => errors666** `2` => errors, warnings667** `3` => errors, warnings, debuginfo668*669*/670var ScrollScene = function (options) {671672/*673* ----------------------------------------------------------------674* settings675* ----------------------------------------------------------------676*/677678var679TRIGGER_HOOK_VALUES = {"onCenter" : 0.5, "onEnter" : 1, "onLeave" : 0},680NAMESPACE = "ScrollScene",681DEFAULT_OPTIONS = {682duration: 0,683offset: 0,684triggerElement: null,685triggerHook: "onCenter",686reverse: true,687tweenChanges: false,688loglevel: 2689};690691/*692* ----------------------------------------------------------------693* private vars694* ----------------------------------------------------------------695*/696697var698ScrollScene = this,699_options = $.extend({}, DEFAULT_OPTIONS, options),700_state = 'BEFORE',701_progress = 0,702_scrollOffset = {start: 0, end: 0}, // reflects the parent's scroll position for the start and end of the scene respectively703_triggerPos = 0,704_enabled = true,705_durationUpdateMethod,706_parent,707_tween,708_pin,709_pinOptions,710_cssClasses,711_cssClassElm;712713// object containing validator functions for various options714var _validate = {715"unknownOptionSupplied" : function () {716$.each(_options, function (key, value) {717if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {718log(2, "WARNING: Unknown option \"" + key + "\"");719delete _options[key];720}721});722},723"duration" : function () {724if ($.isFunction(_options.duration)) {725_durationUpdateMethod = _options.duration;726try {727_options.duration = parseFloat(_durationUpdateMethod());728} catch (e) {729log(1, "ERROR: Invalid return value of supplied function for option \"duration\":", _options.duration);730_durationUpdateMethod = undefined;731_options.duration = DEFAULT_OPTIONS.duration;732}733} else {734_options.duration = parseFloat(_options.duration);735if (!$.isNumeric(_options.duration) || _options.duration < 0) {736log(1, "ERROR: Invalid value for option \"duration\":", _options.duration);737_options.duration = DEFAULT_OPTIONS.duration;738}739}740},741"offset" : function () {742_options.offset = parseFloat(_options.offset);743if (!$.isNumeric(_options.offset)) {744log(1, "ERROR: Invalid value for option \"offset\":", _options.offset);745_options.offset = DEFAULT_OPTIONS.offset;746}747},748"triggerElement" : function () {749if (_options.triggerElement !== null && $(_options.triggerElement).length === 0) {750log(1, "ERROR: Element defined in option \"triggerElement\" was not found:", _options.triggerElement);751_options.triggerElement = DEFAULT_OPTIONS.triggerElement;752}753},754"triggerHook" : function () {755if (!(_options.triggerHook in TRIGGER_HOOK_VALUES)) {756if ($.isNumeric(_options.triggerHook)) {757_options.triggerHook = Math.max(0, Math.min(parseFloat(_options.triggerHook), 1)); // make sure its betweeen 0 and 1758} else {759log(1, "ERROR: Invalid value for option \"triggerHook\": ", _options.triggerHook);760_options.triggerHook = DEFAULT_OPTIONS.triggerHook;761}762}763},764"reverse" : function () {765_options.reverse = !!_options.reverse; // force boolean766},767"tweenChanges" : function () {768_options.tweenChanges = !!_options.tweenChanges; // force boolean769},770"loglevel" : function () {771_options.loglevel = parseInt(_options.loglevel);772if (!$.isNumeric(_options.loglevel) || _options.loglevel < 0 || _options.loglevel > 3) {773var wrongval = _options.loglevel;774_options.loglevel = DEFAULT_OPTIONS.loglevel;775log(1, "ERROR: Invalid value for option \"loglevel\":", wrongval);776}777},778};779780/*781* ----------------------------------------------------------------782* private functions783* ----------------------------------------------------------------784*/785786/**787* Internal constructor function of ScrollMagic788* @private789*/790var construct = function () {791validateOption();792793// event listeners794ScrollScene795.on("change.internal", function (e) {796if (e.what !== "loglevel" && e.what !== "tweenChanges") { // no need for a scene update scene with these options...797if (e.what === "triggerElement") {798updateTriggerElementPosition();799} else if (e.what === "reverse") { // the only property left that may have an impact on the current scene state. Everything else is handled by the shift event.800ScrollScene.update();801}802}803})804.on("shift.internal", function (e) {805updateScrollOffset();806ScrollScene.update(); // update scene to reflect new position807if ((_state === "AFTER" && e.reason === "duration") || (_state === 'DURING' && _options.duration === 0)) {808// if [duration changed after a scene (inside scene progress updates pin position)] or [duration is 0, we are in pin phase and some other value changed].809updatePinState();810}811})812.on("progress.internal", function (e) {813updateTweenProgress();814updatePinState();815})816.on("destroy", function (e) {817e.preventDefault(); // otherwise jQuery would call target.destroy() by default.818});819};820821/**822* Send a debug message to the console.823* @private824*825* @param {number} loglevel - The loglevel required to initiate output for the message.826* @param {...mixed} output - One or more variables that should be passed to the console.827*/828var log = function (loglevel, output) {829if (_options.loglevel >= loglevel) {830var831prefix = "(" + NAMESPACE + ") ->",832args = Array.prototype.splice.call(arguments, 1);833args.unshift(loglevel, prefix);834debug.apply(window, args);835}836};837838/**839* Checks the validity of a specific or all options and reset to default if neccessary.840* @private841*/842var validateOption = function (check) {843if (!arguments.length) {844check = [];845for (var key in _validate){846check.push(key);847}848} else if (!$.isArray(check)) {849check = [check];850}851$.each(check, function (key, value) {852if (_validate[value]) {853_validate[value]();854}855});856};857858/**859* Helper used by the setter/getters for scene options860* @private861*/862var changeOption = function(varname, newval) {863var864changed = false,865oldval = _options[varname];866if (_options[varname] != newval) {867_options[varname] = newval;868validateOption(varname); // resets to default if necessary869changed = oldval != _options[varname];870}871return changed;872};873874/**875* Update the start and end scrollOffset of the container.876* The positions reflect what the parent's scroll position will be at the start and end respectively.877* Is called, when:878* - ScrollScene event "change" is called with: offset, triggerHook, duration879* - scroll container event "resize" is called880* - the position of the triggerElement changes881* - the parent changes -> addTo()882* @private883*/884var updateScrollOffset = function () {885_scrollOffset = {start: _triggerPos + _options.offset};886if (_parent && _options.triggerElement) {887// take away triggerHook portion to get relative to top888_scrollOffset.start -= _parent.info("size") * ScrollScene.triggerHook();889}890_scrollOffset.end = _scrollOffset.start + _options.duration;891};892893/**894* Updates the duration if set to a dynamic function.895* This method is called when the scene is added to a controller and in regular intervals from the controller through scene.refresh().896*897* @fires {@link ScrollScene.change}, if the duration changed898* @fires {@link ScrollScene.shift}, if the duration changed899*900* @param {boolean} [suppressEvents=false] - If true the shift event will be suppressed.901* @private902*/903var updateDuration = function (suppressEvents) {904// update duration905if (_durationUpdateMethod) {906var varname = "duration";907if (changeOption(varname, _durationUpdateMethod.call(ScrollScene)) && !suppressEvents) { // set908ScrollScene.trigger("change", {what: varname, newval: _options[varname]});909ScrollScene.trigger("shift", {reason: varname});910}911}912};913914/**915* Updates the position of the triggerElement, if present.916* This method is called ...917* - ... when the triggerElement is changed918* - ... when the scene is added to a (new) controller919* - ... in regular intervals from the controller through scene.refresh().920*921* @fires {@link ScrollScene.shift}, if the position changed922*923* @param {boolean} [suppressEvents=false] - If true the shift event will be suppressed.924* @private925*/926var updateTriggerElementPosition = function (suppressEvents) {927var elementPos = 0;928if (_parent && _options.triggerElement) {929var930element = $(_options.triggerElement).first(),931controllerInfo = _parent.info(),932containerOffset = getOffset(controllerInfo.container), // container position is needed because element offset is returned in relation to document, not in relation to container.933param = controllerInfo.vertical ? "top" : "left"; // which param is of interest ?934935// if parent is spacer, use spacer position instead so correct start position is returned for pinned elements.936while (element.parent().data("ScrollMagicPinSpacer")) {937element = element.parent();938}939940var elementOffset = getOffset(element);941942if (!controllerInfo.isDocument) { // container is not the document root, so substract scroll Position to get correct trigger element position relative to scrollcontent943containerOffset[param] -= _parent.scrollPos();944}945946elementPos = elementOffset[param] - containerOffset[param];947}948var changed = elementPos != _triggerPos;949_triggerPos = elementPos;950if (changed && !suppressEvents) {951ScrollScene.trigger("shift", {reason: "triggerElementPosition"});952}953};954955/**956* Update the tween progress.957* @private958*959* @param {number} [to] - If not set the scene Progress will be used. (most cases)960* @return {boolean} true if the Tween was updated.961*/962var updateTweenProgress = function (to) {963if (_tween) {964var progress = (to >= 0 && to <= 1) ? to : _progress;965if (_tween.repeat() === -1) {966// infinite loop, so not in relation to progress967if (_state === "DURING" && _tween.paused()) {968_tween.play();969} else if (_state !== "DURING" && !_tween.paused()) {970_tween.pause();971} else {972return false;973}974} else if (progress != _tween.progress()) { // do we even need to update the progress?975// no infinite loop - so should we just play or go to a specific point in time?976if (_options.duration === 0) {977// play the animation978if (_state === "DURING") { // play from 0 to 1979_tween.play();980} else { // play from 1 to 0981_tween.reverse();982}983} else {984// go to a specific point in time985if (_options.tweenChanges) {986// go smooth987_tween.tweenTo(progress * _tween.duration());988} else {989// just hard set it990_tween.progress(progress).pause();991}992}993} else {994return false;995}996return true;997} else {998return false;999}1000};10011002/**1003* Update the pin state.1004* @private1005*/1006var updatePinState = function (forceUnpin) {1007if (_pin && _parent) {1008var1009containerInfo = _parent.info();10101011if (!forceUnpin && _state === "DURING") { // during scene or if duration is 0 and we are past the trigger1012// pinned state1013if (_pin.css("position") != "fixed") {1014// change state before updating pin spacer (position changes due to fixed collapsing might occur.)1015_pin.css("position", "fixed");1016// update pin spacer1017updatePinSpacerSize();1018// add pinned class1019_pin.addClass(_pinOptions.pinnedClass);1020}10211022var1023fixedPos = getOffset(_pinOptions.spacer, true), // get viewport position of spacer1024scrollDistance = _options.reverse || _options.duration === 0 ?1025containerInfo.scrollPos - _scrollOffset.start // quicker1026: Math.round(_progress * _options.duration * 10)/10; // if no reverse and during pin the position needs to be recalculated using the progress10271028// remove spacer margin to get real position (in case marginCollapse mode)1029fixedPos.top -= parseFloat(_pinOptions.spacer.css("margin-top"));10301031// add scrollDistance1032fixedPos[containerInfo.vertical ? "top" : "left"] += scrollDistance;10331034// set new values1035_pin.css({1036top: fixedPos.top,1037left: fixedPos.left1038});1039} else {1040// unpinned state1041var1042newCSS = {1043position: _pinOptions.inFlow ? "relative" : "absolute",1044top: 0,1045left: 01046},1047change = _pin.css("position") != newCSS.position;10481049if (!_pinOptions.pushFollowers) {1050newCSS[containerInfo.vertical ? "top" : "left"] = _options.duration * _progress;1051} else if (_options.duration > 0) { // only concerns scenes with duration1052if (_state === "AFTER" && parseFloat(_pinOptions.spacer.css("padding-top")) === 0) {1053change = true; // if in after state but havent updated spacer yet (jumped past pin)1054} else if (_state === "BEFORE" && parseFloat(_pinOptions.spacer.css("padding-bottom")) === 0) { // before1055change = true; // jumped past fixed state upward direction1056}1057}1058// set new values1059_pin.css(newCSS);1060if (change) {1061// remove pinned class1062_pin.removeClass(_pinOptions.pinnedClass);1063// update pin spacer if state changed1064updatePinSpacerSize();1065}1066}1067}1068};10691070/**1071* Update the pin spacer size.1072* The size of the spacer needs to be updated whenever the duration of the scene changes, if it is to push down following elements.1073* @private1074*/1075var updatePinSpacerSize = function () {1076if (_pin && _parent && _pinOptions.inFlow) { // no spacerresize, if original position is absolute1077var1078after = (_state === "AFTER"),1079before = (_state === "BEFORE"),1080during = (_state === "DURING"),1081pinned = (_pin.css("position") == "fixed"),1082vertical = _parent.info("vertical"),1083$spacercontent = _pinOptions.spacer.children().first(), // usually the pined element but can also be another spacer (cascaded pins)1084marginCollapse = isMarginCollapseType(_pinOptions.spacer.css("display")),1085css = {};10861087if (marginCollapse) {1088css["margin-top"] = before || (during && pinned) ? _pin.css("margin-top") : "auto";1089css["margin-bottom"] = after || (during && pinned) ? _pin.css("margin-bottom") : "auto";1090} else {1091css["margin-top"] = css["margin-bottom"] = "auto";1092}10931094// set new size1095// if relsize: spacer -> pin | else: pin -> spacer1096if (_pinOptions.relSize.width || _pinOptions.relSize.autoFullWidth) {1097if (pinned) {1098if ($(window).width() == _pinOptions.spacer.parent().width()) {1099// relative to body1100_pin.css("width", _pinOptions.relSize.autoFullWidth ? "100%" : "inherit");1101} else {1102// not relative to body -> need to calculate1103_pin.css("width", _pinOptions.spacer.width());1104}1105} else {1106_pin.css("width", "100%");1107}1108} else {1109// minwidth is needed for cascading pins.1110// margin is only included if it's a cascaded pin to resolve an IE9 bug1111css["min-width"] = $spacercontent.outerWidth(!$spacercontent.is(_pin));1112css.width = pinned ? css["min-width"] : "auto";1113}1114if (_pinOptions.relSize.height) {1115if (pinned) {1116if ($(window).height() == _pinOptions.spacer.parent().height()) {1117// relative to body1118_pin.css("height", "inherit");1119} else {1120// not relative to body -> need to calculate1121_pin.css("height", _pinOptions.spacer.height());1122}1123} else {1124_pin.css("height", "100%");1125}1126} else {1127css["min-height"] = $spacercontent.outerHeight(!marginCollapse); // needed for cascading pins1128css.height = pinned ? css["min-height"] : "auto";1129}11301131// add space for duration if pushFollowers is true1132if (_pinOptions.pushFollowers) {1133css["padding" + (vertical ? "Top" : "Left")] = _options.duration * _progress;1134css["padding" + (vertical ? "Bottom" : "Right")] = _options.duration * (1 - _progress);1135}1136_pinOptions.spacer.css(css);1137}1138};11391140/**1141* Updates the Pin state (in certain scenarios)1142* If the controller container is not the document and we are mid-pin-phase scrolling or resizing the main document can result to wrong pin positions.1143* So this function is called on resize and scroll of the document.1144* @private1145*/1146var updatePinInContainer = function () {1147if (_parent && _pin && _state === "DURING" && !_parent.info("isDocument")) {1148updatePinState();1149}1150};11511152/**1153* Updates the Pin spacer size state (in certain scenarios)1154* If container is resized during pin and relatively sized the size of the pin might need to be updated...1155* So this function is called on resize of the container.1156* @private1157*/1158var updateRelativePinSpacer = function () {1159if ( _parent && _pin && // well, duh1160_state === "DURING" && // element in pinned state?1161( // is width or height relatively sized, but not in relation to body? then we need to recalc.1162((_pinOptions.relSize.width || _pinOptions.relSize.autoFullWidth) && $(window).width() != _pinOptions.spacer.parent().width()) ||1163(_pinOptions.relSize.height && $(window).height() != _pinOptions.spacer.parent().height())1164)1165) {1166updatePinSpacerSize();1167}1168};11691170/**1171* Is called, when the mousewhel is used while over a pinned element inside a div container.1172* If the scene is in fixed state scroll events would be counted towards the body. This forwards the event to the scroll container.1173* @private1174*/1175var onMousewheelOverPin = function (e) {1176if (_parent && _pin && _state === "DURING" && !_parent.info("isDocument")) { // in pin state1177e.preventDefault();1178_parent.scrollTo(_parent.info("scrollPos") - (e.originalEvent.wheelDelta/3 || -e.originalEvent.detail*30));1179}1180};118111821183/*1184* ----------------------------------------------------------------1185* public functions (getters/setters)1186* ----------------------------------------------------------------1187*/11881189/**1190* **Get** the parent controller.1191* @public1192* @example1193* // get the parent controller of a scene1194* var controller = scene.parent();1195*1196* @returns {ScrollMagic} Parent controller or `undefined`1197*/1198this.parent = function () {1199return _parent;1200};120112021203/**1204* **Get** or **Set** the duration option value.1205* As a setter it also accepts a function returning a numeric value.1206* This is particularly useful for responsive setups.1207*1208* The duration is updated using the supplied function every time `ScrollScene.refresh()` is called, which happens periodically from the controller (see ScrollMagic option `refreshInterval`).1209* _**NOTE:** Be aware that it's an easy way to kill performance, if you supply a function that has high CPU demand.1210* Even for size and position calculations it is recommended to use a variable to cache the value. (see example)1211* This counts double if you use the same function for multiple scenes._1212*1213* @public1214* @example1215* // get the current duration value1216* var duration = scene.duration();1217*1218* // set a new duration1219* scene.duration(300);1220*1221* // use a function to automatically adjust the duration to the window height.1222* var durationValueCache;1223* function getDuration () {1224* return durationValueCache;1225* }1226* function updateDuration (e) {1227* durationValueCache = $(window).innerHeight();1228* }1229* $(window).on("resize", updateDuration); // update the duration when the window size changes1230* $(window).triggerHandler("resize"); // set to initial value1231* scene.duration(getDuration); // supply duration method1232*1233* @fires {@link ScrollScene.change}, when used as setter1234* @fires {@link ScrollScene.shift}, when used as setter1235* @param {(number|function)} [newDuration] - The new duration of the scene.1236* @returns {number} `get` - Current scene duration.1237* @returns {ScrollScene} `set` - Parent object for chaining.1238*/1239this.duration = function (newDuration) {1240var varname = "duration";1241if (!arguments.length) { // get1242return _options[varname];1243} else {1244if (!$.isFunction(newDuration)) {1245_durationUpdateMethod = undefined;1246}1247if (changeOption(varname, newDuration)) { // set1248ScrollScene.trigger("change", {what: varname, newval: _options[varname]});1249ScrollScene.trigger("shift", {reason: varname});1250}1251}1252return ScrollScene;1253};12541255/**1256* **Get** or **Set** the offset option value.1257* @public1258* @example1259* // get the current offset1260* var offset = scene.offset();1261*1262* // set a new offset1263* scene.offset(100);1264*1265* @fires {@link ScrollScene.change}, when used as setter1266* @fires {@link ScrollScene.shift}, when used as setter1267* @param {number} [newOffset] - The new offset of the scene.1268* @returns {number} `get` - Current scene offset.1269* @returns {ScrollScene} `set` - Parent object for chaining.1270*/1271this.offset = function (newOffset) {1272var varname = "offset";1273if (!arguments.length) { // get1274return _options[varname];1275} else if (changeOption(varname, newOffset)) { // set1276ScrollScene.trigger("change", {what: varname, newval: _options[varname]});1277ScrollScene.trigger("shift", {reason: varname});1278}1279return ScrollScene;1280};12811282/**1283* **Get** or **Set** the triggerElement option value.1284* Does **not** fire `ScrollScene.shift`, because changing the trigger Element doesn't necessarily mean the start position changes. This will be determined in `ScrollScene.refresh()`, which is automatically triggered.1285* @public1286* @example1287* // get the current triggerElement1288* var triggerElement = scene.triggerElement();1289*1290* // set a new triggerElement using a selector1291* scene.triggerElement("#trigger");1292* // set a new triggerElement using a jQuery Object1293* scene.triggerElement($("#trigger"));1294* // set a new triggerElement using a DOM object1295* scene.triggerElement(document.getElementById("trigger"));1296*1297* @fires {@link ScrollScene.change}, when used as setter1298* @param {(string|object)} [newTriggerElement] - The new trigger element for the scene.1299* @returns {(string|object)} `get` - Current triggerElement.1300* @returns {ScrollScene} `set` - Parent object for chaining.1301*/1302this.triggerElement = function (newTriggerElement) {1303var varname = "triggerElement";1304if (!arguments.length) { // get1305return _options[varname];1306} else if (changeOption(varname, newTriggerElement)) { // set1307ScrollScene.trigger("change", {what: varname, newval: _options[varname]});1308}1309return ScrollScene;1310};13111312/**1313* **Get** or **Set** the triggerHook option value.1314* @public1315* @example1316* // get the current triggerHook value1317* var triggerHook = scene.triggerHook();1318*1319* // set a new triggerHook using a string1320* scene.triggerHook("onLeave");1321* // set a new triggerHook using a number1322* scene.triggerHook(0.7);1323*1324* @fires {@link ScrollScene.change}, when used as setter1325* @fires {@link ScrollScene.shift}, when used as setter1326* @param {(number|string)} [newTriggerHook] - The new triggerHook of the scene. See {@link ScrollScene} parameter description for value options.1327* @returns {number} `get` - Current triggerHook (ALWAYS numerical).1328* @returns {ScrollScene} `set` - Parent object for chaining.1329*/1330this.triggerHook = function (newTriggerHook) {1331var varname = "triggerHook";1332if (!arguments.length) { // get1333return $.isNumeric(_options[varname]) ? _options[varname] : TRIGGER_HOOK_VALUES[_options[varname]];1334} else if (changeOption(varname, newTriggerHook)) { // set1335ScrollScene.trigger("change", {what: varname, newval: _options[varname]});1336ScrollScene.trigger("shift", {reason: varname});1337}1338return ScrollScene;1339};13401341/**1342* **Get** or **Set** the reverse option value.1343* @public1344* @example1345* // get the current reverse option1346* var reverse = scene.reverse();1347*1348* // set new reverse option1349* scene.reverse(false);1350*1351* @fires {@link ScrollScene.change}, when used as setter1352* @param {boolean} [newReverse] - The new reverse setting of the scene.1353* @returns {boolean} `get` - Current reverse option value.1354* @returns {ScrollScene} `set` - Parent object for chaining.1355*/1356this.reverse = function (newReverse) {1357var varname = "reverse";1358if (!arguments.length) { // get1359return _options[varname];1360} else if (changeOption(varname, newReverse)) { // set1361ScrollScene.trigger("change", {what: varname, newval: _options[varname]});1362}1363return ScrollScene;1364};13651366/**1367* **Get** or **Set** the tweenChanges option value.1368* @public1369* @example1370* // get the current tweenChanges option1371* var tweenChanges = scene.tweenChanges();1372*1373* // set new tweenChanges option1374* scene.tweenChanges(true);1375*1376* @fires {@link ScrollScene.change}, when used as setter1377* @param {boolean} [newTweenChanges] - The new tweenChanges setting of the scene.1378* @returns {boolean} `get` - Current tweenChanges option value.1379* @returns {ScrollScene} `set` - Parent object for chaining.1380*/1381this.tweenChanges = function (newTweenChanges) {1382var varname = "tweenChanges";1383if (!arguments.length) { // get1384return _options[varname];1385} else if (changeOption(varname, newTweenChanges)) { // set1386ScrollScene.trigger("change", {what: varname, newval: _options[varname]});1387}1388return ScrollScene;1389};13901391/**1392* **Get** or **Set** the loglevel option value.1393* @public1394* @example1395* // get the current loglevel1396* var loglevel = scene.loglevel();1397*1398* // set new loglevel1399* scene.loglevel(3);1400*1401* @fires {@link ScrollScene.change}, when used as setter1402* @param {number} [newLoglevel] - The new loglevel setting of the scene. `[0-3]`1403* @returns {number} `get` - Current loglevel.1404* @returns {ScrollScene} `set` - Parent object for chaining.1405*/1406this.loglevel = function (newLoglevel) {1407var varname = "loglevel";1408if (!arguments.length) { // get1409return _options[varname];1410} else if (changeOption(varname, newLoglevel)) { // set1411ScrollScene.trigger("change", {what: varname, newval: _options[varname]});1412}1413return ScrollScene;1414};14151416/**1417* **Get** the current state.1418* @public1419* @example1420* // get the current state1421* var state = scene.state();1422*1423* @returns {string} `"BEFORE"`, `"DURING"` or `"AFTER"`1424*/1425this.state = function () {1426return _state;1427};14281429/**1430* **Get** the trigger position of the scene (including the value of the `offset` option).1431* @public1432* @example1433* // get the scene's trigger position1434* var triggerPosition = scene.triggerPosition();1435*1436* @returns {number} Start position of the scene. Top position value for vertical and left position value for horizontal scrolls.1437*/1438this.triggerPosition = function () {1439var pos = _options.offset; // the offset is the basis1440if (_parent) {1441// get the trigger position1442if (_options.triggerElement) {1443// Element as trigger1444pos += _triggerPos;1445} else {1446// return the height of the triggerHook to start at the beginning1447pos += _parent.info("size") * ScrollScene.triggerHook();1448}1449}1450return pos;1451};14521453/**1454* **Get** the trigger offset of the scene (including the value of the `offset` option).1455* @public1456* @deprecated Method is deprecated since 1.1.0. You should now use {@link ScrollScene.triggerPosition}1457*/1458this.triggerOffset = function () {1459return ScrollScene.triggerPosition();1460};14611462/**1463* **Get** the current scroll offset for the start of the scene.1464* Mind, that the scrollOffset is related to the size of the container, if `triggerHook` is bigger than `0` (or `"onLeave"`).1465* This means, that resizing the container or changing the `triggerHook` will influence the scene's start offset.1466* @public1467* @example1468* // get the current scroll offset for the start and end of the scene.1469* var start = scene.scrollOffset();1470* var end = scene.scrollOffset() + scene.duration();1471* console.log("the scene starts at", start, "and ends at", end);1472*1473* @returns {number} The scroll offset (of the container) at which the scene will trigger. Y value for vertical and X value for horizontal scrolls.1474*/1475this.scrollOffset = function () {1476return _scrollOffset.start;1477};14781479/*1480* ----------------------------------------------------------------1481* public functions (scene modification)1482* ----------------------------------------------------------------1483*/14841485/**1486* Updates the Scene in the parent Controller to reflect the current state.1487* This is the equivalent to `ScrollMagic.updateScene(scene, immediately)`.1488* The update method calculates the scene's start and end position (based on the trigger element, trigger hook, duration and offset) and checks it against the current scroll position of the container.1489* It then updates the current scene state accordingly (or does nothing, if the state is already correct) – Pins will be set to their correct position and tweens will be updated to their correct progress.1490* This means an update doesn't necessarily result in a progress change. The `progress` event will be fired if the progress has indeed changed between this update and the last.1491* _**NOTE:** This method gets called constantly whenever ScrollMagic detects a change. The only application for you is if you change something outside of the realm of ScrollMagic, like moving the trigger or changing tween parameters._1492* @public1493* @example1494* // update the scene on next tick1495* scene.update();1496*1497* // update the scene immediately1498* scene.update(true);1499*1500* @fires ScrollScene.update1501*1502* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next update cycle (better performance).1503* @returns {ScrollScene} Parent object for chaining.1504*/1505this.update = function (immediately) {1506if (_parent) {1507if (immediately) {1508if (_parent.enabled() && _enabled) {1509var1510scrollPos = _parent.info("scrollPos"),1511newProgress;15121513if (_options.duration > 0) {1514newProgress = (scrollPos - _scrollOffset.start)/(_scrollOffset.end - _scrollOffset.start);1515} else {1516newProgress = scrollPos >= _scrollOffset.start ? 1 : 0;1517}15181519ScrollScene.trigger("update", {startPos: _scrollOffset.start, endPos: _scrollOffset.end, scrollPos: scrollPos});15201521ScrollScene.progress(newProgress);1522} else if (_pin && _state === "DURING") {1523updatePinState(true); // unpin in position1524}1525} else {1526_parent.updateScene(ScrollScene, false);1527}1528}1529return ScrollScene;1530};15311532/**1533* Updates dynamic scene variables like the trigger element position or the duration.1534* This method is automatically called in regular intervals from the controller. See {@link ScrollMagic} option `refreshInterval`.1535*1536* You can call it to minimize lag, for example when you intentionally change the position of the triggerElement.1537* If you don't it will simply be updated in the next refresh interval of the container, which is usually sufficient.1538*1539* @public1540* @since 1.1.01541* @example1542* scene = new ScrollScene({triggerElement: "#trigger"});1543*1544* // change the position of the trigger1545* $("#trigger").css("top", 500);1546* // immediately let the scene know of this change1547* scene.refresh();1548*1549* @fires {@link ScrollScene.shift}, if the trigger element position or the duration changed1550* @fires {@link ScrollScene.change}, if the duration changed1551*1552* @returns {ScrollScene} Parent object for chaining.1553*/1554this.refresh = function () {1555updateDuration();1556updateTriggerElementPosition();1557// update trigger element position1558return ScrollScene;1559};15601561/**1562* **Get** or **Set** the scene's progress.1563* Usually it shouldn't be necessary to use this as a setter, as it is set automatically by scene.update().1564* The order in which the events are fired depends on the duration of the scene:1565* 1. Scenes with `duration == 0`:1566* Scenes that have no duration by definition have no ending. Thus the `end` event will never be fired.1567* When the trigger position of the scene is passed the events are always fired in this order:1568* `enter`, `start`, `progress` when scrolling forward1569* and1570* `progress`, `start`, `leave` when scrolling in reverse1571* 2. Scenes with `duration > 0`:1572* Scenes with a set duration have a defined start and end point.1573* When scrolling past the start position of the scene it will fire these events in this order:1574* `enter`, `start`, `progress`1575* When continuing to scroll and passing the end point it will fire these events:1576* `progress`, `end`, `leave`1577* When reversing through the end point these events are fired:1578* `enter`, `end`, `progress`1579* And when continuing to scroll past the start position in reverse it will fire:1580* `progress`, `start`, `leave`1581* In between start and end the `progress` event will be called constantly, whenever the progress changes.1582*1583* In short:1584* `enter` events will always trigger **before** the progress update and `leave` envents will trigger **after** the progress update.1585* `start` and `end` will always trigger at their respective position.1586*1587* Please review the event descriptions for details on the events and the event object that is passed to the callback.1588*1589* @public1590* @example1591* // get the current scene progress1592* var progress = scene.progress();1593*1594* // set new scene progress1595* scene.progress(0.3);1596*1597* @fires {@link ScrollScene.enter}, when used as setter1598* @fires {@link ScrollScene.start}, when used as setter1599* @fires {@link ScrollScene.progress}, when used as setter1600* @fires {@link ScrollScene.end}, when used as setter1601* @fires {@link ScrollScene.leave}, when used as setter1602*1603* @param {number} [progress] - The new progress value of the scene `[0-1]`.1604* @returns {number} `get` - Current scene progress.1605* @returns {ScrollScene} `set` - Parent object for chaining.1606*/1607this.progress = function (progress) {1608if (!arguments.length) { // get1609return _progress;1610} else { // set1611var1612doUpdate = false,1613oldState = _state,1614scrollDirection = _parent ? _parent.info("scrollDirection") : 'PAUSED',1615reverseOrForward = _options.reverse || progress >= _progress;1616if (_options.duration === 0) {1617// zero duration scenes1618doUpdate = _progress != progress;1619_progress = progress < 1 && reverseOrForward ? 0 : 1;1620_state = _progress === 0 ? 'BEFORE' : 'DURING';1621} else {1622// scenes with start and end1623if (progress <= 0 && _state !== 'BEFORE' && reverseOrForward) {1624// go back to initial state1625_progress = 0;1626_state = 'BEFORE';1627doUpdate = true;1628} else if (progress > 0 && progress < 1 && reverseOrForward) {1629_progress = progress;1630_state = 'DURING';1631doUpdate = true;1632} else if (progress >= 1 && _state !== 'AFTER') {1633_progress = 1;1634_state = 'AFTER';1635doUpdate = true;1636} else if (_state === 'DURING' && !reverseOrForward) {1637updatePinState(); // in case we scrolled backwards mid-scene and reverse is disabled => update the pin position, so it doesn't move back as well.1638}1639}1640if (doUpdate) {1641// fire events1642var1643eventVars = {progress: _progress, state: _state, scrollDirection: scrollDirection},1644stateChanged = _state != oldState;16451646var trigger = function (eventName) { // tmp helper to simplify code1647ScrollScene.trigger(eventName, eventVars);1648};16491650if (stateChanged) { // enter events1651if (oldState !== 'DURING') {1652trigger("enter");1653trigger(oldState === 'BEFORE' ? "start" : "end");1654}1655}1656trigger("progress");1657if (stateChanged) { // leave events1658if (_state !== 'DURING') {1659trigger(_state === 'BEFORE' ? "start" : "end");1660trigger("leave");1661}1662}1663}16641665return ScrollScene;1666}1667};16681669/**1670* Add a tween to the scene.1671* If you want to add multiple tweens, wrap them into one TimelineMax object and add it.1672* The duration of the tween is streched to the scroll duration of the scene, unless the scene has a duration of `0`.1673* @public1674* @example1675* // add a single tween1676* scene.setTween(TweenMax.to("obj"), 1, {x: 100});1677*1678* // add multiple tweens, wrapped in a timeline.1679* var timeline = new TimelineMax();1680* var tween1 = TweenMax.from("obj1", 1, {x: 100});1681* var tween2 = TweenMax.to("obj2", 1, {y: 100});1682* timeline1683* .add(tween1)1684* .add(tween2);1685* scene.addTween(timeline);1686*1687* @param {object} TweenObject - A TweenMax, TweenLite, TimelineMax or TimelineLite object that should be animated in the scene.1688* @returns {ScrollScene} Parent object for chaining.1689*/1690this.setTween = function (TweenObject) {1691if (!TimelineMax) {1692log(1, "ERROR: TimelineMax wasn't found. Please make sure GSAP is loaded before ScrollMagic or use asynchronous loading.");1693return ScrollScene;1694}1695if (_tween) { // kill old tween?1696ScrollScene.removeTween();1697}1698try {1699// wrap Tween into a TimelineMax Object to include delay and repeats in the duration and standardize methods.1700_tween = new TimelineMax({smoothChildTiming: true})1701.add(TweenObject)1702.pause();1703} catch (e) {1704log(1, "ERROR calling method 'setTween()': Supplied argument is not a valid TweenObject");1705} finally {1706// some properties need to be transferred it to the wrapper, otherwise they would get lost.1707if (TweenObject.repeat && TweenObject.repeat() === -1) {// TweenMax or TimelineMax Object?1708_tween.repeat(-1);1709_tween.yoyo(TweenObject.yoyo());1710}1711}1712// Some tween validations and debugging helpers17131714// check if there are position tweens defined for the trigger and warn about it :)1715if (_tween && _parent && _options.triggerElement && _options.loglevel >= 2) {// parent is needed to know scroll direction.1716var1717triggerTweens = _tween.getTweensOf($(_options.triggerElement)),1718vertical = _parent.info("vertical");1719$.each(triggerTweens, function (index, value) {1720var1721tweenvars = value.vars.css || value.vars,1722condition = vertical ? (tweenvars.top !== undefined || tweenvars.bottom !== undefined) : (tweenvars.left !== undefined || tweenvars.right !== undefined);1723if (condition) {1724log(2, "WARNING: Tweening the position of the trigger element affects the scene timing and should be avoided!");1725return false;1726}1727});1728}17291730// warn about tween overwrites, when an element is tweened multiple times1731if (parseFloat(TweenLite.version) >= 1.14) { // onOverwrite only present since GSAP v1.14.01732var1733list = _tween.getChildren(true, true, false), // get all nested tween objects1734newCallback = function () {1735log(2, "WARNING: tween was overwritten by another. To learn how to avoid this issue see here: https://github.com/janpaepke/ScrollMagic/wiki/WARNING:-tween-was-overwritten-by-another");1736};1737for (var i=0, thisTween, oldCallback; i<list.length; i++) {1738/*jshint loopfunc: true */1739thisTween = list[i];1740if (oldCallback !== newCallback) { // if tweens is added more than once1741oldCallback = thisTween.vars.onOverwrite;1742thisTween.vars.onOverwrite = function () {1743if (oldCallback) {1744oldCallback.apply(this, arguments);1745}1746newCallback.apply(this, arguments);1747};1748}1749}1750}1751log(3, "added tween");1752updateTweenProgress();1753return ScrollScene;1754};17551756/**1757* Remove the tween from the scene.1758* @public1759* @example1760* // remove the tween from the scene without resetting it1761* scene.removeTween();1762*1763* // remove the tween from the scene and reset it to initial position1764* scene.removeTween(true);1765*1766* @param {boolean} [reset=false] - If `true` the tween will be reset to its initial values.1767* @returns {ScrollScene} Parent object for chaining.1768*/1769this.removeTween = function (reset) {1770if (_tween) {1771if (reset) {1772updateTweenProgress(0);1773}1774_tween.kill();1775_tween = undefined;1776log(3, "removed tween (reset: " + (reset ? "true" : "false") + ")");1777}1778return ScrollScene;1779};17801781/**1782* Pin an element for the duration of the tween.1783* If the scene duration is 0 the element will only be unpinned, if the user scrolls back past the start position.1784* _**NOTE:** The option `pushFollowers` has no effect, when the scene duration is 0._1785* @public1786* @example1787* // pin element and push all following elements down by the amount of the pin duration.1788* scene.setPin("#pin");1789*1790* // pin element and keeping all following elements in their place. The pinned element will move past them.1791* scene.setPin("#pin", {pushFollowers: false});1792*1793* @param {(string|object)} element - A Selector targeting an element, a DOM object or a jQuery object that is supposed to be pinned.1794* @param {object} [settings] - settings for the pin1795* @param {boolean} [settings.pushFollowers=true] - If `true` following elements will be "pushed" down for the duration of the pin, if `false` the pinned element will just scroll past them.1796Ignored, when duration is `0`.1797* @param {string} [settings.spacerClass="scrollmagic-pin-spacer"] - Classname of the pin spacer element, which is used to replace the element.1798* @param {string} [settings.pinnedClass=""] - Classname that should be added to the pinned element during pin phase (and removed after).1799*1800* @returns {ScrollScene} Parent object for chaining.1801*/1802this.setPin = function (element, settings) {1803var1804defaultSettings = {1805pushFollowers: true,1806spacerClass: "scrollmagic-pin-spacer",1807pinnedClass: ""1808};1809settings = $.extend({}, defaultSettings, settings);18101811// validate Element1812element = $(element).first();1813if (element.length === 0) {1814log(1, "ERROR calling method 'setPin()': Invalid pin element supplied.");1815return ScrollScene; // cancel1816} else if (element.css("position") == "fixed") {1817log(1, "ERROR calling method 'setPin()': Pin does not work with elements that are positioned 'fixed'.");1818return ScrollScene; // cancel1819}18201821if (_pin) { // preexisting pin?1822if (_pin === element) {1823// same pin we already have -> do nothing1824return ScrollScene; // cancel1825} else {1826// kill old pin1827ScrollScene.removePin();1828}18291830}1831_pin = element;18321833_pin.parent().hide(); // hack start to force jQuery css to return stylesheet values instead of calculated px values.1834var1835inFlow = _pin.css("position") != "absolute",1836pinCSS = _pin.css(["display", "top", "left", "bottom", "right"]),1837sizeCSS = _pin.css(["width", "height"]);1838_pin.parent().show(); // hack end.18391840if (sizeCSS.width === "0px" && inFlow && isMarginCollapseType(pinCSS.display)) {1841// log (2, "WARNING: Your pinned element probably needs a defined width or it might collapse during pin.");1842}1843if (!inFlow && settings.pushFollowers) {1844log(2, "WARNING: If the pinned element is positioned absolutely pushFollowers is disabled.");1845settings.pushFollowers = false;1846}18471848// create spacer1849var spacer = $("<div></div>")1850.addClass(settings.spacerClass)1851.css(pinCSS)1852.data("ScrollMagicPinSpacer", true)1853.css({1854position: inFlow ? "relative" : "absolute",1855"margin-left": "auto",1856"margin-right": "auto",1857"box-sizing": "content-box"1858});18591860// set the pin Options1861var pinInlineCSS = _pin[0].style;1862_pinOptions = {1863spacer: spacer,1864relSize: { // save if size is defined using % values. if so, handle spacer resize differently...1865width: sizeCSS.width.slice(-1) === "%",1866height: sizeCSS.height.slice(-1) === "%",1867autoFullWidth: sizeCSS.width === "0px" && inFlow && isMarginCollapseType(pinCSS.display)1868},1869pushFollowers: settings.pushFollowers,1870inFlow: inFlow, // stores if the element takes up space in the document flow1871origStyle: {1872width: pinInlineCSS.width || "",1873position: pinInlineCSS.position || "",1874top: pinInlineCSS.top || "",1875left: pinInlineCSS.left || "",1876bottom: pinInlineCSS.bottom || "",1877right: pinInlineCSS.right || "",1878"box-sizing": pinInlineCSS["box-sizing"] || "",1879"-moz-box-sizing": pinInlineCSS["-moz-box-sizing"] || "",1880"-webkit-box-sizing": pinInlineCSS["-webkit-box-sizing"] || ""1881}, // save old styles (for reset)1882pinnedClass: settings.pinnedClass // the class that should be added to the element when pinned1883};18841885// if relative size, transfer it to spacer and make pin calculate it...1886if (_pinOptions.relSize.width) {1887spacer.css("width", sizeCSS.width);1888}1889if (_pinOptions.relSize.height) {1890spacer.css("height", sizeCSS.height);1891}18921893// now place the pin element inside the spacer1894_pin.before(spacer)1895.appendTo(spacer)1896// and set new css1897.css({1898position: inFlow ? "relative" : "absolute",1899top: "auto",1900left: "auto",1901bottom: "auto",1902right: "auto"1903});19041905if (_pinOptions.relSize.width || _pinOptions.relSize.autoFullWidth) {1906_pin.css("box-sizing", "border-box");1907}19081909// add listener to document to update pin position in case controller is not the document.1910$(window).on("scroll." + NAMESPACE + "_pin resize." + NAMESPACE + "_pin", updatePinInContainer);1911// add mousewheel listener to catch scrolls over fixed elements1912_pin.on("mousewheel DOMMouseScroll", onMousewheelOverPin);19131914log(3, "added pin");19151916// finally update the pin to init1917updatePinState();19181919return ScrollScene;1920};19211922/**1923* Remove the pin from the scene.1924* @public1925* @example1926* // remove the pin from the scene without resetting it (the spacer is not removed)1927* scene.removePin();1928*1929* // remove the pin from the scene and reset the pin element to its initial position (spacer is removed)1930* scene.removePin(true);1931*1932* @param {boolean} [reset=false] - If `false` the spacer will not be removed and the element's position will not be reset.1933* @returns {ScrollScene} Parent object for chaining.1934*/1935this.removePin = function (reset) {1936if (_pin) {1937if (reset || !_parent) { // if there's no parent no progress was made anyway...1938_pin.insertBefore(_pinOptions.spacer)1939.css(_pinOptions.origStyle);1940_pinOptions.spacer.remove();1941} else {1942if (_state === "DURING") {1943updatePinState(true); // force unpin at position1944}1945}1946$(window).off("scroll." + NAMESPACE + "_pin resize." + NAMESPACE + "_pin");1947_pin.off("mousewheel DOMMouseScroll", onMousewheelOverPin);1948_pin = undefined;1949log(3, "removed pin (reset: " + (reset ? "true" : "false") + ")");1950}1951return ScrollScene;1952};19531954/**1955* Define a css class modification while the scene is active.1956* When the scene triggers the classes will be added to the supplied element and removed, when the scene is over.1957* If the scene duration is 0 the classes will only be removed if the user scrolls back past the start position.1958* @public1959* @example1960* // add the class 'myclass' to the element with the id 'my-elem' for the duration of the scene1961* scene.setClassToggle("#my-elem", "myclass");1962*1963* // add multiple classes to multiple elements defined by the selector '.classChange'1964* scene.setClassToggle(".classChange", "class1 class2 class3");1965*1966* @param {(string|object)} element - A Selector targeting one or more elements, a DOM object or a jQuery object that is supposed to be modified.1967* @param {string} classes - One or more Classnames (separated by space) that should be added to the element during the scene.1968*1969* @returns {ScrollScene} Parent object for chaining.1970*/1971this.setClassToggle = function (element, classes) {1972var $elm = $(element);1973if ($elm.length === 0 || $.type(classes) !== "string") {1974log(1, "ERROR calling method 'setClassToggle()': Invalid " + ($elm.length === 0 ? "element" : "classes") + " supplied.");1975return ScrollScene;1976}1977_cssClasses = classes;1978_cssClassElm = $elm;1979ScrollScene.on("enter.internal_class leave.internal_class", function (e) {1980_cssClassElm.toggleClass(_cssClasses, e.type === "enter");1981});1982return ScrollScene;1983};19841985/**1986* Remove the class binding from the scene.1987* @public1988* @example1989* // remove class binding from the scene without reset1990* scene.removeClassToggle();1991*1992* // remove class binding and remove the changes it caused1993* scene.removeClassToggle(true);1994*1995* @param {boolean} [reset=false] - If `false` and the classes are currently active, they will remain on the element. If `true` they will be removed.1996* @returns {ScrollScene} Parent object for chaining.1997*/1998this.removeClassToggle = function (reset) {1999if (_cssClassElm && reset) {2000_cssClassElm.removeClass(_cssClasses);2001}2002ScrollScene.off("start.internal_class end.internal_class");2003_cssClasses = undefined;2004_cssClassElm = undefined;2005return ScrollScene;2006};20072008/**2009* Add the scene to a controller.2010* This is the equivalent to `ScrollMagic.addScene(scene)`.2011* @public2012* @example2013* // add a scene to a ScrollMagic controller2014* scene.addTo(controller);2015*2016* @param {ScrollMagic} controller - The controller to which the scene should be added.2017* @returns {ScrollScene} Parent object for chaining.2018*/2019this.addTo = function (controller) {2020if (!(controller instanceof ScrollMagic)) {2021log(1, "ERROR: supplied argument of 'addTo()' is not a valid ScrollMagic controller");2022} else if (_parent != controller) {2023// new parent2024if (_parent) { // I had a parent before, so remove it...2025_parent.removeScene(ScrollScene);2026}2027_parent = controller;2028validateOption();2029updateDuration(true);2030updateTriggerElementPosition(true);2031updateScrollOffset();2032updatePinSpacerSize();2033_parent.info("container").on("resize." + NAMESPACE, function () {2034updateRelativePinSpacer();2035if (ScrollScene.triggerHook() > 0) {2036ScrollScene.trigger("shift", {reason: "containerSize"});2037}2038});2039log(3, "added " + NAMESPACE + " to controller");2040controller.addScene(ScrollScene);2041ScrollScene.update();2042}2043return ScrollScene;2044};20452046/**2047* **Get** or **Set** the current enabled state of the scene.2048* This can be used to disable this scene without removing or destroying it.2049* @public2050*2051* @example2052* // get the current value2053* var enabled = scene.enabled();2054*2055* // disable the scene2056* scene.enabled(false);2057*2058* @param {boolean} [newState] - The new enabled state of the scene `true` or `false`.2059* @returns {(boolean|ScrollScene)} Current enabled state or parent object for chaining.2060*/2061this.enabled = function (newState) {2062if (!arguments.length) { // get2063return _enabled;2064} else if (_enabled != newState) { // set2065_enabled = !!newState;2066ScrollScene.update(true);2067}2068return ScrollScene;2069};20702071/**2072* Remove the scene from its parent controller.2073* This is the equivalent to `ScrollMagic.removeScene(scene)`.2074* The scene will not be updated anymore until you readd it to a controller.2075* To remove the pin or the tween you need to call removeTween() or removePin() respectively.2076* @public2077* @example2078* // remove the scene from its parent controller2079* scene.remove();2080*2081* @returns {ScrollScene} Parent object for chaining.2082*/2083this.remove = function () {2084if (_parent) {2085_parent.info("container").off("resize." + NAMESPACE);2086var tmpParent = _parent;2087_parent = undefined;2088log(3, "removed " + NAMESPACE + " from controller");2089tmpParent.removeScene(ScrollScene);2090}2091return ScrollScene;2092};20932094/**2095* Destroy the scene and everything.2096* @public2097* @example2098* // destroy the scene without resetting the pin and tween to their initial positions2099* scene = scene.destroy();2100*2101* // destroy the scene and reset the pin and tween2102* scene = scene.destroy(true);2103*2104* @param {boolean} [reset=false] - If `true` the pin and tween (if existent) will be reset.2105* @returns {null} Null to unset handler variables.2106*/2107this.destroy = function (reset) {2108ScrollScene.removeTween(reset);2109ScrollScene.removePin(reset);2110ScrollScene.removeClassToggle(reset);2111ScrollScene.trigger("destroy", {reset: reset});2112ScrollScene.remove();2113ScrollScene.off("start end enter leave progress change update shift destroy shift.internal change.internal progress.internal");2114log(3, "destroyed " + NAMESPACE + " (reset: " + (reset ? "true" : "false") + ")");2115return null;2116};21172118/*2119* ----------------------------------------------------------------2120* EVENTS2121* ----------------------------------------------------------------2122*/21232124/**2125* Scene start event.2126* Fires whenever the scroll position its the starting point of the scene.2127* It will also fire when scrolling back up going over the start position of the scene. If you want something to happen only when scrolling down/right, use the scrollDirection parameter passed to the callback.2128*2129* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.2130*2131* @event ScrollScene.start2132*2133* @example2134* scene.on("start", function (event) {2135* alert("Hit start point of scene.");2136* });2137*2138* @property {object} event - The event Object passed to each callback2139* @property {string} event.type - The name of the event2140* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2141* @property {number} event.progress - Reflects the current progress of the scene2142* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`2143* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`2144*/2145/**2146* Scene end event.2147* Fires whenever the scroll position its the ending point of the scene.2148* It will also fire when scrolling back up from after the scene and going over its end position. If you want something to happen only when scrolling down/right, use the scrollDirection parameter passed to the callback.2149*2150* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.2151*2152* @event ScrollScene.end2153*2154* @example2155* scene.on("end", function (event) {2156* alert("Hit end point of scene.");2157* });2158*2159* @property {object} event - The event Object passed to each callback2160* @property {string} event.type - The name of the event2161* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2162* @property {number} event.progress - Reflects the current progress of the scene2163* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`2164* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`2165*/2166/**2167* Scene enter event.2168* Fires whenever the scene enters the "DURING" state.2169* Keep in mind that it doesn't matter if the scene plays forward or backward: This event always fires when the scene enters its active scroll timeframe, regardless of the scroll-direction.2170*2171* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.2172*2173* @event ScrollScene.enter2174*2175* @example2176* scene.on("enter", function (event) {2177* alert("Entered a scene.");2178* });2179*2180* @property {object} event - The event Object passed to each callback2181* @property {string} event.type - The name of the event2182* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2183* @property {number} event.progress - Reflects the current progress of the scene2184* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`2185* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`2186*/2187/**2188* Scene leave event.2189* Fires whenever the scene's state goes from "DURING" to either "BEFORE" or "AFTER".2190* Keep in mind that it doesn't matter if the scene plays forward or backward: This event always fires when the scene leaves its active scroll timeframe, regardless of the scroll-direction.2191*2192* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.2193*2194* @event ScrollScene.leave2195*2196* @example2197* scene.on("leave", function (event) {2198* alert("Left a scene.");2199* });2200*2201* @property {object} event - The event Object passed to each callback2202* @property {string} event.type - The name of the event2203* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2204* @property {number} event.progress - Reflects the current progress of the scene2205* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`2206* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`2207*/2208/**2209* Scene update event.2210* Fires whenever the scene is updated (but not necessarily changes the progress).2211*2212* @event ScrollScene.update2213*2214* @example2215* scene.on("update", function (event) {2216* console.log("Scene updated.");2217* });2218*2219* @property {object} event - The event Object passed to each callback2220* @property {string} event.type - The name of the event2221* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2222* @property {number} event.startPos - The starting position of the scene (in relation to the conainer)2223* @property {number} event.endPos - The ending position of the scene (in relation to the conainer)2224* @property {number} event.scrollPos - The current scroll position of the container2225*/2226/**2227* Scene progress event.2228* Fires whenever the progress of the scene changes.2229*2230* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.2231*2232* @event ScrollScene.progress2233*2234* @example2235* scene.on("progress", function (event) {2236* console.log("Scene progress changed.");2237* });2238*2239* @property {object} event - The event Object passed to each callback2240* @property {string} event.type - The name of the event2241* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2242* @property {number} event.progress - Reflects the current progress of the scene2243* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`2244* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`2245*/2246/**2247* Scene change event.2248* Fires whenvever a property of the scene is changed.2249*2250* @event ScrollScene.change2251*2252* @example2253* scene.on("change", function (event) {2254* console.log("Scene Property \"" + event.what + "\" changed to " + event.newval);2255* });2256*2257* @property {object} event - The event Object passed to each callback2258* @property {string} event.type - The name of the event2259* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2260* @property {string} event.what - Indicates what value has been changed2261* @property {mixed} event.newval - The new value of the changed property2262*/2263/**2264* Scene shift event.2265* Fires whenvever the start or end **scroll offset** of the scene change.2266* This happens explicitely, when one of these values change: `offset`, `duration` or `triggerHook`.2267* It will fire implicitly when the `triggerElement` changes, if the new element has a different position (most cases).2268* It will also fire implicitly when the size of the container changes and the triggerHook is anything other than `onLeave`.2269*2270* @event ScrollScene.shift2271* @since 1.1.02272*2273* @example2274* scene.on("shift", function (event) {2275* console.log("Scene moved, because the " + event.reason + " has changed.)");2276* });2277*2278* @property {object} event - The event Object passed to each callback2279* @property {string} event.type - The name of the event2280* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2281* @property {string} event.reason - Indicates why the scene has shifted2282*/2283/**2284* Scene destroy event.2285* Fires whenvever the scene is destroyed.2286* This can be used to tidy up custom behaviour used in events.2287*2288* @event ScrollScene.destroy2289* @since 1.1.02290*2291* @example2292* scene.on("enter", function (event) {2293* // add custom action2294* $("#my-elem").left("200");2295* })2296* .on("destroy", function (event) {2297* // reset my element to start position2298* if (event.reset) {2299* $("#my-elem").left("0");2300* }2301* });2302*2303* @property {object} event - The event Object passed to each callback2304* @property {string} event.type - The name of the event2305* @property {ScrollScene} event.target - The ScrollScene object that triggered this event2306* @property {boolean} event.reset - Indicates if the destroy method was called with reset `true` or `false`.2307*/23082309/**2310* Add one ore more event listener.2311* The callback function will be fired at the respective event, and an object containing relevant data will be passed to the callback.2312* @public2313*2314* @example2315* function callback (event) {2316* console.log("Event fired! (" + event.type + ")");2317* }2318* // add listeners2319* scene.on("change update progress start end enter leave", callback);2320*2321* @param {string} name - The name or names of the event the callback should be attached to.2322* @param {function} callback - A function that should be executed, when the event is dispatched. An event object will be passed to the callback.2323* @returns {ScrollScene} Parent object for chaining.2324*/2325this.on = function (name, callback) {2326if ($.isFunction(callback)) {2327var names = $.trim(name).toLowerCase()2328.replace(/(\w+)\.(\w+)/g, '$1.' + NAMESPACE + '_$2') // add custom namespace, if one is defined2329.replace(/( |^)(\w+)(?= |$)/g, '$1$2.' + NAMESPACE ); // add namespace to regulars.2330$(ScrollScene).on(names, callback);2331} else {2332log(1, "ERROR calling method 'on()': Supplied argument is not a valid callback!");2333}2334return ScrollScene;2335};23362337/**2338* Remove one or more event listener.2339* @public2340*2341* @example2342* function callback (event) {2343* console.log("Event fired! (" + event.type + ")");2344* }2345* // add listeners2346* scene.on("change update", callback);2347* // remove listeners2348* scene.off("change update", callback);2349*2350* @param {string} name - The name or names of the event that should be removed.2351* @param {function} [callback] - A specific callback function that should be removed. If none is passed all callbacks to the event listener will be removed.2352* @returns {ScrollScene} Parent object for chaining.2353*/2354this.off = function (name, callback) {2355var names = $.trim(name).toLowerCase()2356.replace(/(\w+)\.(\w+)/g, '$1.' + NAMESPACE + '_$2') // add custom namespace, if one is defined2357.replace(/( |^)(\w+)(?= |$)/g, '$1$2.' + NAMESPACE + '$3'); // add namespace to regulars.2358$(ScrollScene).off(names, callback);2359return ScrollScene;2360};23612362/**2363* Trigger an event.2364* @public2365*2366* @example2367* this.trigger("change");2368*2369* @param {string} name - The name of the event that should be triggered.2370* @param {object} [vars] - An object containing info that should be passed to the callback.2371* @returns {ScrollScene} Parent object for chaining.2372*/2373this.trigger = function (name, vars) {2374log(3, 'event fired:', name, "->", vars);2375var event = $.Event($.trim(name).toLowerCase(), vars);2376$(ScrollScene).trigger(event);2377return ScrollScene;2378};23792380// INIT2381construct();2382return ScrollScene;2383};23842385/*2386* ----------------------------------------------------------------2387* global logging functions and making sure no console errors occur2388* ----------------------------------------------------------------2389*/23902391var debug = (function (console) {2392var loglevels = ["error", "warn", "log"];2393if (!console.log) {2394console.log = function(){}; // no console log, well - do nothing then...2395}2396for(var i = 0, method; i<loglevels.length; i++) { // make sure methods for all levels exist.2397method = loglevels[i];2398if (!console[method]) {2399console[method] = console.log; // prefer .log over nothing2400}2401}2402// debugging function2403return function (loglevel) {2404if (loglevel > loglevels.length || loglevel <= 0) loglevel = loglevels.length;2405var now = new Date(),2406time = ("0"+now.getHours()).slice(-2) + ":" + ("0"+now.getMinutes()).slice(-2) + ":" + ("0"+now.getSeconds()).slice(-2) + ":" + ("00"+now.getMilliseconds()).slice(-3),2407method = loglevels[loglevel-1],2408args = Array.prototype.splice.call(arguments, 1),2409func = Function.prototype.bind.call(console[method], console);24102411args.unshift(time);2412func.apply(console, args);2413};2414}(window.console = window.console || {}));2415// a helper function that should generally be faster than jQuery.offset() and can also return position in relation to viewport.2416var getOffset = function (elem, relativeToViewport) {2417var offset = {top: 0, left: 0};2418elem = elem[0]; // tmp workaround until jQuery dependency is removed.2419if (elem && elem.getBoundingClientRect) { // check if available2420var rect = elem.getBoundingClientRect();2421offset.top = rect.top;2422offset.left = rect.left;2423if (!relativeToViewport) { // clientRect is by default relative to viewport...2424offset.top += (window.pageYOffset || document.scrollTop || 0) - (document.clientTop || 0);2425offset.left += (window.pageXOffset || document.scrollLeft || 0) - (document.clientLeft || 0);2426}2427}2428return offset;2429};2430var isDomElement = function (o){2431return (2432typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM22433o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName==="string"2434);2435};2436var isMarginCollapseType = function (str) {2437return ["block", "flex", "list-item", "table", "-webkit-box"].indexOf(str) > -1;2438};2439// implementation of requestAnimationFrame2440var animationFrameCallback = window.requestAnimationFrame;2441var animationFrameCancelCallback = window.cancelAnimationFrame;24422443// polyfill -> based on https://gist.github.com/paulirish/15796712444(function (window) {2445var2446lastTime = 0,2447vendors = ['ms', 'moz', 'webkit', 'o'],2448i;24492450// try vendor prefixes if the above doesn't work2451for (i = 0; !animationFrameCallback && i < vendors.length; ++i) {2452animationFrameCallback = window[vendors[i] + 'RequestAnimationFrame'];2453animationFrameCancelCallback = window[vendors[i] + 'CancelAnimationFrame'] || window[vendors[i] + 'CancelRequestAnimationFrame'];2454}24552456// fallbacks2457if (!animationFrameCallback) {2458animationFrameCallback = function (callback) {2459var2460currTime = new Date().getTime(),2461timeToCall = Math.max(0, 16 - (currTime - lastTime)),2462id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall);2463lastTime = currTime + timeToCall;2464return id;2465};2466}2467if (!animationFrameCancelCallback) {2468animationFrameCancelCallback = function (id) {2469window.clearTimeout(id);2470};2471}2472}(window));24732474return {2475Controller: ScrollMagic,2476Scene: ScrollScene,2477};2478}));247924802481