new ScrollMagic(options)
The main class that is needed once per scroll container.
Parameters:
Name | Type | Argument | Description | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
<optional> |
An object containing one or more options for the controller. Properties
|
- Source:
Example
// basic initialization var controller = new ScrollMagic(); // passing options var controller = new ScrollMagic({container: "#myContainer", loglevel: 3});
Control Methods
-
addScene(ScrollScene) → {ScrollMagic}
-
Add one ore more scene(s) to the controller.
This is the equivalent toScrollScene.addTo(controller)
.Parameters:
Name Type Description ScrollScene
ScrollScene | array ScrollScene or Array of ScrollScenes to be added to the controller.
- Source:
Returns:
Parent object for chaining.
{ ScrollMagic }Example
// with a previously defined scene controller.addScene(scene); // with a newly created scene. controller.addScene(new ScrollScene({duration : 0})); // adding multiple scenes controller.addScene([scene, scene2, new ScrollScene({duration : 0})]);
-
destroy(resetScenes) → {null}
-
Destroy the Controller, all Scenes and everything.
Parameters:
Name Type Argument Default Description resetScenes
boolean <optional>
false If
true
the pins and tweens (if existent) of all scenes will be reset.- Source:
Returns:
Null to unset handler variables.
{ null }Example
// without resetting the scenes controller = controller.destroy(); // with scene reset controller = controller.destroy(true);
-
removeScene(ScrollScene) → {ScrollMagic}
-
Remove one ore more scene(s) from the controller.
This is the equivalent toScrollScene.remove()
.Parameters:
Name Type Description ScrollScene
ScrollScene | array ScrollScene or Array of ScrollScenes to be removed from the controller.
- Source:
Returns:
Parent object for chaining.
{ ScrollMagic }Example
// remove a scene from the controller controller.removeScene(scene); // remove multiple scenes from the controller controller.removeScene([scene, scene2, scene3]);
-
scrollTo(scrollTarget) → {ScrollMagic}
-
Scroll to a numeric scroll offset, a DOM element, the start of a scene or provide an alternate method for scrolling.
For vertical controllers it will change the top scroll offset and for horizontal applications it will change the left offset.Parameters:
Name Type Argument Description scrollTarget
mixed <optional>
The supplied argument can be one of these types:
number
-> The container will scroll to this new scroll offset.string
orobject
-> Can be a selector, a DOM object or a jQuery element.
The container will scroll to the position of this element.ScrollScene
-> The container will scroll to the start of this scene.function
-> This function will be used as a callback for future scroll position modifications.
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 usingthis
.
NOTE: All other options will still work as expected, using the new function to scroll.
- Since:
- 1.1.0
- Source:
Returns:
Parent object for chaining.
{ ScrollMagic }Example
// scroll to an offset of 100 controller.scrollTo(100); // scroll to a DOM element controller.scrollTo("#anchor"); // scroll to the beginning of a scene var scene = new ScrollScene({offset: 200}); controller.scrollTo(scene); // define a new scroll position modification function (animate instead of jump) controller.scrollTo(function (newScrollPos) { $("body").animate({scrollTop: newScrollPos}); });
-
update(immediately) → {ScrollMagic}
-
Updates the controller params and calls updateScene on every scene, that is attached to the controller.
SeeScrollMagic.updateScene()
for more information about what this means.
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.
The only application for this method is when ScrollMagic fails to detect these events.
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. For this case there will also be the need to provide a custom function to calculate the correct scroll position. SeeScrollMagic.scrollPos()
for details.Parameters:
Name Type Argument Default Description immediately
boolean <optional>
false If
true
the update will be instant, iffalse
it will wait until next tweenmax tick (better performance)- Source:
Returns:
Parent object for chaining.
{ ScrollMagic }Example
// update the controller on next tick (saves performance) controller.update(); // update the controller immediately controller.update(true);
-
updateScene(ScrollScene, immediately) → {ScrollMagic}
-
Update one ore more scene(s) according to the scroll position of the container.
This is the equivalent toScrollScene.update()
.
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.
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.
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.Parameters:
Name Type Argument Default Description ScrollScene
ScrollScene ScrollScene or Array of ScrollScenes that is/are supposed to be updated.
immediately
boolean <optional>
false If
true
the update will be instant, iffalse
it will wait until next tweenmax tick.
This is useful when changing multiple properties of the scene - this way it will only be updated once all new properties are set (onTick).- Source:
Returns:
Parent object for chaining.
{ ScrollMagic }Example
// update a specific scene on next tick controller.updateScene(scene); // update a specific scene immediately controller.updateScene(scene, true); // update multiple scenes scene on next tick controller.updateScene([scene1, scene2, scene3]);
Parameters (getter / setter)
-
enabled(newState) → {boolean|ScrollMagic}
-
Get or Set the current enabled state of the controller.
This can be used to disable all Scenes connected to the controller without destroying or removing them.Parameters:
Name Type Argument Description newState
boolean <optional>
The new enabled state of the controller
true
orfalse
.- Source:
Returns:
Example
// get the current value var enabled = controller.enabled(); // disable the controller controller.enabled(false);
-
loglevel(newLoglevel) → {number|ScrollMagic}
-
Get or Set the current loglevel option value.
Parameters:
Name Type Argument Description newLoglevel
number <optional>
The new loglevel setting of the ScrollMagic controller.
[0-3]
- Source:
Returns:
Example
// get the current value var loglevel = controller.loglevel(); // set a new value controller.loglevel(3);
-
scrollPos(scrollPosMethod) → {number|ScrollMagic}
-
Get the current scrollPosition or Set a new method to calculate it.
-> GET: When used as a getter this function will return the current scroll position.
To get a cached value use ScrollMagic.info("scrollPos"), which will be updated on tick to save on performance.
For vertical controllers it will return the top scroll offset and for horizontal applications it will return the left offset.-> SET: When used as a setter this method prodes a way to permanently overwrite the controller's scroll position calculation.
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.
Moving a child container inside a parent is a commonly used method for several scrolling frameworks, including iScroll.
By providing an alternate calculation function you can make sure ScrollMagic receives the correct scroll position.
Please also bear in mind that your function should return y values for vertical scrolls an x for horizontals.To change the current scroll position please use
ScrollMagic.scrollTo()
.Parameters:
Name Type Argument Description scrollPosMethod
function <optional>
The function to be used for the scroll position calculation of the container.
- Source:
Returns:
Example
// get the current scroll Position var scrollPos = controller.scrollPos(); // set a new scroll position calculation method controller.scrollPos(function () { return this.info("vertical") ? -$mychildcontainer.y : -$mychildcontainer.x });
Properties (getter)
-
info(about) → {mixed|object}
-
Get all infos or one in particular about the controller.
Parameters:
Name Type Argument Description about
string <optional>
If passed only this info will be returned instead of an object containing all.
Valid options are:"size"
=> the current viewport size of the container"vertical"
=> true if vertical scrolling, otherwise false"scrollPos"
=> the current scroll position"scrollDirection"
=> the last known direction of the scroll"container"
=> the container element"isDocument"
=> true if container element is the document.
- Source:
Returns:
The requested info(s).
{ mixed | object }Example
// returns the current scroll position (number) var scrollPos = controller.info("scrollPos"); // returns all infos as an object var infos = controller.info();