GreenSock Docs (HTML5/JS)

ScrollToPlugin

Animates the scroll position of the window (like doing window.scrollTo(x, y)) or a DOM element (like doing myDiv.scrollTop = y; myDiv.scrollLeft = x;).

To scroll the window to a particular position, use window as the target of the tween like this:

//scroll to 400 pixels down from the top
TweenLite.to(window, 2, {scrollTo:400});

//or to scroll to the element with the ID "#someID" (as of GSAP 1.19.0):
TweenLite.to(window, 2, {scrollTo:"#someID"});

To tween the content of a div, make sure you've set the overflow:scroll on the div and then:

//scroll to 250 pixels down from the top of the content in the div
TweenLite.to(myDiv, 2, {scrollTo:250});

You can define an x or y value or both (to scroll on both the x- and y-axis). For example, to scroll to 400 pixels from the top and 200 pixels from the left, do this:

TweenLite.to(myDiv, 2, {scrollTo:{y:400, x:200}, ease:Power2.easeOut});

As of GSAP 1.19.0, you can also optionally pass offsetX and/or offsetY numeric values if you want to offset the destination from the element.


//scroll #someID into view with 50 pixels from the top (like a margin)
TweenMax.to(window, 2, {scrollTo:{y:"#someID", offsetY:50}});

The demo below uses the offsetY so that each section scrolls into view just under the navigation. Click the section buttons in the demo below. Check out the JS source.

See the Pen ScrollToPlugin: Scroll to Element by GreenSock (@GreenSock) on CodePen.

By default, the ScrollToPlugin will automatically sense if the scroll position was changed outside of itself (like if the user manually started dragging the scrollbar mid-tween) and cancel that portion of the tween. If, however, you'd like to prevent the auto-killing behavior, set autoKill:false inside the scrollTo object, like:

TweenLite.to(myDiv, 2, {scrollTo:{y:400, autoKill:false}, ease:Power2.easeOut});

If you would like to detect when autoKill gets triggered you can define an onAutoKill callback.

TweenLite.to(window, 2, {scrollTo:{y:300, onAutoKill:myAutoKillFunction}});

function myAutoKillFunction() {
alert("autoKill");
}

To scroll to the maximum scroll position, use the string "max" as the value, like this:

TweenLite.to(myDiv, 2, {scrollTo:{y:"max"}});

If you don't wrap the value in an object, it will assume you want to scroll in the "y" direction, so these two lines are functionally equivalent:

TweenLite.to(myDiv, 2, {scrollTo:{y:"max"}});
TweenLite.to(myDiv, 2, {scrollTo:"max"});

Copyright 2017, GreenSock. All rights reserved. This work is subject to theterms of useor for Club GreenSock members, the software agreement that was issued with the membership.

Get GSAP

Version: 2.0.2 updated 2018-08-27

Core

    Extras

      Plugins

        By using GreenSock code, you agree to the terms of use.

        For an all-access pass to premium content

        Join Club GreenSock