Jump to content
GreenSock

Learning

Animating Stacked Backgrounds?

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 guys,

 

I'm trying to stack 3 images onto a div's background.

Wondering if it's possible to tween the opacity of this 3 images separately?

Can I target it separately?

 

My background image css looks something like this,

background-image: url(https://example.com/base.jpg) , url(https://example.com/overlay1.png), url(https://example.com/overlay2.png);
Link to comment
Share on other sites

Hi,

 

Sure you can. Since version 1.18.3 complex strings are possible for this type of properties. Check this codepen:

 

See the Pen oAIan by rhernando (@rhernando) on CodePen

 

The old way of doing it is commented out and just below is the new way.

 

Unfortunately you can't target each background separately, since you're animating a property of one element. Turns out that if you change the values the overwrite manager will cancel the current animation and start a new one, that's why in the sample all values are kept between each instance.

 

Here's the original CSS:

background-position: 100px 100px, 300px 200px, 500px 0px;

Here's the timeline:

tl
  .to(div1, 5, {backgroundPosition:"100px 100px, 300px 200px, 0px 0px", ease:Linear.easeNone})
  .to(div1, 2.5, {backgroundPosition:"0px 0px, 300px 200px, 0px 0px", ease:Linear.easeNone}, 2.5)
  .to(div1, 1, {backgroundPosition:"0px 0px, 100px 0px, 0px 0px", ease:Linear.easeNone}, 4);

The first instance moves the third background to zero-zero. The second moves the first background to zero-zero and the value for the third  is the same, otherwise the overwrite manager creates a new instance to the new values cancelling the previous instance. Finally the third instance moves the second background to 100-zero and the values of the other two backgrounds are the same.

 

If you need something different, then you'll have to stick with the old solution.

 

Happy Tweening!!

  • Like 3
Link to comment
Share on other sites

Blake.. Haha.. that looks like the cloud repeating background-image i created :)

  • Like 1
Link to comment
Share on other sites

Yep, Blake's solution is pretty solid and avoids a lot of code in the process.

  • Like 2
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.
×