Jump to content
Search Community

ScrollTrigger - Show/Hide different Text on scroll

kbatchu test
Moderator Tag

Go to solution Solved by mvaneijgen,

Recommended Posts

Hello,

 

I am trying to show/hide different text as the user scrolls. However, I noticed that when I scroll using the mouse scroll wheel, the entire text (e.g. txt2 in the codepen example)  is not displayed. It shows partial text depending upon the amount of scroll. I understand this is how ScrollTrigger works, but how do I make sure that when the user scrolls, the entire text of 'txt2' or 'txt3' or 'txt4'  is displayed at a time, not partial text. Ideally, I want to create the 'fade-in', 'fade-out' effect for each text variable i.e. txt2, txt3 etc.

 

Thanks,

Kiran

See the Pen mdgbYJw by kbatchu (@kbatchu) on CodePen

Link to comment
Share on other sites

  • Solution

As you've stated that is indeed how ScrollTrigger works. But there is another plugin that  sounds like what you're looking for have you seen the Observer plugin https://gsap.com/docs/v3/Plugins/Observer/

 

With the plugin you can watch for scroll events and then do some logic based on that. Here is a example that tweens to labels on a timeline and it just animates the previous of next label on a timeline. To implement this in your project you'll just have to put labels in your timeline where you want the scroll to stop. Hope it helps and happy tweening! 

 

See the Pen GRzrPPy by mvaneijgen (@mvaneijgen) on CodePen

 

And here an example that mixes normal scroll with the Observer plugin

 

See the Pen oNQPLqJ by GreenSock (@GreenSock) on CodePen

 

Just going to put this here:

See the Pen dyLbBom?editors=0011 by mvaneijgen (@mvaneijgen) on CodePen

  • Like 1
Link to comment
Share on other sites

Thank you so much for the examples. I think I need what you created in the second example where normal scroll is mixed with Observer plugin. There is a lot to unpack here for me. Your third example (with my use case) helped me understand how Observer by itself works. 

 

GSAP is a wonderful bag of tricks.

  • Like 2
Link to comment
Share on other sites

I am still wrapping my head around 'Observer' so I apologize for this question.

In your third example (with my use case), I see  "onDown: (self) => tweenToLabel(tl.previousLabel())". I would expect that when I am scrolling the mouse wheel 'down', it should go the 'next' label and the opposite in "onUp". How does this work?

 

Thanks,

Link to comment
Share on other sites

This might be reversed and you should set it to your preference. I'm on a Mac and have set my logic to be the same as on a phone when you scroll and move your fingers up on the track pad the page moves in that same direction. 

 

Again, you have to set this what feels right for your target audience.

Link to comment
Share on other sites

@mvaneijgen So I tried to  use the mixed observer and scrolling method to my use case. I am running into a couple of issues

1) When I mouse-wheel scroll down, the tween jumps from 'txt1' to 'txt4' directly. I don't see 'txt2' and 'txt3'

2) When I mouse-wheel scroll up (reverse direction), the tween is stuck at 'txt4' and does not display the text elements in the reverse order.

 

I think I am going wrong in the 'function tweenToLabel(direction)' but cannot figure out what. 

Do you mind taking a look?

I updated my original codepen sample.

 

Thank you.

 

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

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