Jump to content
Search Community

TweenMax ScrollTo plugin keeps resetting window position

anthonyhonciano 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

HI All,

 

Loving this plugin, it works, but it always starts back to the top and scrolls to the new position. What am I doing wrong? I tried using TweenMax.set, but that doesn't do anything. Here's my code.

 

In general, I have a dynamically created list with links. The links have top coordinates for the article on the page. The click script reads that position attribute in the anchor tag and scrolls to the new position. It's just that window will always start at zero position.

       _this = $(this);
       _position = parseInt(_this.attr("position")); //gets position attribute from clicked anchor tag
       _curPosition = $(window).scrollTop();  //get windows current position
      
       TweenMax.set(window,{
           scrollTop: _curPosition
       })
       TweenMax.to(window, 2, {
           scrollTo: {y: _position},
           ease: Power3.easeInOut
       })
Link to comment
Share on other sites

I'd echo Carl's request for a codepen, but I'll throw out a hunch...

 

"window" doesn't have a scrollTop like divs do, but you're trying to set() it that way. It's kinda weird how browsers do this, I know. So instead of setting window.scrollTop, use the ScrollToPlugin so that it handles all the browser quirkiness for you:

// OLD/BAD:
TweenMax.set(window,{
     scrollTop: _curPosition
});

// NEW/GOOD
TweenMax.set(window,{
     scrollTo: _curPosition
});

Does that help at all? 

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

Ah yes, if your link has href="#", that tells the browser to go to the top of the current window, so that'd certainly explain why things appear to "reset" to the top. Expected behavior. As wia points out, the solution is to remove that # and just use an onclick to trigger your JS logic. 

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