Jump to content
GreenSock

grimey

fade background-image

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

Is it possible to animate the opacity of a DIV's background-image property?

 

I have a client who wants a slideshow on his index page with the header on top of the images with a little transparency. The images will swap out beneath the header. Horrible idea, I know, but it's a paycheck.

 

Thanks!

Link to comment
Share on other sites

Hello grimey and Welcome to  the GreenSock Forums!

 

This is definitely possible with GSAP, and very easy:

 

Working example animating opacity:

See the Pen KqLIe by jonathan (@jonathan) on CodePen

 

JS:

// cache element in variable
var $img = $('#box');
// set initial CSS opacity to 0
// GSAP handles the cross browser vendor prefixes
TweenMax.set($img,{opacity:0});
// animate CSS opacity to 1
TweenMax.to($img,3, {opacity:1});

HTML:

<div id="box"></div>

CSS:

#box {
     background-image:url('IMAGE_GOES_HERE.jpg');
     background-repeat: no-repeat;
     background-position: 0 0;
     background-color: transparent;
     width: 538px;
     height: 196px;
}

Also you could use autoAlpha instead of opacity in GSAP for a better fade in and fade out.

 

Same example but with autoAlpha:

See the Pen iHdek by jonathan (@jonathan) on CodePen

 

Taken from CSSPlugin Docs:

 

autoAlpha - the same thing as "opacity" except that when the value hits "0", the "visibility" property will be set to "hidden" in order to improve browser rendering performance and prevent clicks/interactivity on the target. When the value is anything other than 0, "visibility" will be set to "inherit". We don't set it to "visible" in order to honor inheritance (imagine the parent element is hidden - setting the child to visible explicitly would cause it to appear when that's probably not what was intended). And for convenience, if the element's visibility is initially set to "hidden" and opacity is 1, it will assume opacity should also start at 0. This makes it simple to start things out on your page as invisible (set your css visibility:hidden) and then fade them in whenever you want.

 

You can learn more about GSAP in the Docs:

 

Documentation: TweenMaxTweenLiteTimelineMaxTimelineLitePlugins

 

Does that help? :)

  • Like 2
Link to comment
Share on other sites

Whoa. Thank you! This is a lifesaver!

 

/grimey

 

P.S. I've actually used GSAP with AS3 for years, but am starting to need it for web work now. Whoever thought JS was a suitable replacement for AS3 was in err, IMO. :)

Link to comment
Share on other sites

Glad you got it working.. Happy Tweening!

Link to comment
Share on other sites

Hi,

 

In order to add to Jonathan's advice another choice could be use the :after pseudo-element and the CSS Rule plugin. I remembered a codepen Jamie did some time using the plugin so I'm just leeching his brain, so-to speak :D

 

I've set up a codepen:

 

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

 

Although you don't have full browser support, IE8 doesn't support the negative z-index hack that puts the pseudo element behind the actual element.

 

Rodrigo.

  • Like 5
Link to comment
Share on other sites

  • 6 months later...

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