Jump to content
Search Community

How To: Div fade-out and div fade-in on scroll

mathuse.dev test
Moderator Tag

Go to solution Solved by mathuse.dev,

Recommended Posts

I've been trying to work with GSAP since yesterday and I'm running into some problems and maybe some of you can point me into the right direction (I don't expect a full / complete solution). Let me explain it alongside my code:

 

Snippet from HTML with TailwindCSS:

<section id="intro" class="flex flex-col items-center relative bg-white text-black overflow-hidden">
	<div id="scrolling-text-1" class="w-full py-8 md:py-12 lg:py-16 xl:py-24 2xl:py-32">
		<div class="container grid lg:grid-cols-5 gap-8">
        	<div class="lg:col-span-2 py-12 sm:py-16 lg:py-20 xl:py-24">
            	<h5>Lorem Ipsum</h5>
                <h2 class="mb-12">Lorem ipsum dolor sit amet</h2>
            </div>
			<div class="lg:col-span-3 py-12 sm:py-16 lg:py-20 xl:py-24">
				<p class="text-lg font-grotesk font-medium my-12">
                  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
              	</p>
			</div>
		</div>
	</div>
	<div id="scrolling-text-2" class="w-full py-8 md:py-12 lg:py-16 xl:py-24 2xl:py-32">
		<div class="container grid lg:grid-cols-5 gap-8">
        	<div class="lg:col-span-2 py-12 sm:py-16 lg:py-20 xl:py-24">
            	<h5>Lorem Ipsum</h5>
                <h2 class="mb-12">Lorem ipsum dolor sit amet</h2>
            </div>
			<div class="lg:col-span-3 py-12 sm:py-16 lg:py-20 xl:py-24">
				<p class="text-lg font-grotesk font-medium my-12">
              	Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
              	</p>
			</div>
		</div>
	</div>
</section>

JS:

import gsap from "gsap/dist/gsap";
import ScrollTrigger from "gsap/dist/ScrollTrigger"
gsap.registerPlugin(ScrollTrigger);

// fade-in/out animation
let elem1 = gsap.to('#scrolling-text-1', {opacity:0, paused: true, y:"-50%"})
let elem2 = gsap.to('#scrolling-text-2', {opacity:0, paused: true, y:"0%"})

// pin section
ScrollTrigger.create({
  trigger: "#intro",
  start: "top top",
  end: "bottom top",
  pin: true,
  scrub: true,
  markers: true,
  animation: elem1
})

I have tried putting elem1 and elem2 into a timeline and executing elem2 after elem1 was finished but elem2 would never appear in the animation it would stay where it originally is. Here's the code from one of my attempts at trying to time elem1 and elem2:

 

JS (attempt at timing elem1 & elem2):

let elem1 = gsap.timeline({
  paused: true,
  onComplete: function () {
    elem2.play();
  }
});

elem1.to("#scrolling-text-1", { opacity: 0, y: "-50%" });

let elem2 = gsap.timeline({
  paused: true,
});

elem2.fromTo(
  "#scrolling-text-2",
  { opacity: 0, y: "50%" },
  { opacity: 1, y: "0%", ease: "none" }
);

ScrollTrigger.create({
  trigger: "#intro",
  start: "top top",
  end: "bottom top",
  pin: true,
  scrub: true,
  markers: true,
  animation: elem1,
});

I tried to run elem2 after the animation of elem1 was done but that didn't seem to work. Can anyone tell me why or push me into the right direction?

Thank you for reading and thank you in advance.

 

EDIT:

I tried this super simple addition with a timeline but to no avail.

let tl = gsap.timeline();
tl.to('#scrolling-text-1', {opacity:0, paused: true, y:"-50%"});
tl.fromTo('#scrolling-text-2', {y:"100%", opacity:0}, {y:"0%", opacity:1})

ScrollTrigger.create({
  trigger: "#intro",
  start: "top top",
  end: "bottom top",
  pin: true,
  scrub: true,
  markers: true,
  animation: tl //added timeline
})

 

Edited by mathuse.dev
Fixed title
Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates 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. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

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

 

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

Hi, 

 

If this is working in simple pen. Then there must be some issue in the HTML and CSS setup you have for your project.  

I will suggest now you bring the HTML and CSS of your project to the codepen and try to make it work. It will be easier to figure out the issue. 

Link to comment
Share on other sites

@Trapti added it to a pen now and tried it with a modified version which makes use of timelines. The output is very similar to the one I'm having on my local system. It somehow wont work the way I expect it to. I want to have #scrolling-text-1 fade out and #scrolling-text-2 fade in at the same time, I also want to be able to see both during the transition phase, similar to this website

See the Pen XWogpjw by mathuseTMC (@mathuseTMC) on CodePen

 

EDIT: As you can see #scrolling-text-2 is unaffected by the animation which shouldnt be

Link to comment
Share on other sites

Hi, 

Animation is working fine it is the matter of timing. Animation finishes by the time the para is in the viewport. Change scroll trigger start and end accordingly to perfect the timing. 

Check this. Also, if only a paragraph needs to be animated, then target paragraph and not the entire container. 

See the Pen gOZRRRx by tripti1410 (@tripti1410) on CodePen

  • Like 2
Link to comment
Share on other sites

The code is working fine as I said. 

tl.to("#scrolling-text-1", { y: "-50%", opacity: 0 }).fromTo(
  "#scrolling-text-2",
  { y: "0%", opacity: 0 }, {opacity: 1, color: "red"}
);

This is code snippet of your pen. I just added color: red. that animates means it's working. Also, y: 0 is not doing anything and it just changing the opacity. 

  • Like 3
Link to comment
Share on other sites

  • mathuse.dev changed the title to How To: Div fade-out and div fade-in on scroll

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