Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/website/GAUSS/js/jquery.scrollmagic.js
2941 views
1
/*
2
ScrollMagic v1.3.0
3
The jQuery plugin for doing magical scroll interactions.
4
(c) 2014 Jan Paepke (@janpaepke)
5
License & Info: http://janpaepke.github.io/ScrollMagic
6
7
Inspired by and partially based on SUPERSCROLLORAMA by John Polacek (@johnpolacek)
8
http://johnpolacek.github.com/superscrollorama/
9
10
Powered by the Greensock Tweening Platform (GSAP): http://www.greensock.com/js
11
Greensock License info at http://www.greensock.com/licensing/
12
*/
13
/**
14
@overview ##Info
15
@version 1.3.0
16
@license Dual licensed under MIT license and GPL.
17
@author Jan Paepke - [email protected]
18
19
@todo: enhancement: remove dependencies and move to plugins -> 2.0
20
@todo: bug: when cascading pins (pinning one element multiple times) and later removing them without reset, positioning errors occur.
21
@todo: bug: having multiple scroll directions with cascaded pins doesn't work (one scroll vertical, one horizontal)
22
@todo: feature: optimize performance on debug plugin (huge drawbacks, when using many scenes)
23
*/
24
(function (root, factory) {
25
if (typeof define === 'function' && define.amd) {
26
// AMD. Register as an anonymous module.
27
define(['jquery', 'TweenMax', 'TimelineMax'], factory);
28
} else {
29
// Browser globals
30
var sm = factory(root.jQuery, root.TweenMax, root.TimelineMax);
31
root.ScrollMagic = sm.Controller;
32
root.ScrollScene = sm.Scene;
33
}
34
}(this, function ($, TweenMax, TimelineMax) {
35
36
/**
37
* The main class that is needed once per scroll container.
38
*
39
* @class
40
* @global
41
*
42
* @example
43
* // basic initialization
44
* var controller = new ScrollMagic();
45
*
46
* // passing options
47
* var controller = new ScrollMagic({container: "#myContainer", loglevel: 3});
48
*
49
* @param {object} [options] - An object containing one or more options for the controller.
50
* @param {(string|object)} [options.container=window] - A selector, DOM object or a jQuery object that references the main container for scrolling.
51
* @param {boolean} [options.vertical=true] - Sets the scroll mode to vertical (`true`) or horizontal (`false`) scrolling.
52
* @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}.
53
* @param {number} [options.loglevel=2] Loglevel for debugging. Note that logging is disabled in the minified version of ScrollMagic.
54
** `0` => silent
55
** `1` => errors
56
** `2` => errors, warnings
57
** `3` => errors, warnings, debuginfo
58
* @param {boolean} [options.refreshInterval=100] - Some changes don't call events by default, like changing the container size or moving a scene trigger element.
59
This interval polls these parameters to fire the necessary events.
60
If 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.
61
*
62
*/
63
var ScrollMagic = function(options) {
64
65
/*
66
* ----------------------------------------------------------------
67
* settings
68
* ----------------------------------------------------------------
69
*/
70
var
71
NAMESPACE = "ScrollMagic",
72
DEFAULT_OPTIONS = {
73
container: window,
74
vertical: true,
75
globalSceneOptions: {},
76
loglevel: 2,
77
refreshInterval: 100
78
};
79
80
/*
81
* ----------------------------------------------------------------
82
* private vars
83
* ----------------------------------------------------------------
84
*/
85
86
var
87
ScrollMagic = this,
88
_options = $.extend({}, DEFAULT_OPTIONS, options),
89
_sceneObjects = [],
90
_updateScenesOnNextCycle = false, // can be boolean (true => all scenes) or an array of scenes to be updated
91
_scrollPos = 0,
92
_scrollDirection = "PAUSED",
93
_isDocument = true,
94
_viewPortSize = 0,
95
_enabled = true,
96
_updateCycle,
97
_refreshInterval;
98
99
/*
100
* ----------------------------------------------------------------
101
* private functions
102
* ----------------------------------------------------------------
103
*/
104
105
/**
106
* Internal constructor function of ScrollMagic
107
* @private
108
*/
109
var construct = function () {
110
ScrollMagic.version = ScrollMagic.constructor.version;
111
$.each(_options, function (key, value) {
112
if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {
113
log(2, "WARNING: Unknown option \"" + key + "\"");
114
delete _options[key];
115
}
116
});
117
_options.container = $(_options.container).first();
118
// check ScrollContainer
119
if (_options.container.length === 0) {
120
log(1, "ERROR creating object " + NAMESPACE + ": No valid scroll container supplied");
121
throw NAMESPACE + " init failed."; // cancel
122
}
123
_isDocument = !$.contains(document, _options.container.get(0));
124
// prevent bubbling of fake resize event to window
125
if (!_isDocument) {
126
_options.container.on('resize', function ( e ) {
127
e.stopPropagation();
128
});
129
}
130
// update container size immediately
131
_viewPortSize = _options.vertical ? _options.container.height() : _options.container.width();
132
// set event handlers
133
_options.container.on("scroll resize", onChange);
134
135
_options.refreshInterval = parseInt(_options.refreshInterval);
136
if (_options.refreshInterval > 0) {
137
_refreshInterval = window.setInterval(refresh, _options.refreshInterval);
138
}
139
140
// start checking for changes
141
_updateCycle = animationFrameCallback(updateScenes);
142
log(3, "added new " + NAMESPACE + " controller (v" + ScrollMagic.version + ")");
143
};
144
145
/**
146
* Default function to get scroll pos - overwriteable using `ScrollMagic.scrollPos(newFunction)`
147
* @private
148
*/
149
var getScrollPos = function () {
150
return _options.vertical ? _options.container.scrollTop() : _options.container.scrollLeft();
151
};
152
/**
153
* Default function to set scroll pos - overwriteable using `ScrollMagic.scrollTo(newFunction)`
154
* @private
155
*/
156
var setScrollPos = function (pos) {
157
if (_options.vertical) {
158
_options.container.scrollTop(pos);
159
} else {
160
_options.container.scrollLeft(pos);
161
}
162
};
163
164
/**
165
* Handle updates in cycles instead of on scroll (performance)
166
* @private
167
*/
168
var updateScenes = function () {
169
_updateCycle = animationFrameCallback(updateScenes);
170
if (_enabled && _updateScenesOnNextCycle) {
171
var
172
scenesToUpdate = $.isArray(_updateScenesOnNextCycle) ? _updateScenesOnNextCycle : _sceneObjects.slice(0),
173
oldScrollPos = _scrollPos;
174
// update scroll pos & direction
175
_scrollPos = ScrollMagic.scrollPos();
176
var deltaScroll = _scrollPos - oldScrollPos;
177
_scrollDirection = (deltaScroll === 0) ? "PAUSED" : (deltaScroll > 0) ? "FORWARD" : "REVERSE";
178
if (deltaScroll < 0) { // reverse order if scrolling reverse
179
scenesToUpdate.reverse();
180
}
181
// update scenes
182
$.each(scenesToUpdate, function (index, scene) {
183
log(3, "updating Scene " + (index + 1) + "/" + scenesToUpdate.length + " (" + _sceneObjects.length + " total)");
184
scene.update(true);
185
});
186
if (scenesToUpdate.length === 0 && _options.loglevel >= 3) {
187
log(3, "updating 0 Scenes (nothing added to controller)");
188
}
189
_updateScenesOnNextCycle = false;
190
}
191
};
192
193
/**
194
* Handles Container changes
195
* @private
196
*/
197
var onChange = function (e) {
198
if (e.type == "resize") {
199
_viewPortSize = _options.vertical ? _options.container.height() : _options.container.width();
200
}
201
_updateScenesOnNextCycle = true;
202
};
203
204
var refresh = function () {
205
if (!_isDocument) {
206
// simulate resize event. Only works for viewport relevant param
207
if (_viewPortSize != (_options.vertical ? _options.container.height() : _options.container.width())) {
208
_options.container.trigger("resize");
209
}
210
}
211
$.each(_sceneObjects, function (index, scene) {// refresh all scenes
212
scene.refresh();
213
});
214
};
215
216
/**
217
* Send a debug message to the console.
218
* @private
219
*
220
* @param {number} loglevel - The loglevel required to initiate output for the message.
221
* @param {...mixed} output - One or more variables that should be passed to the console.
222
*/
223
var log = function (loglevel, output) {
224
if (_options.loglevel >= loglevel) {
225
var
226
prefix = "(" + NAMESPACE + ") ->",
227
args = Array.prototype.splice.call(arguments, 1);
228
args.unshift(loglevel, prefix);
229
debug.apply(window, args);
230
}
231
};
232
233
/**
234
* Sort scenes in ascending order of their start offset.
235
* @private
236
*
237
* @param {array} ScrollScenesArray - an array of ScrollScenes that should be sorted
238
* @return {array} The sorted array of ScrollScenes.
239
*/
240
var sortScenes = function (ScrollScenesArray) {
241
if (ScrollScenesArray.length <= 1) {
242
return ScrollScenesArray;
243
} else {
244
var scenes = ScrollScenesArray.slice(0);
245
scenes.sort(function(a, b) {
246
return a.scrollOffset() > b.scrollOffset() ? 1 : -1;
247
});
248
return scenes;
249
}
250
};
251
252
/*
253
* ----------------------------------------------------------------
254
* public functions
255
* ----------------------------------------------------------------
256
*/
257
258
/**
259
* Add one ore more scene(s) to the controller.
260
* This is the equivalent to `ScrollScene.addTo(controller)`.
261
* @public
262
* @example
263
* // with a previously defined scene
264
* controller.addScene(scene);
265
*
266
* // with a newly created scene.
267
* controller.addScene(new ScrollScene({duration : 0}));
268
*
269
* // adding multiple scenes
270
* controller.addScene([scene, scene2, new ScrollScene({duration : 0})]);
271
*
272
* @param {(ScrollScene|array)} ScrollScene - ScrollScene or Array of ScrollScenes to be added to the controller.
273
* @return {ScrollMagic} Parent object for chaining.
274
*/
275
this.addScene = function (newScene) {
276
if ($.isArray(newScene)) {
277
$.each(newScene, function (index, scene) {
278
ScrollMagic.addScene(scene);
279
});
280
} else if (newScene instanceof ScrollScene) {
281
if (newScene.parent() != ScrollMagic) {
282
newScene.addTo(ScrollMagic);
283
} else if ($.inArray(newScene, _sceneObjects) < 0){
284
// new scene
285
_sceneObjects.push(newScene); // add to array
286
_sceneObjects = sortScenes(_sceneObjects); // sort
287
newScene.on("shift." + NAMESPACE + "_sort", function() { // resort whenever scene moves
288
_sceneObjects = sortScenes(_sceneObjects);
289
});
290
// insert Global defaults.
291
$.each(_options.globalSceneOptions, function (key, value) {
292
if (newScene[key]) {
293
newScene[key].call(newScene, value);
294
}
295
});
296
log(3, "added Scene (" + _sceneObjects.length + " total)");
297
}
298
} else {
299
log(1, "ERROR: invalid argument supplied for '.addScene()'");
300
}
301
return ScrollMagic;
302
};
303
304
/**
305
* Remove one ore more scene(s) from the controller.
306
* This is the equivalent to `ScrollScene.remove()`.
307
* @public
308
* @example
309
* // remove a scene from the controller
310
* controller.removeScene(scene);
311
*
312
* // remove multiple scenes from the controller
313
* controller.removeScene([scene, scene2, scene3]);
314
*
315
* @param {(ScrollScene|array)} ScrollScene - ScrollScene or Array of ScrollScenes to be removed from the controller.
316
* @returns {ScrollMagic} Parent object for chaining.
317
*/
318
this.removeScene = function (ScrollScene) {
319
if ($.isArray(ScrollScene)) {
320
$.each(ScrollScene, function (index, scene) {
321
ScrollMagic.removeScene(scene);
322
});
323
} else {
324
var index = $.inArray(ScrollScene, _sceneObjects);
325
if (index > -1) {
326
ScrollScene.off("shift." + NAMESPACE + "_sort");
327
_sceneObjects.splice(index, 1);
328
ScrollScene.remove();
329
log(3, "removed Scene (" + _sceneObjects.length + " total)");
330
}
331
}
332
return ScrollMagic;
333
};
334
335
/**
336
* Update one ore more scene(s) according to the scroll position of the container.
337
* This is the equivalent to `ScrollScene.update()`.
338
* 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.
339
* 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.
340
* _**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._
341
* @public
342
* @example
343
* // update a specific scene on next cycle
344
* controller.updateScene(scene);
345
*
346
* // update a specific scene immediately
347
* controller.updateScene(scene, true);
348
*
349
* // update multiple scenes scene on next cycle
350
* controller.updateScene([scene1, scene2, scene3]);
351
*
352
* @param {ScrollScene} ScrollScene - ScrollScene or Array of ScrollScenes that is/are supposed to be updated.
353
* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next update cycle.
354
This is useful when changing multiple properties of the scene - this way it will only be updated once all new properties are set (updateScenes).
355
* @return {ScrollMagic} Parent object for chaining.
356
*/
357
this.updateScene = function (ScrollScene, immediately) {
358
if ($.isArray(ScrollScene)) {
359
$.each(ScrollScene, function (index, scene) {
360
ScrollMagic.updateScene(scene, immediately);
361
});
362
} else {
363
if (immediately) {
364
ScrollScene.update(true);
365
} else {
366
// prep array for next update cycle
367
if (!$.isArray(_updateScenesOnNextCycle)) {
368
_updateScenesOnNextCycle = [];
369
}
370
if ($.inArray(ScrollScene, _updateScenesOnNextCycle) == -1) {
371
_updateScenesOnNextCycle.push(ScrollScene);
372
}
373
_updateScenesOnNextCycle = sortScenes(_updateScenesOnNextCycle); // sort
374
}
375
}
376
return ScrollMagic;
377
};
378
379
/**
380
* Updates the controller params and calls updateScene on every scene, that is attached to the controller.
381
* See `ScrollMagic.updateScene()` for more information about what this means.
382
* 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.
383
* The only application for this method is when ScrollMagic fails to detect these events.
384
* 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.
385
* 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.
386
* @public
387
* @example
388
* // update the controller on next cycle (saves performance due to elimination of redundant updates)
389
* controller.update();
390
*
391
* // update the controller immediately
392
* controller.update(true);
393
*
394
* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next update cycle (better performance)
395
* @return {ScrollMagic} Parent object for chaining.
396
*/
397
this.update = function (immediately) {
398
onChange({type: "resize"}); // will update size and set _updateScenesOnNextCycle to true
399
if (immediately) {
400
updateScenes();
401
}
402
return ScrollMagic;
403
};
404
405
/**
406
* Scroll to a numeric scroll offset, a DOM element, the start of a scene or provide an alternate method for scrolling.
407
* For vertical controllers it will change the top scroll offset and for horizontal applications it will change the left offset.
408
* @public
409
*
410
* @since 1.1.0
411
* @example
412
* // scroll to an offset of 100
413
* controller.scrollTo(100);
414
*
415
* // scroll to a DOM element
416
* controller.scrollTo("#anchor");
417
*
418
* // scroll to the beginning of a scene
419
* var scene = new ScrollScene({offset: 200});
420
* controller.scrollTo(scene);
421
*
422
* // define a new scroll position modification function (animate instead of jump)
423
* controller.scrollTo(function (newScrollPos) {
424
* $("body").animate({scrollTop: newScrollPos});
425
* });
426
*
427
* @param {mixed} [scrollTarget] - The supplied argument can be one of these types:
428
* 1. `number` -> The container will scroll to this new scroll offset.
429
* 2. `string` or `object` -> Can be a selector, a DOM object or a jQuery element.
430
* The container will scroll to the position of this element.
431
* 3. `ScrollScene` -> The container will scroll to the start of this scene.
432
* 4. `function` -> This function will be used as a callback for future scroll position modifications.
433
* 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`.
434
* _**NOTE:** All other options will still work as expected, using the new function to scroll._
435
* @returns {ScrollMagic} Parent object for chaining.
436
*/
437
this.scrollTo = function (scrollTarget) {
438
if (scrollTarget instanceof ScrollScene) {
439
if (scrollTarget.parent() === ScrollMagic) { // check if this controller is the parent
440
ScrollMagic.scrollTo(scrollTarget.scrollOffset());
441
} else {
442
log (2, "scrollTo(): The supplied scene does not belong to this controller. Scroll cancelled.", scrollTarget);
443
}
444
} else if ($.type(scrollTarget) === "string" || isDomElement(scrollTarget) || scrollTarget instanceof $) {
445
var $elm = $(scrollTarget).first();
446
if ($elm[0]) {
447
var
448
param = _options.vertical ? "top" : "left", // which param is of interest ?
449
containerOffset = getOffset(_options.container), // container position is needed because element offset is returned in relation to document, not in relation to container.
450
elementOffset = getOffset($elm);
451
452
if (!_isDocument) { // container is not the document root, so substract scroll Position to get correct trigger element position relative to scrollcontent
453
containerOffset[param] -= ScrollMagic.scrollPos();
454
}
455
456
ScrollMagic.scrollTo(elementOffset[param] - containerOffset[param]);
457
} else {
458
log (2, "scrollTo(): The supplied element could not be found. Scroll cancelled.", scrollTarget);
459
}
460
} else if ($.isFunction(scrollTarget)) {
461
setScrollPos = scrollTarget;
462
} else {
463
setScrollPos.call(_options.container[0], scrollTarget);
464
}
465
return ScrollMagic;
466
};
467
468
/**
469
* **Get** the current scrollPosition or **Set** a new method to calculate it.
470
* -> **GET**:
471
* When used as a getter this function will return the current scroll position.
472
* To get a cached value use ScrollMagic.info("scrollPos"), which will be updated in the update cycle.
473
* For vertical controllers it will return the top scroll offset and for horizontal applications it will return the left offset.
474
*
475
* -> **SET**:
476
* When used as a setter this method prodes a way to permanently overwrite the controller's scroll position calculation.
477
* 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.
478
* Moving a child container inside a parent is a commonly used method for several scrolling frameworks, including iScroll.
479
* By providing an alternate calculation function you can make sure ScrollMagic receives the correct scroll position.
480
* Please also bear in mind that your function should return y values for vertical scrolls an x for horizontals.
481
*
482
* To change the current scroll position please use `ScrollMagic.scrollTo()`.
483
* @public
484
*
485
* @example
486
* // get the current scroll Position
487
* var scrollPos = controller.scrollPos();
488
*
489
* // set a new scroll position calculation method
490
* controller.scrollPos(function () {
491
* return this.info("vertical") ? -$mychildcontainer.y : -$mychildcontainer.x
492
* });
493
*
494
* @param {function} [scrollPosMethod] - The function to be used for the scroll position calculation of the container.
495
* @returns {(number|ScrollMagic)} Current scroll position or parent object for chaining.
496
*/
497
this.scrollPos = function (scrollPosMethod) {
498
if (!arguments.length) { // get
499
return getScrollPos.call(ScrollMagic);
500
} else { // set
501
if ($.isFunction(scrollPosMethod)) {
502
getScrollPos = scrollPosMethod;
503
} else {
504
log(2, "Provided value for method 'scrollPos' is not a function. To change the current scroll position use 'scrollTo()'.");
505
}
506
}
507
return ScrollMagic;
508
};
509
510
/**
511
* **Get** all infos or one in particular about the controller.
512
* @public
513
* @example
514
* // returns the current scroll position (number)
515
* var scrollPos = controller.info("scrollPos");
516
*
517
* // returns all infos as an object
518
* var infos = controller.info();
519
*
520
* @param {string} [about] - If passed only this info will be returned instead of an object containing all.
521
Valid options are:
522
** `"size"` => the current viewport size of the container
523
** `"vertical"` => true if vertical scrolling, otherwise false
524
** `"scrollPos"` => the current scroll position
525
** `"scrollDirection"` => the last known direction of the scroll
526
** `"container"` => the container element
527
** `"isDocument"` => true if container element is the document.
528
* @returns {(mixed|object)} The requested info(s).
529
*/
530
this.info = function (about) {
531
var values = {
532
size: _viewPortSize, // contains height or width (in regard to orientation);
533
vertical: _options.vertical,
534
scrollPos: _scrollPos,
535
scrollDirection: _scrollDirection,
536
container: _options.container,
537
isDocument: _isDocument
538
};
539
if (!arguments.length) { // get all as an object
540
return values;
541
} else if (values[about] !== undefined) {
542
return values[about];
543
} else {
544
log(1, "ERROR: option \"" + about + "\" is not available");
545
return;
546
}
547
};
548
549
/**
550
* **Get** or **Set** the current loglevel option value.
551
* @public
552
*
553
* @example
554
* // get the current value
555
* var loglevel = controller.loglevel();
556
*
557
* // set a new value
558
* controller.loglevel(3);
559
*
560
* @param {number} [newLoglevel] - The new loglevel setting of the ScrollMagic controller. `[0-3]`
561
* @returns {(number|ScrollMagic)} Current loglevel or parent object for chaining.
562
*/
563
this.loglevel = function (newLoglevel) {
564
if (!arguments.length) { // get
565
return _options.loglevel;
566
} else if (_options.loglevel != newLoglevel) { // set
567
_options.loglevel = newLoglevel;
568
}
569
return ScrollMagic;
570
};
571
572
/**
573
* **Get** or **Set** the current enabled state of the controller.
574
* This can be used to disable all Scenes connected to the controller without destroying or removing them.
575
* @public
576
*
577
* @example
578
* // get the current value
579
* var enabled = controller.enabled();
580
*
581
* // disable the controller
582
* controller.enabled(false);
583
*
584
* @param {boolean} [newState] - The new enabled state of the controller `true` or `false`.
585
* @returns {(boolean|ScrollMagic)} Current enabled state or parent object for chaining.
586
*/
587
this.enabled = function (newState) {
588
if (!arguments.length) { // get
589
return _enabled;
590
} else if (_enabled != newState) { // set
591
_enabled = !!newState;
592
ScrollMagic.updateScene(_sceneObjects, true);
593
}
594
return ScrollMagic;
595
};
596
597
/**
598
* Destroy the Controller, all Scenes and everything.
599
* @public
600
*
601
* @example
602
* // without resetting the scenes
603
* controller = controller.destroy();
604
*
605
* // with scene reset
606
* controller = controller.destroy(true);
607
*
608
* @param {boolean} [resetScenes=false] - If `true` the pins and tweens (if existent) of all scenes will be reset.
609
* @returns {null} Null to unset handler variables.
610
*/
611
this.destroy = function (resetScenes) {
612
window.clearTimeout(_refreshInterval);
613
var i = _sceneObjects.length;
614
while (i--) {
615
_sceneObjects[i].destroy(resetScenes);
616
}
617
_options.container.off("scroll resize", onChange);
618
animationFrameCancelCallback(_updateCycle);
619
log(3, "destroyed " + NAMESPACE + " (reset: " + (resetScenes ? "true" : "false") + ")");
620
return null;
621
};
622
623
// INIT
624
construct();
625
return ScrollMagic;
626
};
627
ScrollMagic.version = "1.3.0"; // version number for browser global
628
629
/**
630
* A ScrollScene defines where the controller should react and how.
631
*
632
* @class
633
* @global
634
*
635
* @example
636
* // create a standard scene and add it to a controller
637
* new ScrollScene()
638
* .addTo(controller);
639
*
640
* // create a scene with custom options and assign a handler to it.
641
* var scene = new ScrollScene({
642
* duration: 100,
643
* offset: 200,
644
* triggerHook: "onEnter",
645
* reverse: false
646
* });
647
*
648
* @param {object} [options] - Options for the Scene. The options can be updated at any time.
649
Instead 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.
650
When a scene is added to the controller the options defined using the ScrollScene constructor will be overwritten by those set in `globalSceneOptions`.
651
* @param {(number|function)} [options.duration=0] - The duration of the scene.
652
If `0` tweens will auto-play when reaching the scene start point, pins will be pinned indefinetly starting at the start position.
653
A function retuning the duration value is also supported. Please see `ScrollScene.duration()` for details.
654
* @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.
655
* @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).
656
* @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.
657
Can also be defined using a string:
658
** `"onEnter"` => `1`
659
** `"onCenter"` => `0.5`
660
** `"onLeave"` => `0`
661
* @param {boolean} [options.reverse=true] - Should the scene reverse, when scrolling up?
662
* @param {boolean} [options.tweenChanges=false] - Tweens Animation to the progress target instead of setting it.
663
Does not affect animations where duration is `0`.
664
* @param {number} [options.loglevel=2] - Loglevel for debugging. Note that logging is disabled in the minified version of ScrollMagic.
665
** `0` => silent
666
** `1` => errors
667
** `2` => errors, warnings
668
** `3` => errors, warnings, debuginfo
669
*
670
*/
671
var ScrollScene = function (options) {
672
673
/*
674
* ----------------------------------------------------------------
675
* settings
676
* ----------------------------------------------------------------
677
*/
678
679
var
680
TRIGGER_HOOK_VALUES = {"onCenter" : 0.5, "onEnter" : 1, "onLeave" : 0},
681
NAMESPACE = "ScrollScene",
682
DEFAULT_OPTIONS = {
683
duration: 0,
684
offset: 0,
685
triggerElement: null,
686
triggerHook: "onCenter",
687
reverse: true,
688
tweenChanges: false,
689
loglevel: 2
690
};
691
692
/*
693
* ----------------------------------------------------------------
694
* private vars
695
* ----------------------------------------------------------------
696
*/
697
698
var
699
ScrollScene = this,
700
_options = $.extend({}, DEFAULT_OPTIONS, options),
701
_state = 'BEFORE',
702
_progress = 0,
703
_scrollOffset = {start: 0, end: 0}, // reflects the parent's scroll position for the start and end of the scene respectively
704
_triggerPos = 0,
705
_enabled = true,
706
_durationUpdateMethod,
707
_parent,
708
_tween,
709
_pin,
710
_pinOptions,
711
_cssClasses,
712
_cssClassElm;
713
714
// object containing validator functions for various options
715
var _validate = {
716
"unknownOptionSupplied" : function () {
717
$.each(_options, function (key, value) {
718
if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {
719
log(2, "WARNING: Unknown option \"" + key + "\"");
720
delete _options[key];
721
}
722
});
723
},
724
"duration" : function () {
725
if ($.isFunction(_options.duration)) {
726
_durationUpdateMethod = _options.duration;
727
try {
728
_options.duration = parseFloat(_durationUpdateMethod());
729
} catch (e) {
730
log(1, "ERROR: Invalid return value of supplied function for option \"duration\":", _options.duration);
731
_durationUpdateMethod = undefined;
732
_options.duration = DEFAULT_OPTIONS.duration;
733
}
734
} else {
735
_options.duration = parseFloat(_options.duration);
736
if (!$.isNumeric(_options.duration) || _options.duration < 0) {
737
log(1, "ERROR: Invalid value for option \"duration\":", _options.duration);
738
_options.duration = DEFAULT_OPTIONS.duration;
739
}
740
}
741
},
742
"offset" : function () {
743
_options.offset = parseFloat(_options.offset);
744
if (!$.isNumeric(_options.offset)) {
745
log(1, "ERROR: Invalid value for option \"offset\":", _options.offset);
746
_options.offset = DEFAULT_OPTIONS.offset;
747
}
748
},
749
"triggerElement" : function () {
750
if (_options.triggerElement !== null && $(_options.triggerElement).length === 0) {
751
log(1, "ERROR: Element defined in option \"triggerElement\" was not found:", _options.triggerElement);
752
_options.triggerElement = DEFAULT_OPTIONS.triggerElement;
753
}
754
},
755
"triggerHook" : function () {
756
if (!(_options.triggerHook in TRIGGER_HOOK_VALUES)) {
757
if ($.isNumeric(_options.triggerHook)) {
758
_options.triggerHook = Math.max(0, Math.min(parseFloat(_options.triggerHook), 1)); // make sure its betweeen 0 and 1
759
} else {
760
log(1, "ERROR: Invalid value for option \"triggerHook\": ", _options.triggerHook);
761
_options.triggerHook = DEFAULT_OPTIONS.triggerHook;
762
}
763
}
764
},
765
"reverse" : function () {
766
_options.reverse = !!_options.reverse; // force boolean
767
},
768
"tweenChanges" : function () {
769
_options.tweenChanges = !!_options.tweenChanges; // force boolean
770
},
771
"loglevel" : function () {
772
_options.loglevel = parseInt(_options.loglevel);
773
if (!$.isNumeric(_options.loglevel) || _options.loglevel < 0 || _options.loglevel > 3) {
774
var wrongval = _options.loglevel;
775
_options.loglevel = DEFAULT_OPTIONS.loglevel;
776
log(1, "ERROR: Invalid value for option \"loglevel\":", wrongval);
777
}
778
},
779
};
780
781
/*
782
* ----------------------------------------------------------------
783
* private functions
784
* ----------------------------------------------------------------
785
*/
786
787
/**
788
* Internal constructor function of ScrollMagic
789
* @private
790
*/
791
var construct = function () {
792
validateOption();
793
794
// event listeners
795
ScrollScene
796
.on("change.internal", function (e) {
797
if (e.what !== "loglevel" && e.what !== "tweenChanges") { // no need for a scene update scene with these options...
798
if (e.what === "triggerElement") {
799
updateTriggerElementPosition();
800
} 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.
801
ScrollScene.update();
802
}
803
}
804
})
805
.on("shift.internal", function (e) {
806
updateScrollOffset();
807
ScrollScene.update(); // update scene to reflect new position
808
if ((_state === "AFTER" && e.reason === "duration") || (_state === 'DURING' && _options.duration === 0)) {
809
// 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].
810
updatePinState();
811
}
812
})
813
.on("progress.internal", function (e) {
814
updateTweenProgress();
815
updatePinState();
816
})
817
.on("destroy", function (e) {
818
e.preventDefault(); // otherwise jQuery would call target.destroy() by default.
819
});
820
};
821
822
/**
823
* Send a debug message to the console.
824
* @private
825
*
826
* @param {number} loglevel - The loglevel required to initiate output for the message.
827
* @param {...mixed} output - One or more variables that should be passed to the console.
828
*/
829
var log = function (loglevel, output) {
830
if (_options.loglevel >= loglevel) {
831
var
832
prefix = "(" + NAMESPACE + ") ->",
833
args = Array.prototype.splice.call(arguments, 1);
834
args.unshift(loglevel, prefix);
835
debug.apply(window, args);
836
}
837
};
838
839
/**
840
* Checks the validity of a specific or all options and reset to default if neccessary.
841
* @private
842
*/
843
var validateOption = function (check) {
844
if (!arguments.length) {
845
check = [];
846
for (var key in _validate){
847
check.push(key);
848
}
849
} else if (!$.isArray(check)) {
850
check = [check];
851
}
852
$.each(check, function (key, value) {
853
if (_validate[value]) {
854
_validate[value]();
855
}
856
});
857
};
858
859
/**
860
* Helper used by the setter/getters for scene options
861
* @private
862
*/
863
var changeOption = function(varname, newval) {
864
var
865
changed = false,
866
oldval = _options[varname];
867
if (_options[varname] != newval) {
868
_options[varname] = newval;
869
validateOption(varname); // resets to default if necessary
870
changed = oldval != _options[varname];
871
}
872
return changed;
873
};
874
875
/**
876
* Update the start and end scrollOffset of the container.
877
* The positions reflect what the parent's scroll position will be at the start and end respectively.
878
* Is called, when:
879
* - ScrollScene event "change" is called with: offset, triggerHook, duration
880
* - scroll container event "resize" is called
881
* - the position of the triggerElement changes
882
* - the parent changes -> addTo()
883
* @private
884
*/
885
var updateScrollOffset = function () {
886
_scrollOffset = {start: _triggerPos + _options.offset};
887
if (_parent && _options.triggerElement) {
888
// take away triggerHook portion to get relative to top
889
_scrollOffset.start -= _parent.info("size") * ScrollScene.triggerHook();
890
}
891
_scrollOffset.end = _scrollOffset.start + _options.duration;
892
};
893
894
/**
895
* Updates the duration if set to a dynamic function.
896
* This method is called when the scene is added to a controller and in regular intervals from the controller through scene.refresh().
897
*
898
* @fires {@link ScrollScene.change}, if the duration changed
899
* @fires {@link ScrollScene.shift}, if the duration changed
900
*
901
* @param {boolean} [suppressEvents=false] - If true the shift event will be suppressed.
902
* @private
903
*/
904
var updateDuration = function (suppressEvents) {
905
// update duration
906
if (_durationUpdateMethod) {
907
var varname = "duration";
908
if (changeOption(varname, _durationUpdateMethod.call(ScrollScene)) && !suppressEvents) { // set
909
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
910
ScrollScene.trigger("shift", {reason: varname});
911
}
912
}
913
};
914
915
/**
916
* Updates the position of the triggerElement, if present.
917
* This method is called ...
918
* - ... when the triggerElement is changed
919
* - ... when the scene is added to a (new) controller
920
* - ... in regular intervals from the controller through scene.refresh().
921
*
922
* @fires {@link ScrollScene.shift}, if the position changed
923
*
924
* @param {boolean} [suppressEvents=false] - If true the shift event will be suppressed.
925
* @private
926
*/
927
var updateTriggerElementPosition = function (suppressEvents) {
928
var elementPos = 0;
929
if (_parent && _options.triggerElement) {
930
var
931
element = $(_options.triggerElement).first(),
932
controllerInfo = _parent.info(),
933
containerOffset = getOffset(controllerInfo.container), // container position is needed because element offset is returned in relation to document, not in relation to container.
934
param = controllerInfo.vertical ? "top" : "left"; // which param is of interest ?
935
936
// if parent is spacer, use spacer position instead so correct start position is returned for pinned elements.
937
while (element.parent().data("ScrollMagicPinSpacer")) {
938
element = element.parent();
939
}
940
941
var elementOffset = getOffset(element);
942
943
if (!controllerInfo.isDocument) { // container is not the document root, so substract scroll Position to get correct trigger element position relative to scrollcontent
944
containerOffset[param] -= _parent.scrollPos();
945
}
946
947
elementPos = elementOffset[param] - containerOffset[param];
948
}
949
var changed = elementPos != _triggerPos;
950
_triggerPos = elementPos;
951
if (changed && !suppressEvents) {
952
ScrollScene.trigger("shift", {reason: "triggerElementPosition"});
953
}
954
};
955
956
/**
957
* Update the tween progress.
958
* @private
959
*
960
* @param {number} [to] - If not set the scene Progress will be used. (most cases)
961
* @return {boolean} true if the Tween was updated.
962
*/
963
var updateTweenProgress = function (to) {
964
if (_tween) {
965
var progress = (to >= 0 && to <= 1) ? to : _progress;
966
if (_tween.repeat() === -1) {
967
// infinite loop, so not in relation to progress
968
if (_state === "DURING" && _tween.paused()) {
969
_tween.play();
970
} else if (_state !== "DURING" && !_tween.paused()) {
971
_tween.pause();
972
} else {
973
return false;
974
}
975
} else if (progress != _tween.progress()) { // do we even need to update the progress?
976
// no infinite loop - so should we just play or go to a specific point in time?
977
if (_options.duration === 0) {
978
// play the animation
979
if (_state === "DURING") { // play from 0 to 1
980
_tween.play();
981
} else { // play from 1 to 0
982
_tween.reverse();
983
}
984
} else {
985
// go to a specific point in time
986
if (_options.tweenChanges) {
987
// go smooth
988
_tween.tweenTo(progress * _tween.duration());
989
} else {
990
// just hard set it
991
_tween.progress(progress).pause();
992
}
993
}
994
} else {
995
return false;
996
}
997
return true;
998
} else {
999
return false;
1000
}
1001
};
1002
1003
/**
1004
* Update the pin state.
1005
* @private
1006
*/
1007
var updatePinState = function (forceUnpin) {
1008
if (_pin && _parent) {
1009
var
1010
containerInfo = _parent.info();
1011
1012
if (!forceUnpin && _state === "DURING") { // during scene or if duration is 0 and we are past the trigger
1013
// pinned state
1014
if (_pin.css("position") != "fixed") {
1015
// change state before updating pin spacer (position changes due to fixed collapsing might occur.)
1016
_pin.css("position", "fixed");
1017
// update pin spacer
1018
updatePinSpacerSize();
1019
// add pinned class
1020
_pin.addClass(_pinOptions.pinnedClass);
1021
}
1022
1023
var
1024
fixedPos = getOffset(_pinOptions.spacer, true), // get viewport position of spacer
1025
scrollDistance = _options.reverse || _options.duration === 0 ?
1026
containerInfo.scrollPos - _scrollOffset.start // quicker
1027
: Math.round(_progress * _options.duration * 10)/10; // if no reverse and during pin the position needs to be recalculated using the progress
1028
1029
// remove spacer margin to get real position (in case marginCollapse mode)
1030
fixedPos.top -= parseFloat(_pinOptions.spacer.css("margin-top"));
1031
1032
// add scrollDistance
1033
fixedPos[containerInfo.vertical ? "top" : "left"] += scrollDistance;
1034
1035
// set new values
1036
_pin.css({
1037
top: fixedPos.top,
1038
left: fixedPos.left
1039
});
1040
} else {
1041
// unpinned state
1042
var
1043
newCSS = {
1044
position: _pinOptions.inFlow ? "relative" : "absolute",
1045
top: 0,
1046
left: 0
1047
},
1048
change = _pin.css("position") != newCSS.position;
1049
1050
if (!_pinOptions.pushFollowers) {
1051
newCSS[containerInfo.vertical ? "top" : "left"] = _options.duration * _progress;
1052
} else if (_options.duration > 0) { // only concerns scenes with duration
1053
if (_state === "AFTER" && parseFloat(_pinOptions.spacer.css("padding-top")) === 0) {
1054
change = true; // if in after state but havent updated spacer yet (jumped past pin)
1055
} else if (_state === "BEFORE" && parseFloat(_pinOptions.spacer.css("padding-bottom")) === 0) { // before
1056
change = true; // jumped past fixed state upward direction
1057
}
1058
}
1059
// set new values
1060
_pin.css(newCSS);
1061
if (change) {
1062
// remove pinned class
1063
_pin.removeClass(_pinOptions.pinnedClass);
1064
// update pin spacer if state changed
1065
updatePinSpacerSize();
1066
}
1067
}
1068
}
1069
};
1070
1071
/**
1072
* Update the pin spacer size.
1073
* The size of the spacer needs to be updated whenever the duration of the scene changes, if it is to push down following elements.
1074
* @private
1075
*/
1076
var updatePinSpacerSize = function () {
1077
if (_pin && _parent && _pinOptions.inFlow) { // no spacerresize, if original position is absolute
1078
var
1079
after = (_state === "AFTER"),
1080
before = (_state === "BEFORE"),
1081
during = (_state === "DURING"),
1082
pinned = (_pin.css("position") == "fixed"),
1083
vertical = _parent.info("vertical"),
1084
$spacercontent = _pinOptions.spacer.children().first(), // usually the pined element but can also be another spacer (cascaded pins)
1085
marginCollapse = isMarginCollapseType(_pinOptions.spacer.css("display")),
1086
css = {};
1087
1088
if (marginCollapse) {
1089
css["margin-top"] = before || (during && pinned) ? _pin.css("margin-top") : "auto";
1090
css["margin-bottom"] = after || (during && pinned) ? _pin.css("margin-bottom") : "auto";
1091
} else {
1092
css["margin-top"] = css["margin-bottom"] = "auto";
1093
}
1094
1095
// set new size
1096
// if relsize: spacer -> pin | else: pin -> spacer
1097
if (_pinOptions.relSize.width || _pinOptions.relSize.autoFullWidth) {
1098
if (pinned) {
1099
if ($(window).width() == _pinOptions.spacer.parent().width()) {
1100
// relative to body
1101
_pin.css("width", _pinOptions.relSize.autoFullWidth ? "100%" : "inherit");
1102
} else {
1103
// not relative to body -> need to calculate
1104
_pin.css("width", _pinOptions.spacer.width());
1105
}
1106
} else {
1107
_pin.css("width", "100%");
1108
}
1109
} else {
1110
// minwidth is needed for cascading pins.
1111
// margin is only included if it's a cascaded pin to resolve an IE9 bug
1112
css["min-width"] = $spacercontent.outerWidth(!$spacercontent.is(_pin));
1113
css.width = pinned ? css["min-width"] : "auto";
1114
}
1115
if (_pinOptions.relSize.height) {
1116
if (pinned) {
1117
if ($(window).height() == _pinOptions.spacer.parent().height()) {
1118
// relative to body
1119
_pin.css("height", "inherit");
1120
} else {
1121
// not relative to body -> need to calculate
1122
_pin.css("height", _pinOptions.spacer.height());
1123
}
1124
} else {
1125
_pin.css("height", "100%");
1126
}
1127
} else {
1128
css["min-height"] = $spacercontent.outerHeight(!marginCollapse); // needed for cascading pins
1129
css.height = pinned ? css["min-height"] : "auto";
1130
}
1131
1132
// add space for duration if pushFollowers is true
1133
if (_pinOptions.pushFollowers) {
1134
css["padding" + (vertical ? "Top" : "Left")] = _options.duration * _progress;
1135
css["padding" + (vertical ? "Bottom" : "Right")] = _options.duration * (1 - _progress);
1136
}
1137
_pinOptions.spacer.css(css);
1138
}
1139
};
1140
1141
/**
1142
* Updates the Pin state (in certain scenarios)
1143
* 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.
1144
* So this function is called on resize and scroll of the document.
1145
* @private
1146
*/
1147
var updatePinInContainer = function () {
1148
if (_parent && _pin && _state === "DURING" && !_parent.info("isDocument")) {
1149
updatePinState();
1150
}
1151
};
1152
1153
/**
1154
* Updates the Pin spacer size state (in certain scenarios)
1155
* If container is resized during pin and relatively sized the size of the pin might need to be updated...
1156
* So this function is called on resize of the container.
1157
* @private
1158
*/
1159
var updateRelativePinSpacer = function () {
1160
if ( _parent && _pin && // well, duh
1161
_state === "DURING" && // element in pinned state?
1162
( // is width or height relatively sized, but not in relation to body? then we need to recalc.
1163
((_pinOptions.relSize.width || _pinOptions.relSize.autoFullWidth) && $(window).width() != _pinOptions.spacer.parent().width()) ||
1164
(_pinOptions.relSize.height && $(window).height() != _pinOptions.spacer.parent().height())
1165
)
1166
) {
1167
updatePinSpacerSize();
1168
}
1169
};
1170
1171
/**
1172
* Is called, when the mousewhel is used while over a pinned element inside a div container.
1173
* If the scene is in fixed state scroll events would be counted towards the body. This forwards the event to the scroll container.
1174
* @private
1175
*/
1176
var onMousewheelOverPin = function (e) {
1177
if (_parent && _pin && _state === "DURING" && !_parent.info("isDocument")) { // in pin state
1178
e.preventDefault();
1179
_parent.scrollTo(_parent.info("scrollPos") - (e.originalEvent.wheelDelta/3 || -e.originalEvent.detail*30));
1180
}
1181
};
1182
1183
1184
/*
1185
* ----------------------------------------------------------------
1186
* public functions (getters/setters)
1187
* ----------------------------------------------------------------
1188
*/
1189
1190
/**
1191
* **Get** the parent controller.
1192
* @public
1193
* @example
1194
* // get the parent controller of a scene
1195
* var controller = scene.parent();
1196
*
1197
* @returns {ScrollMagic} Parent controller or `undefined`
1198
*/
1199
this.parent = function () {
1200
return _parent;
1201
};
1202
1203
1204
/**
1205
* **Get** or **Set** the duration option value.
1206
* As a setter it also accepts a function returning a numeric value.
1207
* This is particularly useful for responsive setups.
1208
*
1209
* The duration is updated using the supplied function every time `ScrollScene.refresh()` is called, which happens periodically from the controller (see ScrollMagic option `refreshInterval`).
1210
* _**NOTE:** Be aware that it's an easy way to kill performance, if you supply a function that has high CPU demand.
1211
* Even for size and position calculations it is recommended to use a variable to cache the value. (see example)
1212
* This counts double if you use the same function for multiple scenes._
1213
*
1214
* @public
1215
* @example
1216
* // get the current duration value
1217
* var duration = scene.duration();
1218
*
1219
* // set a new duration
1220
* scene.duration(300);
1221
*
1222
* // use a function to automatically adjust the duration to the window height.
1223
* var durationValueCache;
1224
* function getDuration () {
1225
* return durationValueCache;
1226
* }
1227
* function updateDuration (e) {
1228
* durationValueCache = $(window).innerHeight();
1229
* }
1230
* $(window).on("resize", updateDuration); // update the duration when the window size changes
1231
* $(window).triggerHandler("resize"); // set to initial value
1232
* scene.duration(getDuration); // supply duration method
1233
*
1234
* @fires {@link ScrollScene.change}, when used as setter
1235
* @fires {@link ScrollScene.shift}, when used as setter
1236
* @param {(number|function)} [newDuration] - The new duration of the scene.
1237
* @returns {number} `get` - Current scene duration.
1238
* @returns {ScrollScene} `set` - Parent object for chaining.
1239
*/
1240
this.duration = function (newDuration) {
1241
var varname = "duration";
1242
if (!arguments.length) { // get
1243
return _options[varname];
1244
} else {
1245
if (!$.isFunction(newDuration)) {
1246
_durationUpdateMethod = undefined;
1247
}
1248
if (changeOption(varname, newDuration)) { // set
1249
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
1250
ScrollScene.trigger("shift", {reason: varname});
1251
}
1252
}
1253
return ScrollScene;
1254
};
1255
1256
/**
1257
* **Get** or **Set** the offset option value.
1258
* @public
1259
* @example
1260
* // get the current offset
1261
* var offset = scene.offset();
1262
*
1263
* // set a new offset
1264
* scene.offset(100);
1265
*
1266
* @fires {@link ScrollScene.change}, when used as setter
1267
* @fires {@link ScrollScene.shift}, when used as setter
1268
* @param {number} [newOffset] - The new offset of the scene.
1269
* @returns {number} `get` - Current scene offset.
1270
* @returns {ScrollScene} `set` - Parent object for chaining.
1271
*/
1272
this.offset = function (newOffset) {
1273
var varname = "offset";
1274
if (!arguments.length) { // get
1275
return _options[varname];
1276
} else if (changeOption(varname, newOffset)) { // set
1277
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
1278
ScrollScene.trigger("shift", {reason: varname});
1279
}
1280
return ScrollScene;
1281
};
1282
1283
/**
1284
* **Get** or **Set** the triggerElement option value.
1285
* 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.
1286
* @public
1287
* @example
1288
* // get the current triggerElement
1289
* var triggerElement = scene.triggerElement();
1290
*
1291
* // set a new triggerElement using a selector
1292
* scene.triggerElement("#trigger");
1293
* // set a new triggerElement using a jQuery Object
1294
* scene.triggerElement($("#trigger"));
1295
* // set a new triggerElement using a DOM object
1296
* scene.triggerElement(document.getElementById("trigger"));
1297
*
1298
* @fires {@link ScrollScene.change}, when used as setter
1299
* @param {(string|object)} [newTriggerElement] - The new trigger element for the scene.
1300
* @returns {(string|object)} `get` - Current triggerElement.
1301
* @returns {ScrollScene} `set` - Parent object for chaining.
1302
*/
1303
this.triggerElement = function (newTriggerElement) {
1304
var varname = "triggerElement";
1305
if (!arguments.length) { // get
1306
return _options[varname];
1307
} else if (changeOption(varname, newTriggerElement)) { // set
1308
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
1309
}
1310
return ScrollScene;
1311
};
1312
1313
/**
1314
* **Get** or **Set** the triggerHook option value.
1315
* @public
1316
* @example
1317
* // get the current triggerHook value
1318
* var triggerHook = scene.triggerHook();
1319
*
1320
* // set a new triggerHook using a string
1321
* scene.triggerHook("onLeave");
1322
* // set a new triggerHook using a number
1323
* scene.triggerHook(0.7);
1324
*
1325
* @fires {@link ScrollScene.change}, when used as setter
1326
* @fires {@link ScrollScene.shift}, when used as setter
1327
* @param {(number|string)} [newTriggerHook] - The new triggerHook of the scene. See {@link ScrollScene} parameter description for value options.
1328
* @returns {number} `get` - Current triggerHook (ALWAYS numerical).
1329
* @returns {ScrollScene} `set` - Parent object for chaining.
1330
*/
1331
this.triggerHook = function (newTriggerHook) {
1332
var varname = "triggerHook";
1333
if (!arguments.length) { // get
1334
return $.isNumeric(_options[varname]) ? _options[varname] : TRIGGER_HOOK_VALUES[_options[varname]];
1335
} else if (changeOption(varname, newTriggerHook)) { // set
1336
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
1337
ScrollScene.trigger("shift", {reason: varname});
1338
}
1339
return ScrollScene;
1340
};
1341
1342
/**
1343
* **Get** or **Set** the reverse option value.
1344
* @public
1345
* @example
1346
* // get the current reverse option
1347
* var reverse = scene.reverse();
1348
*
1349
* // set new reverse option
1350
* scene.reverse(false);
1351
*
1352
* @fires {@link ScrollScene.change}, when used as setter
1353
* @param {boolean} [newReverse] - The new reverse setting of the scene.
1354
* @returns {boolean} `get` - Current reverse option value.
1355
* @returns {ScrollScene} `set` - Parent object for chaining.
1356
*/
1357
this.reverse = function (newReverse) {
1358
var varname = "reverse";
1359
if (!arguments.length) { // get
1360
return _options[varname];
1361
} else if (changeOption(varname, newReverse)) { // set
1362
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
1363
}
1364
return ScrollScene;
1365
};
1366
1367
/**
1368
* **Get** or **Set** the tweenChanges option value.
1369
* @public
1370
* @example
1371
* // get the current tweenChanges option
1372
* var tweenChanges = scene.tweenChanges();
1373
*
1374
* // set new tweenChanges option
1375
* scene.tweenChanges(true);
1376
*
1377
* @fires {@link ScrollScene.change}, when used as setter
1378
* @param {boolean} [newTweenChanges] - The new tweenChanges setting of the scene.
1379
* @returns {boolean} `get` - Current tweenChanges option value.
1380
* @returns {ScrollScene} `set` - Parent object for chaining.
1381
*/
1382
this.tweenChanges = function (newTweenChanges) {
1383
var varname = "tweenChanges";
1384
if (!arguments.length) { // get
1385
return _options[varname];
1386
} else if (changeOption(varname, newTweenChanges)) { // set
1387
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
1388
}
1389
return ScrollScene;
1390
};
1391
1392
/**
1393
* **Get** or **Set** the loglevel option value.
1394
* @public
1395
* @example
1396
* // get the current loglevel
1397
* var loglevel = scene.loglevel();
1398
*
1399
* // set new loglevel
1400
* scene.loglevel(3);
1401
*
1402
* @fires {@link ScrollScene.change}, when used as setter
1403
* @param {number} [newLoglevel] - The new loglevel setting of the scene. `[0-3]`
1404
* @returns {number} `get` - Current loglevel.
1405
* @returns {ScrollScene} `set` - Parent object for chaining.
1406
*/
1407
this.loglevel = function (newLoglevel) {
1408
var varname = "loglevel";
1409
if (!arguments.length) { // get
1410
return _options[varname];
1411
} else if (changeOption(varname, newLoglevel)) { // set
1412
ScrollScene.trigger("change", {what: varname, newval: _options[varname]});
1413
}
1414
return ScrollScene;
1415
};
1416
1417
/**
1418
* **Get** the current state.
1419
* @public
1420
* @example
1421
* // get the current state
1422
* var state = scene.state();
1423
*
1424
* @returns {string} `"BEFORE"`, `"DURING"` or `"AFTER"`
1425
*/
1426
this.state = function () {
1427
return _state;
1428
};
1429
1430
/**
1431
* **Get** the trigger position of the scene (including the value of the `offset` option).
1432
* @public
1433
* @example
1434
* // get the scene's trigger position
1435
* var triggerPosition = scene.triggerPosition();
1436
*
1437
* @returns {number} Start position of the scene. Top position value for vertical and left position value for horizontal scrolls.
1438
*/
1439
this.triggerPosition = function () {
1440
var pos = _options.offset; // the offset is the basis
1441
if (_parent) {
1442
// get the trigger position
1443
if (_options.triggerElement) {
1444
// Element as trigger
1445
pos += _triggerPos;
1446
} else {
1447
// return the height of the triggerHook to start at the beginning
1448
pos += _parent.info("size") * ScrollScene.triggerHook();
1449
}
1450
}
1451
return pos;
1452
};
1453
1454
/**
1455
* **Get** the trigger offset of the scene (including the value of the `offset` option).
1456
* @public
1457
* @deprecated Method is deprecated since 1.1.0. You should now use {@link ScrollScene.triggerPosition}
1458
*/
1459
this.triggerOffset = function () {
1460
return ScrollScene.triggerPosition();
1461
};
1462
1463
/**
1464
* **Get** the current scroll offset for the start of the scene.
1465
* Mind, that the scrollOffset is related to the size of the container, if `triggerHook` is bigger than `0` (or `"onLeave"`).
1466
* This means, that resizing the container or changing the `triggerHook` will influence the scene's start offset.
1467
* @public
1468
* @example
1469
* // get the current scroll offset for the start and end of the scene.
1470
* var start = scene.scrollOffset();
1471
* var end = scene.scrollOffset() + scene.duration();
1472
* console.log("the scene starts at", start, "and ends at", end);
1473
*
1474
* @returns {number} The scroll offset (of the container) at which the scene will trigger. Y value for vertical and X value for horizontal scrolls.
1475
*/
1476
this.scrollOffset = function () {
1477
return _scrollOffset.start;
1478
};
1479
1480
/*
1481
* ----------------------------------------------------------------
1482
* public functions (scene modification)
1483
* ----------------------------------------------------------------
1484
*/
1485
1486
/**
1487
* Updates the Scene in the parent Controller to reflect the current state.
1488
* This is the equivalent to `ScrollMagic.updateScene(scene, immediately)`.
1489
* 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.
1490
* 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.
1491
* 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.
1492
* _**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._
1493
* @public
1494
* @example
1495
* // update the scene on next tick
1496
* scene.update();
1497
*
1498
* // update the scene immediately
1499
* scene.update(true);
1500
*
1501
* @fires ScrollScene.update
1502
*
1503
* @param {boolean} [immediately=false] - If `true` the update will be instant, if `false` it will wait until next update cycle (better performance).
1504
* @returns {ScrollScene} Parent object for chaining.
1505
*/
1506
this.update = function (immediately) {
1507
if (_parent) {
1508
if (immediately) {
1509
if (_parent.enabled() && _enabled) {
1510
var
1511
scrollPos = _parent.info("scrollPos"),
1512
newProgress;
1513
1514
if (_options.duration > 0) {
1515
newProgress = (scrollPos - _scrollOffset.start)/(_scrollOffset.end - _scrollOffset.start);
1516
} else {
1517
newProgress = scrollPos >= _scrollOffset.start ? 1 : 0;
1518
}
1519
1520
ScrollScene.trigger("update", {startPos: _scrollOffset.start, endPos: _scrollOffset.end, scrollPos: scrollPos});
1521
1522
ScrollScene.progress(newProgress);
1523
} else if (_pin && _state === "DURING") {
1524
updatePinState(true); // unpin in position
1525
}
1526
} else {
1527
_parent.updateScene(ScrollScene, false);
1528
}
1529
}
1530
return ScrollScene;
1531
};
1532
1533
/**
1534
* Updates dynamic scene variables like the trigger element position or the duration.
1535
* This method is automatically called in regular intervals from the controller. See {@link ScrollMagic} option `refreshInterval`.
1536
*
1537
* You can call it to minimize lag, for example when you intentionally change the position of the triggerElement.
1538
* If you don't it will simply be updated in the next refresh interval of the container, which is usually sufficient.
1539
*
1540
* @public
1541
* @since 1.1.0
1542
* @example
1543
* scene = new ScrollScene({triggerElement: "#trigger"});
1544
*
1545
* // change the position of the trigger
1546
* $("#trigger").css("top", 500);
1547
* // immediately let the scene know of this change
1548
* scene.refresh();
1549
*
1550
* @fires {@link ScrollScene.shift}, if the trigger element position or the duration changed
1551
* @fires {@link ScrollScene.change}, if the duration changed
1552
*
1553
* @returns {ScrollScene} Parent object for chaining.
1554
*/
1555
this.refresh = function () {
1556
updateDuration();
1557
updateTriggerElementPosition();
1558
// update trigger element position
1559
return ScrollScene;
1560
};
1561
1562
/**
1563
* **Get** or **Set** the scene's progress.
1564
* Usually it shouldn't be necessary to use this as a setter, as it is set automatically by scene.update().
1565
* The order in which the events are fired depends on the duration of the scene:
1566
* 1. Scenes with `duration == 0`:
1567
* Scenes that have no duration by definition have no ending. Thus the `end` event will never be fired.
1568
* When the trigger position of the scene is passed the events are always fired in this order:
1569
* `enter`, `start`, `progress` when scrolling forward
1570
* and
1571
* `progress`, `start`, `leave` when scrolling in reverse
1572
* 2. Scenes with `duration > 0`:
1573
* Scenes with a set duration have a defined start and end point.
1574
* When scrolling past the start position of the scene it will fire these events in this order:
1575
* `enter`, `start`, `progress`
1576
* When continuing to scroll and passing the end point it will fire these events:
1577
* `progress`, `end`, `leave`
1578
* When reversing through the end point these events are fired:
1579
* `enter`, `end`, `progress`
1580
* And when continuing to scroll past the start position in reverse it will fire:
1581
* `progress`, `start`, `leave`
1582
* In between start and end the `progress` event will be called constantly, whenever the progress changes.
1583
*
1584
* In short:
1585
* `enter` events will always trigger **before** the progress update and `leave` envents will trigger **after** the progress update.
1586
* `start` and `end` will always trigger at their respective position.
1587
*
1588
* Please review the event descriptions for details on the events and the event object that is passed to the callback.
1589
*
1590
* @public
1591
* @example
1592
* // get the current scene progress
1593
* var progress = scene.progress();
1594
*
1595
* // set new scene progress
1596
* scene.progress(0.3);
1597
*
1598
* @fires {@link ScrollScene.enter}, when used as setter
1599
* @fires {@link ScrollScene.start}, when used as setter
1600
* @fires {@link ScrollScene.progress}, when used as setter
1601
* @fires {@link ScrollScene.end}, when used as setter
1602
* @fires {@link ScrollScene.leave}, when used as setter
1603
*
1604
* @param {number} [progress] - The new progress value of the scene `[0-1]`.
1605
* @returns {number} `get` - Current scene progress.
1606
* @returns {ScrollScene} `set` - Parent object for chaining.
1607
*/
1608
this.progress = function (progress) {
1609
if (!arguments.length) { // get
1610
return _progress;
1611
} else { // set
1612
var
1613
doUpdate = false,
1614
oldState = _state,
1615
scrollDirection = _parent ? _parent.info("scrollDirection") : 'PAUSED',
1616
reverseOrForward = _options.reverse || progress >= _progress;
1617
if (_options.duration === 0) {
1618
// zero duration scenes
1619
doUpdate = _progress != progress;
1620
_progress = progress < 1 && reverseOrForward ? 0 : 1;
1621
_state = _progress === 0 ? 'BEFORE' : 'DURING';
1622
} else {
1623
// scenes with start and end
1624
if (progress <= 0 && _state !== 'BEFORE' && reverseOrForward) {
1625
// go back to initial state
1626
_progress = 0;
1627
_state = 'BEFORE';
1628
doUpdate = true;
1629
} else if (progress > 0 && progress < 1 && reverseOrForward) {
1630
_progress = progress;
1631
_state = 'DURING';
1632
doUpdate = true;
1633
} else if (progress >= 1 && _state !== 'AFTER') {
1634
_progress = 1;
1635
_state = 'AFTER';
1636
doUpdate = true;
1637
} else if (_state === 'DURING' && !reverseOrForward) {
1638
updatePinState(); // in case we scrolled backwards mid-scene and reverse is disabled => update the pin position, so it doesn't move back as well.
1639
}
1640
}
1641
if (doUpdate) {
1642
// fire events
1643
var
1644
eventVars = {progress: _progress, state: _state, scrollDirection: scrollDirection},
1645
stateChanged = _state != oldState;
1646
1647
var trigger = function (eventName) { // tmp helper to simplify code
1648
ScrollScene.trigger(eventName, eventVars);
1649
};
1650
1651
if (stateChanged) { // enter events
1652
if (oldState !== 'DURING') {
1653
trigger("enter");
1654
trigger(oldState === 'BEFORE' ? "start" : "end");
1655
}
1656
}
1657
trigger("progress");
1658
if (stateChanged) { // leave events
1659
if (_state !== 'DURING') {
1660
trigger(_state === 'BEFORE' ? "start" : "end");
1661
trigger("leave");
1662
}
1663
}
1664
}
1665
1666
return ScrollScene;
1667
}
1668
};
1669
1670
/**
1671
* Add a tween to the scene.
1672
* If you want to add multiple tweens, wrap them into one TimelineMax object and add it.
1673
* The duration of the tween is streched to the scroll duration of the scene, unless the scene has a duration of `0`.
1674
* @public
1675
* @example
1676
* // add a single tween
1677
* scene.setTween(TweenMax.to("obj"), 1, {x: 100});
1678
*
1679
* // add multiple tweens, wrapped in a timeline.
1680
* var timeline = new TimelineMax();
1681
* var tween1 = TweenMax.from("obj1", 1, {x: 100});
1682
* var tween2 = TweenMax.to("obj2", 1, {y: 100});
1683
* timeline
1684
* .add(tween1)
1685
* .add(tween2);
1686
* scene.addTween(timeline);
1687
*
1688
* @param {object} TweenObject - A TweenMax, TweenLite, TimelineMax or TimelineLite object that should be animated in the scene.
1689
* @returns {ScrollScene} Parent object for chaining.
1690
*/
1691
this.setTween = function (TweenObject) {
1692
if (!TimelineMax) {
1693
log(1, "ERROR: TimelineMax wasn't found. Please make sure GSAP is loaded before ScrollMagic or use asynchronous loading.");
1694
return ScrollScene;
1695
}
1696
if (_tween) { // kill old tween?
1697
ScrollScene.removeTween();
1698
}
1699
try {
1700
// wrap Tween into a TimelineMax Object to include delay and repeats in the duration and standardize methods.
1701
_tween = new TimelineMax({smoothChildTiming: true})
1702
.add(TweenObject)
1703
.pause();
1704
} catch (e) {
1705
log(1, "ERROR calling method 'setTween()': Supplied argument is not a valid TweenObject");
1706
} finally {
1707
// some properties need to be transferred it to the wrapper, otherwise they would get lost.
1708
if (TweenObject.repeat && TweenObject.repeat() === -1) {// TweenMax or TimelineMax Object?
1709
_tween.repeat(-1);
1710
_tween.yoyo(TweenObject.yoyo());
1711
}
1712
}
1713
// Some tween validations and debugging helpers
1714
1715
// check if there are position tweens defined for the trigger and warn about it :)
1716
if (_tween && _parent && _options.triggerElement && _options.loglevel >= 2) {// parent is needed to know scroll direction.
1717
var
1718
triggerTweens = _tween.getTweensOf($(_options.triggerElement)),
1719
vertical = _parent.info("vertical");
1720
$.each(triggerTweens, function (index, value) {
1721
var
1722
tweenvars = value.vars.css || value.vars,
1723
condition = vertical ? (tweenvars.top !== undefined || tweenvars.bottom !== undefined) : (tweenvars.left !== undefined || tweenvars.right !== undefined);
1724
if (condition) {
1725
log(2, "WARNING: Tweening the position of the trigger element affects the scene timing and should be avoided!");
1726
return false;
1727
}
1728
});
1729
}
1730
1731
// warn about tween overwrites, when an element is tweened multiple times
1732
if (parseFloat(TweenLite.version) >= 1.14) { // onOverwrite only present since GSAP v1.14.0
1733
var
1734
list = _tween.getChildren(true, true, false), // get all nested tween objects
1735
newCallback = function () {
1736
log(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");
1737
};
1738
for (var i=0, thisTween, oldCallback; i<list.length; i++) {
1739
/*jshint loopfunc: true */
1740
thisTween = list[i];
1741
if (oldCallback !== newCallback) { // if tweens is added more than once
1742
oldCallback = thisTween.vars.onOverwrite;
1743
thisTween.vars.onOverwrite = function () {
1744
if (oldCallback) {
1745
oldCallback.apply(this, arguments);
1746
}
1747
newCallback.apply(this, arguments);
1748
};
1749
}
1750
}
1751
}
1752
log(3, "added tween");
1753
updateTweenProgress();
1754
return ScrollScene;
1755
};
1756
1757
/**
1758
* Remove the tween from the scene.
1759
* @public
1760
* @example
1761
* // remove the tween from the scene without resetting it
1762
* scene.removeTween();
1763
*
1764
* // remove the tween from the scene and reset it to initial position
1765
* scene.removeTween(true);
1766
*
1767
* @param {boolean} [reset=false] - If `true` the tween will be reset to its initial values.
1768
* @returns {ScrollScene} Parent object for chaining.
1769
*/
1770
this.removeTween = function (reset) {
1771
if (_tween) {
1772
if (reset) {
1773
updateTweenProgress(0);
1774
}
1775
_tween.kill();
1776
_tween = undefined;
1777
log(3, "removed tween (reset: " + (reset ? "true" : "false") + ")");
1778
}
1779
return ScrollScene;
1780
};
1781
1782
/**
1783
* Pin an element for the duration of the tween.
1784
* If the scene duration is 0 the element will only be unpinned, if the user scrolls back past the start position.
1785
* _**NOTE:** The option `pushFollowers` has no effect, when the scene duration is 0._
1786
* @public
1787
* @example
1788
* // pin element and push all following elements down by the amount of the pin duration.
1789
* scene.setPin("#pin");
1790
*
1791
* // pin element and keeping all following elements in their place. The pinned element will move past them.
1792
* scene.setPin("#pin", {pushFollowers: false});
1793
*
1794
* @param {(string|object)} element - A Selector targeting an element, a DOM object or a jQuery object that is supposed to be pinned.
1795
* @param {object} [settings] - settings for the pin
1796
* @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.
1797
Ignored, when duration is `0`.
1798
* @param {string} [settings.spacerClass="scrollmagic-pin-spacer"] - Classname of the pin spacer element, which is used to replace the element.
1799
* @param {string} [settings.pinnedClass=""] - Classname that should be added to the pinned element during pin phase (and removed after).
1800
*
1801
* @returns {ScrollScene} Parent object for chaining.
1802
*/
1803
this.setPin = function (element, settings) {
1804
var
1805
defaultSettings = {
1806
pushFollowers: true,
1807
spacerClass: "scrollmagic-pin-spacer",
1808
pinnedClass: ""
1809
};
1810
settings = $.extend({}, defaultSettings, settings);
1811
1812
// validate Element
1813
element = $(element).first();
1814
if (element.length === 0) {
1815
log(1, "ERROR calling method 'setPin()': Invalid pin element supplied.");
1816
return ScrollScene; // cancel
1817
} else if (element.css("position") == "fixed") {
1818
log(1, "ERROR calling method 'setPin()': Pin does not work with elements that are positioned 'fixed'.");
1819
return ScrollScene; // cancel
1820
}
1821
1822
if (_pin) { // preexisting pin?
1823
if (_pin === element) {
1824
// same pin we already have -> do nothing
1825
return ScrollScene; // cancel
1826
} else {
1827
// kill old pin
1828
ScrollScene.removePin();
1829
}
1830
1831
}
1832
_pin = element;
1833
1834
_pin.parent().hide(); // hack start to force jQuery css to return stylesheet values instead of calculated px values.
1835
var
1836
inFlow = _pin.css("position") != "absolute",
1837
pinCSS = _pin.css(["display", "top", "left", "bottom", "right"]),
1838
sizeCSS = _pin.css(["width", "height"]);
1839
_pin.parent().show(); // hack end.
1840
1841
if (sizeCSS.width === "0px" && inFlow && isMarginCollapseType(pinCSS.display)) {
1842
// log (2, "WARNING: Your pinned element probably needs a defined width or it might collapse during pin.");
1843
}
1844
if (!inFlow && settings.pushFollowers) {
1845
log(2, "WARNING: If the pinned element is positioned absolutely pushFollowers is disabled.");
1846
settings.pushFollowers = false;
1847
}
1848
1849
// create spacer
1850
var spacer = $("<div></div>")
1851
.addClass(settings.spacerClass)
1852
.css(pinCSS)
1853
.data("ScrollMagicPinSpacer", true)
1854
.css({
1855
position: inFlow ? "relative" : "absolute",
1856
"margin-left": "auto",
1857
"margin-right": "auto",
1858
"box-sizing": "content-box"
1859
});
1860
1861
// set the pin Options
1862
var pinInlineCSS = _pin[0].style;
1863
_pinOptions = {
1864
spacer: spacer,
1865
relSize: { // save if size is defined using % values. if so, handle spacer resize differently...
1866
width: sizeCSS.width.slice(-1) === "%",
1867
height: sizeCSS.height.slice(-1) === "%",
1868
autoFullWidth: sizeCSS.width === "0px" && inFlow && isMarginCollapseType(pinCSS.display)
1869
},
1870
pushFollowers: settings.pushFollowers,
1871
inFlow: inFlow, // stores if the element takes up space in the document flow
1872
origStyle: {
1873
width: pinInlineCSS.width || "",
1874
position: pinInlineCSS.position || "",
1875
top: pinInlineCSS.top || "",
1876
left: pinInlineCSS.left || "",
1877
bottom: pinInlineCSS.bottom || "",
1878
right: pinInlineCSS.right || "",
1879
"box-sizing": pinInlineCSS["box-sizing"] || "",
1880
"-moz-box-sizing": pinInlineCSS["-moz-box-sizing"] || "",
1881
"-webkit-box-sizing": pinInlineCSS["-webkit-box-sizing"] || ""
1882
}, // save old styles (for reset)
1883
pinnedClass: settings.pinnedClass // the class that should be added to the element when pinned
1884
};
1885
1886
// if relative size, transfer it to spacer and make pin calculate it...
1887
if (_pinOptions.relSize.width) {
1888
spacer.css("width", sizeCSS.width);
1889
}
1890
if (_pinOptions.relSize.height) {
1891
spacer.css("height", sizeCSS.height);
1892
}
1893
1894
// now place the pin element inside the spacer
1895
_pin.before(spacer)
1896
.appendTo(spacer)
1897
// and set new css
1898
.css({
1899
position: inFlow ? "relative" : "absolute",
1900
top: "auto",
1901
left: "auto",
1902
bottom: "auto",
1903
right: "auto"
1904
});
1905
1906
if (_pinOptions.relSize.width || _pinOptions.relSize.autoFullWidth) {
1907
_pin.css("box-sizing", "border-box");
1908
}
1909
1910
// add listener to document to update pin position in case controller is not the document.
1911
$(window).on("scroll." + NAMESPACE + "_pin resize." + NAMESPACE + "_pin", updatePinInContainer);
1912
// add mousewheel listener to catch scrolls over fixed elements
1913
_pin.on("mousewheel DOMMouseScroll", onMousewheelOverPin);
1914
1915
log(3, "added pin");
1916
1917
// finally update the pin to init
1918
updatePinState();
1919
1920
return ScrollScene;
1921
};
1922
1923
/**
1924
* Remove the pin from the scene.
1925
* @public
1926
* @example
1927
* // remove the pin from the scene without resetting it (the spacer is not removed)
1928
* scene.removePin();
1929
*
1930
* // remove the pin from the scene and reset the pin element to its initial position (spacer is removed)
1931
* scene.removePin(true);
1932
*
1933
* @param {boolean} [reset=false] - If `false` the spacer will not be removed and the element's position will not be reset.
1934
* @returns {ScrollScene} Parent object for chaining.
1935
*/
1936
this.removePin = function (reset) {
1937
if (_pin) {
1938
if (reset || !_parent) { // if there's no parent no progress was made anyway...
1939
_pin.insertBefore(_pinOptions.spacer)
1940
.css(_pinOptions.origStyle);
1941
_pinOptions.spacer.remove();
1942
} else {
1943
if (_state === "DURING") {
1944
updatePinState(true); // force unpin at position
1945
}
1946
}
1947
$(window).off("scroll." + NAMESPACE + "_pin resize." + NAMESPACE + "_pin");
1948
_pin.off("mousewheel DOMMouseScroll", onMousewheelOverPin);
1949
_pin = undefined;
1950
log(3, "removed pin (reset: " + (reset ? "true" : "false") + ")");
1951
}
1952
return ScrollScene;
1953
};
1954
1955
/**
1956
* Define a css class modification while the scene is active.
1957
* When the scene triggers the classes will be added to the supplied element and removed, when the scene is over.
1958
* If the scene duration is 0 the classes will only be removed if the user scrolls back past the start position.
1959
* @public
1960
* @example
1961
* // add the class 'myclass' to the element with the id 'my-elem' for the duration of the scene
1962
* scene.setClassToggle("#my-elem", "myclass");
1963
*
1964
* // add multiple classes to multiple elements defined by the selector '.classChange'
1965
* scene.setClassToggle(".classChange", "class1 class2 class3");
1966
*
1967
* @param {(string|object)} element - A Selector targeting one or more elements, a DOM object or a jQuery object that is supposed to be modified.
1968
* @param {string} classes - One or more Classnames (separated by space) that should be added to the element during the scene.
1969
*
1970
* @returns {ScrollScene} Parent object for chaining.
1971
*/
1972
this.setClassToggle = function (element, classes) {
1973
var $elm = $(element);
1974
if ($elm.length === 0 || $.type(classes) !== "string") {
1975
log(1, "ERROR calling method 'setClassToggle()': Invalid " + ($elm.length === 0 ? "element" : "classes") + " supplied.");
1976
return ScrollScene;
1977
}
1978
_cssClasses = classes;
1979
_cssClassElm = $elm;
1980
ScrollScene.on("enter.internal_class leave.internal_class", function (e) {
1981
_cssClassElm.toggleClass(_cssClasses, e.type === "enter");
1982
});
1983
return ScrollScene;
1984
};
1985
1986
/**
1987
* Remove the class binding from the scene.
1988
* @public
1989
* @example
1990
* // remove class binding from the scene without reset
1991
* scene.removeClassToggle();
1992
*
1993
* // remove class binding and remove the changes it caused
1994
* scene.removeClassToggle(true);
1995
*
1996
* @param {boolean} [reset=false] - If `false` and the classes are currently active, they will remain on the element. If `true` they will be removed.
1997
* @returns {ScrollScene} Parent object for chaining.
1998
*/
1999
this.removeClassToggle = function (reset) {
2000
if (_cssClassElm && reset) {
2001
_cssClassElm.removeClass(_cssClasses);
2002
}
2003
ScrollScene.off("start.internal_class end.internal_class");
2004
_cssClasses = undefined;
2005
_cssClassElm = undefined;
2006
return ScrollScene;
2007
};
2008
2009
/**
2010
* Add the scene to a controller.
2011
* This is the equivalent to `ScrollMagic.addScene(scene)`.
2012
* @public
2013
* @example
2014
* // add a scene to a ScrollMagic controller
2015
* scene.addTo(controller);
2016
*
2017
* @param {ScrollMagic} controller - The controller to which the scene should be added.
2018
* @returns {ScrollScene} Parent object for chaining.
2019
*/
2020
this.addTo = function (controller) {
2021
if (!(controller instanceof ScrollMagic)) {
2022
log(1, "ERROR: supplied argument of 'addTo()' is not a valid ScrollMagic controller");
2023
} else if (_parent != controller) {
2024
// new parent
2025
if (_parent) { // I had a parent before, so remove it...
2026
_parent.removeScene(ScrollScene);
2027
}
2028
_parent = controller;
2029
validateOption();
2030
updateDuration(true);
2031
updateTriggerElementPosition(true);
2032
updateScrollOffset();
2033
updatePinSpacerSize();
2034
_parent.info("container").on("resize." + NAMESPACE, function () {
2035
updateRelativePinSpacer();
2036
if (ScrollScene.triggerHook() > 0) {
2037
ScrollScene.trigger("shift", {reason: "containerSize"});
2038
}
2039
});
2040
log(3, "added " + NAMESPACE + " to controller");
2041
controller.addScene(ScrollScene);
2042
ScrollScene.update();
2043
}
2044
return ScrollScene;
2045
};
2046
2047
/**
2048
* **Get** or **Set** the current enabled state of the scene.
2049
* This can be used to disable this scene without removing or destroying it.
2050
* @public
2051
*
2052
* @example
2053
* // get the current value
2054
* var enabled = scene.enabled();
2055
*
2056
* // disable the scene
2057
* scene.enabled(false);
2058
*
2059
* @param {boolean} [newState] - The new enabled state of the scene `true` or `false`.
2060
* @returns {(boolean|ScrollScene)} Current enabled state or parent object for chaining.
2061
*/
2062
this.enabled = function (newState) {
2063
if (!arguments.length) { // get
2064
return _enabled;
2065
} else if (_enabled != newState) { // set
2066
_enabled = !!newState;
2067
ScrollScene.update(true);
2068
}
2069
return ScrollScene;
2070
};
2071
2072
/**
2073
* Remove the scene from its parent controller.
2074
* This is the equivalent to `ScrollMagic.removeScene(scene)`.
2075
* The scene will not be updated anymore until you readd it to a controller.
2076
* To remove the pin or the tween you need to call removeTween() or removePin() respectively.
2077
* @public
2078
* @example
2079
* // remove the scene from its parent controller
2080
* scene.remove();
2081
*
2082
* @returns {ScrollScene} Parent object for chaining.
2083
*/
2084
this.remove = function () {
2085
if (_parent) {
2086
_parent.info("container").off("resize." + NAMESPACE);
2087
var tmpParent = _parent;
2088
_parent = undefined;
2089
log(3, "removed " + NAMESPACE + " from controller");
2090
tmpParent.removeScene(ScrollScene);
2091
}
2092
return ScrollScene;
2093
};
2094
2095
/**
2096
* Destroy the scene and everything.
2097
* @public
2098
* @example
2099
* // destroy the scene without resetting the pin and tween to their initial positions
2100
* scene = scene.destroy();
2101
*
2102
* // destroy the scene and reset the pin and tween
2103
* scene = scene.destroy(true);
2104
*
2105
* @param {boolean} [reset=false] - If `true` the pin and tween (if existent) will be reset.
2106
* @returns {null} Null to unset handler variables.
2107
*/
2108
this.destroy = function (reset) {
2109
ScrollScene.removeTween(reset);
2110
ScrollScene.removePin(reset);
2111
ScrollScene.removeClassToggle(reset);
2112
ScrollScene.trigger("destroy", {reset: reset});
2113
ScrollScene.remove();
2114
ScrollScene.off("start end enter leave progress change update shift destroy shift.internal change.internal progress.internal");
2115
log(3, "destroyed " + NAMESPACE + " (reset: " + (reset ? "true" : "false") + ")");
2116
return null;
2117
};
2118
2119
/*
2120
* ----------------------------------------------------------------
2121
* EVENTS
2122
* ----------------------------------------------------------------
2123
*/
2124
2125
/**
2126
* Scene start event.
2127
* Fires whenever the scroll position its the starting point of the scene.
2128
* 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.
2129
*
2130
* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.
2131
*
2132
* @event ScrollScene.start
2133
*
2134
* @example
2135
* scene.on("start", function (event) {
2136
* alert("Hit start point of scene.");
2137
* });
2138
*
2139
* @property {object} event - The event Object passed to each callback
2140
* @property {string} event.type - The name of the event
2141
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2142
* @property {number} event.progress - Reflects the current progress of the scene
2143
* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`
2144
* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`
2145
*/
2146
/**
2147
* Scene end event.
2148
* Fires whenever the scroll position its the ending point of the scene.
2149
* 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.
2150
*
2151
* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.
2152
*
2153
* @event ScrollScene.end
2154
*
2155
* @example
2156
* scene.on("end", function (event) {
2157
* alert("Hit end point of scene.");
2158
* });
2159
*
2160
* @property {object} event - The event Object passed to each callback
2161
* @property {string} event.type - The name of the event
2162
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2163
* @property {number} event.progress - Reflects the current progress of the scene
2164
* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`
2165
* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`
2166
*/
2167
/**
2168
* Scene enter event.
2169
* Fires whenever the scene enters the "DURING" state.
2170
* 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.
2171
*
2172
* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.
2173
*
2174
* @event ScrollScene.enter
2175
*
2176
* @example
2177
* scene.on("enter", function (event) {
2178
* alert("Entered a scene.");
2179
* });
2180
*
2181
* @property {object} event - The event Object passed to each callback
2182
* @property {string} event.type - The name of the event
2183
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2184
* @property {number} event.progress - Reflects the current progress of the scene
2185
* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`
2186
* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`
2187
*/
2188
/**
2189
* Scene leave event.
2190
* Fires whenever the scene's state goes from "DURING" to either "BEFORE" or "AFTER".
2191
* 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.
2192
*
2193
* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.
2194
*
2195
* @event ScrollScene.leave
2196
*
2197
* @example
2198
* scene.on("leave", function (event) {
2199
* alert("Left a scene.");
2200
* });
2201
*
2202
* @property {object} event - The event Object passed to each callback
2203
* @property {string} event.type - The name of the event
2204
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2205
* @property {number} event.progress - Reflects the current progress of the scene
2206
* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`
2207
* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`
2208
*/
2209
/**
2210
* Scene update event.
2211
* Fires whenever the scene is updated (but not necessarily changes the progress).
2212
*
2213
* @event ScrollScene.update
2214
*
2215
* @example
2216
* scene.on("update", function (event) {
2217
* console.log("Scene updated.");
2218
* });
2219
*
2220
* @property {object} event - The event Object passed to each callback
2221
* @property {string} event.type - The name of the event
2222
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2223
* @property {number} event.startPos - The starting position of the scene (in relation to the conainer)
2224
* @property {number} event.endPos - The ending position of the scene (in relation to the conainer)
2225
* @property {number} event.scrollPos - The current scroll position of the container
2226
*/
2227
/**
2228
* Scene progress event.
2229
* Fires whenever the progress of the scene changes.
2230
*
2231
* For details on this event and the order in which it is fired, please review the {@link ScrollScene.progress} method.
2232
*
2233
* @event ScrollScene.progress
2234
*
2235
* @example
2236
* scene.on("progress", function (event) {
2237
* console.log("Scene progress changed.");
2238
* });
2239
*
2240
* @property {object} event - The event Object passed to each callback
2241
* @property {string} event.type - The name of the event
2242
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2243
* @property {number} event.progress - Reflects the current progress of the scene
2244
* @property {string} event.state - The current state of the scene `"BEFORE"`, `"DURING"` or `"AFTER"`
2245
* @property {string} event.scrollDirection - Indicates which way we are scrolling `"PAUSED"`, `"FORWARD"` or `"REVERSE"`
2246
*/
2247
/**
2248
* Scene change event.
2249
* Fires whenvever a property of the scene is changed.
2250
*
2251
* @event ScrollScene.change
2252
*
2253
* @example
2254
* scene.on("change", function (event) {
2255
* console.log("Scene Property \"" + event.what + "\" changed to " + event.newval);
2256
* });
2257
*
2258
* @property {object} event - The event Object passed to each callback
2259
* @property {string} event.type - The name of the event
2260
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2261
* @property {string} event.what - Indicates what value has been changed
2262
* @property {mixed} event.newval - The new value of the changed property
2263
*/
2264
/**
2265
* Scene shift event.
2266
* Fires whenvever the start or end **scroll offset** of the scene change.
2267
* This happens explicitely, when one of these values change: `offset`, `duration` or `triggerHook`.
2268
* It will fire implicitly when the `triggerElement` changes, if the new element has a different position (most cases).
2269
* It will also fire implicitly when the size of the container changes and the triggerHook is anything other than `onLeave`.
2270
*
2271
* @event ScrollScene.shift
2272
* @since 1.1.0
2273
*
2274
* @example
2275
* scene.on("shift", function (event) {
2276
* console.log("Scene moved, because the " + event.reason + " has changed.)");
2277
* });
2278
*
2279
* @property {object} event - The event Object passed to each callback
2280
* @property {string} event.type - The name of the event
2281
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2282
* @property {string} event.reason - Indicates why the scene has shifted
2283
*/
2284
/**
2285
* Scene destroy event.
2286
* Fires whenvever the scene is destroyed.
2287
* This can be used to tidy up custom behaviour used in events.
2288
*
2289
* @event ScrollScene.destroy
2290
* @since 1.1.0
2291
*
2292
* @example
2293
* scene.on("enter", function (event) {
2294
* // add custom action
2295
* $("#my-elem").left("200");
2296
* })
2297
* .on("destroy", function (event) {
2298
* // reset my element to start position
2299
* if (event.reset) {
2300
* $("#my-elem").left("0");
2301
* }
2302
* });
2303
*
2304
* @property {object} event - The event Object passed to each callback
2305
* @property {string} event.type - The name of the event
2306
* @property {ScrollScene} event.target - The ScrollScene object that triggered this event
2307
* @property {boolean} event.reset - Indicates if the destroy method was called with reset `true` or `false`.
2308
*/
2309
2310
/**
2311
* Add one ore more event listener.
2312
* The callback function will be fired at the respective event, and an object containing relevant data will be passed to the callback.
2313
* @public
2314
*
2315
* @example
2316
* function callback (event) {
2317
* console.log("Event fired! (" + event.type + ")");
2318
* }
2319
* // add listeners
2320
* scene.on("change update progress start end enter leave", callback);
2321
*
2322
* @param {string} name - The name or names of the event the callback should be attached to.
2323
* @param {function} callback - A function that should be executed, when the event is dispatched. An event object will be passed to the callback.
2324
* @returns {ScrollScene} Parent object for chaining.
2325
*/
2326
this.on = function (name, callback) {
2327
if ($.isFunction(callback)) {
2328
var names = $.trim(name).toLowerCase()
2329
.replace(/(\w+)\.(\w+)/g, '$1.' + NAMESPACE + '_$2') // add custom namespace, if one is defined
2330
.replace(/( |^)(\w+)(?= |$)/g, '$1$2.' + NAMESPACE ); // add namespace to regulars.
2331
$(ScrollScene).on(names, callback);
2332
} else {
2333
log(1, "ERROR calling method 'on()': Supplied argument is not a valid callback!");
2334
}
2335
return ScrollScene;
2336
};
2337
2338
/**
2339
* Remove one or more event listener.
2340
* @public
2341
*
2342
* @example
2343
* function callback (event) {
2344
* console.log("Event fired! (" + event.type + ")");
2345
* }
2346
* // add listeners
2347
* scene.on("change update", callback);
2348
* // remove listeners
2349
* scene.off("change update", callback);
2350
*
2351
* @param {string} name - The name or names of the event that should be removed.
2352
* @param {function} [callback] - A specific callback function that should be removed. If none is passed all callbacks to the event listener will be removed.
2353
* @returns {ScrollScene} Parent object for chaining.
2354
*/
2355
this.off = function (name, callback) {
2356
var names = $.trim(name).toLowerCase()
2357
.replace(/(\w+)\.(\w+)/g, '$1.' + NAMESPACE + '_$2') // add custom namespace, if one is defined
2358
.replace(/( |^)(\w+)(?= |$)/g, '$1$2.' + NAMESPACE + '$3'); // add namespace to regulars.
2359
$(ScrollScene).off(names, callback);
2360
return ScrollScene;
2361
};
2362
2363
/**
2364
* Trigger an event.
2365
* @public
2366
*
2367
* @example
2368
* this.trigger("change");
2369
*
2370
* @param {string} name - The name of the event that should be triggered.
2371
* @param {object} [vars] - An object containing info that should be passed to the callback.
2372
* @returns {ScrollScene} Parent object for chaining.
2373
*/
2374
this.trigger = function (name, vars) {
2375
log(3, 'event fired:', name, "->", vars);
2376
var event = $.Event($.trim(name).toLowerCase(), vars);
2377
$(ScrollScene).trigger(event);
2378
return ScrollScene;
2379
};
2380
2381
// INIT
2382
construct();
2383
return ScrollScene;
2384
};
2385
2386
/*
2387
* ----------------------------------------------------------------
2388
* global logging functions and making sure no console errors occur
2389
* ----------------------------------------------------------------
2390
*/
2391
2392
var debug = (function (console) {
2393
var loglevels = ["error", "warn", "log"];
2394
if (!console.log) {
2395
console.log = function(){}; // no console log, well - do nothing then...
2396
}
2397
for(var i = 0, method; i<loglevels.length; i++) { // make sure methods for all levels exist.
2398
method = loglevels[i];
2399
if (!console[method]) {
2400
console[method] = console.log; // prefer .log over nothing
2401
}
2402
}
2403
// debugging function
2404
return function (loglevel) {
2405
if (loglevel > loglevels.length || loglevel <= 0) loglevel = loglevels.length;
2406
var now = new Date(),
2407
time = ("0"+now.getHours()).slice(-2) + ":" + ("0"+now.getMinutes()).slice(-2) + ":" + ("0"+now.getSeconds()).slice(-2) + ":" + ("00"+now.getMilliseconds()).slice(-3),
2408
method = loglevels[loglevel-1],
2409
args = Array.prototype.splice.call(arguments, 1),
2410
func = Function.prototype.bind.call(console[method], console);
2411
2412
args.unshift(time);
2413
func.apply(console, args);
2414
};
2415
}(window.console = window.console || {}));
2416
// a helper function that should generally be faster than jQuery.offset() and can also return position in relation to viewport.
2417
var getOffset = function (elem, relativeToViewport) {
2418
var offset = {top: 0, left: 0};
2419
elem = elem[0]; // tmp workaround until jQuery dependency is removed.
2420
if (elem && elem.getBoundingClientRect) { // check if available
2421
var rect = elem.getBoundingClientRect();
2422
offset.top = rect.top;
2423
offset.left = rect.left;
2424
if (!relativeToViewport) { // clientRect is by default relative to viewport...
2425
offset.top += (window.pageYOffset || document.scrollTop || 0) - (document.clientTop || 0);
2426
offset.left += (window.pageXOffset || document.scrollLeft || 0) - (document.clientLeft || 0);
2427
}
2428
}
2429
return offset;
2430
};
2431
var isDomElement = function (o){
2432
return (
2433
typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
2434
o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName==="string"
2435
);
2436
};
2437
var isMarginCollapseType = function (str) {
2438
return ["block", "flex", "list-item", "table", "-webkit-box"].indexOf(str) > -1;
2439
};
2440
// implementation of requestAnimationFrame
2441
var animationFrameCallback = window.requestAnimationFrame;
2442
var animationFrameCancelCallback = window.cancelAnimationFrame;
2443
2444
// polyfill -> based on https://gist.github.com/paulirish/1579671
2445
(function (window) {
2446
var
2447
lastTime = 0,
2448
vendors = ['ms', 'moz', 'webkit', 'o'],
2449
i;
2450
2451
// try vendor prefixes if the above doesn't work
2452
for (i = 0; !animationFrameCallback && i < vendors.length; ++i) {
2453
animationFrameCallback = window[vendors[i] + 'RequestAnimationFrame'];
2454
animationFrameCancelCallback = window[vendors[i] + 'CancelAnimationFrame'] || window[vendors[i] + 'CancelRequestAnimationFrame'];
2455
}
2456
2457
// fallbacks
2458
if (!animationFrameCallback) {
2459
animationFrameCallback = function (callback) {
2460
var
2461
currTime = new Date().getTime(),
2462
timeToCall = Math.max(0, 16 - (currTime - lastTime)),
2463
id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall);
2464
lastTime = currTime + timeToCall;
2465
return id;
2466
};
2467
}
2468
if (!animationFrameCancelCallback) {
2469
animationFrameCancelCallback = function (id) {
2470
window.clearTimeout(id);
2471
};
2472
}
2473
}(window));
2474
2475
return {
2476
Controller: ScrollMagic,
2477
Scene: ScrollScene,
2478
};
2479
}));
2480
2481