Class: ScrollMagic

ScrollMagic

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
Name Type Argument Default Description
container string | object <optional>
window

A selector, DOM object or a jQuery object that references the main container for scrolling.

vertical boolean <optional>
true

Sets the scroll mode to vertical (true) or horizontal (false) scrolling.

globalSceneOptions object <optional>
{}

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 ScrollScene.

loglevel number <optional>
2

Loglevel for debugging. Note that logging is disabled in the minified version of ScrollMagic.

  • 0 => silent
  • 1 => errors
  • 2 => errors, warnings
  • 3 => errors, warnings, debuginfo
_refreshInterval boolean <optional>
100

Some changes don't call events by default, like changing the container size or moving a scene trigger element.
This interval polls these parameters to fire the necessary events.
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.

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 to ScrollScene.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 to ScrollScene.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:

  1. number -> The container will scroll to this new scroll offset.
  2. string or object -> Can be a selector, a DOM object or a jQuery element.
    The container will scroll to the position of this element.
  3. ScrollScene -> The container will scroll to the start of this scene.
  4. 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 using this.
    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.
See ScrollMagic.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. See ScrollMagic.scrollPos() for details.

Parameters:
Name Type Argument Default Description
immediately boolean <optional>
false

If true the update will be instant, if false 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 to ScrollScene.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, if false 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 or false.

Source:
Returns:

Current enabled state or parent object for chaining.

{ boolean | ScrollMagic }
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:

Current loglevel or parent object for chaining.

{ number | ScrollMagic }
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:

Current scroll position or parent object for chaining.

{ number | ScrollMagic }
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();