Jump to content
Search Community

ScrollTo: Next and Previous

ronnieb test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Greetings all,

 

I am working on a site which includes simple "next" and "previous" buttons to navigate using "scrollTo". I have successfully configured scrollTo to navigate to multiple anchor elements moving forward (i.e. "#myDiv1, #myDiv2, etc.") using the "next" button, but I was wondering if it is possible to scroll back to the previous anchor using just the "previous" button (I don't want to scroll back to the beginning).

 

I hope this is clear enough. Any advice would be greatly appreciated.

 

Cheers,

Ron

 

P.S. Greensock rocks! I've only been using it for a short while, but even as a newbie I've been able to exceed my clients' expecations.

Link to comment
Share on other sites

Since your divs are named chronologically, you can just use a variable to keep track of that number. Your next button will increment that variable and then you can just target the next div using that number.

 

here is a very quick example:

http://codepen.io/GreenSock/pen/36df4eabb4d7c78b562fd46b5f612d37

 

Also, you could use jQuery's next() and previous() methods to find the proper sibling of the current div.

 

http://api.jquery.com/next/

 

Using this method, instead of incrementing and decrementing a number, you would just store a reference to "currently scrolled-to div" and then use each button to find the next() or previous() sibling and get its top offset.

  • Like 1
Link to comment
Share on other sites

Hi Ron,

 

The best choice I can think of is to use getLabelBefore and getLabelAfter methods.

 

Create a paused timeline and for every step of the scrolling animation add a label. Once the timeline is ready you add the clicks events to your timeline and use those methods along with the tweenTo method

var tl = new TimelineMax({paused:true}),
    nextBtn = $("button#nextBtn"),
    prevBtn = $("button#prevBtn");

tl
    .to(element, time, {vars}, 'label')
    .to(element, time, {vars}, 'label')
    .to(element, time, {vars}, 'label')
    .to(element, time, {vars}, 'label');

nextBtn.click(function()
{
    tl.tweenTo(tl.getLabelAfter());
});
prevBtn.click(function()
{
    tl.tweenTo(tl.getLabelBefore());
});

You can see the api reference for more info.

 

Hope this helps,

Cheers,

Rodrigo.

  • Like 1
Link to comment
Share on other sites

Thank you, gentlemen. This is exactly what I am looking for! I appreciate your taking the time to address this issue so thoroughly. This forum is such a great learning resource.

 

Regards,

Ron

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...