Jump to content
Search Community

Replace background img with crossfade effect, possible?

Saleem Beg test
Moderator Tag

Recommended Posts

Hi all,

As you can see in codepen, I have four buttons, each of them is responsible for setting its corresponding image as the main background image, while the rest get arranged in an appropriate order in the 3 boxes below. The logic is working fine, but I would like the background image of the #main_image to crossfade into the new image as a button is clicked; and the same effect to be applied to the 3 boxes underneath.

 

Any tips or tricks will be appreciated.

 

Thanks!

 

PS: Please zoom codepen to 0.25x to see the 3 boxes, or simply view this in codepen.

See the Pen QWMpvgb?editors=1111 by saleemepoch (@saleemepoch) on CodePen

Link to comment
Share on other sites

Hi @Saleem Beg,

 

Thanks for being part of club GreenSock!

 

At the moment you're changing the src of the image, you won't be able to animate this.

I would recommend having the images positioned absolutely on top of each other, then you can change the opacity to fade between them.

 

// e.g.

gsap.to(oldImage, {
  autoAlpha: 0
})
gsap.to(newImage, {
  autoAlpha: 1
})

 

 

 

 

Link to comment
Share on other sites

13 hours ago, Saleem Beg said:

due to code repetition

There is absolutely no performance loss, nor bad coding habits with having multiple divs here. Loading the images by changing the src would cause a lot more issues due to http requests and repainting/rendering (not to mention it would be an accessibility/ADA challenge to implement well). If you are still worried about the extra markup, you could use WebGL to do image effect, but, again, you'd loose accessibility best practices.

  • Like 1
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...