Jump to content
Search Community

Playing a progress controlled timeline in reverse

rflc 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

Hello. I'm stuck tryin to reverse a timeline controlled by  page scrolling. I'm calling a function with tl.progress(%) everytime the scroll event fires, but when I apply tl.reverse() and tl.progress(%) or simply tl.progress( decreasing %) nothing happens.  Not sure what I'm doing wrong.

Link to comment
Share on other sites

I think the problem is (if I'm understanding you correctly) is that progress is not set with x% ... it's set with a value 0 to 1 (.5 being the halfway or 50% mark).

 

So, you should not deal at all with % or +/- values when setting progress. It would simply be a calculation of scrollPosition/documentHeight. That formula will always return a positive value (save for elastic scrolling where negative values can become a factor) >= 0 and <= 1.

  • Like 1
Link to comment
Share on other sites

Yes I used that representation for illustration purposes. I'm supplying values from 0 to 1 and viceversa when scrolling back. This is what I got so far:

 

var animate = (function(){
    // Scene one
    var one = function(pct, rwd) {
        let tl = new TimelineMax({paused: true});
        if (rwd)
            tl.reverse(pct);
        tl.to('#hand', 2, {opacity: 0})
            .set('#ip', {attr:{mask: ""}}, 1.5);
         tl.progress(pct);
    }

    // Scene two
    var two = function(pct, fwd){
        let tl = new TimelineMax({paused: true});
    }

    // Scene three

    var three = function(pct, fwd){
        let tl = new TimelineMax({paused: true});
    }
    // Scene Four

    var four = function(ptc, fwd){
        let tl = new TimelineMax({paused: true});
    }

    var arr = [one, two, three, four];

    return arr;
})();


var play = (function(){

  // closure scope
  let length;
  let lastY = null;
  let i = 0;

  //function called by the event listener
  return function(e){
    let frame = scene[i].getBoundingClientRect();
    let cam   = camera[i].getBoundingClientRect();
    length    = cam.height - window.innerHeight;
    // Determine scroll direction
    if (lastY < window.pageYOffset) {
      if (frame.top >=  0 && frame.bottom <= window.innerHeight) {
        // Get camera scroll percentage
        let pct = ((cam.top / length) * -1).toFixed(2);
        animate[i](pct, 0);
      }
      else if (frame.bottom < 0) {
        i++;
      }
    }
    else {
      if (frame.top <=  0 && frame.bottom <= window.innerHeight) {
        let pct = ((cam.top / length) * -1).toFixed(2);
        animate[i](pct, 1);
      }
      else if (frame.top > window.innerHeight && (i > 0)) {
        i--;
      }
    }

    lastY = window.pageYOffset;
  }
})();

 

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