Jump to content
Search Community

How to animate an image swap on hover?

jonathan.a.katz test
Moderator Tag

Go to solution Solved by Carl,

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

I'm trying to animate an image swap when hovering on different elements in the page.

I already know I'm on the wrong track given how much code I've written to accomplish this. There must be a simpler solution (and this one doesn't even work, so there's that too).

The codepen demonstrates what I have already and the effect I'm trying to achieve.  Any help on a better/working solution would be greatly appreciated!

See the Pen GorLZb by jakatz (@jakatz) on CodePen

Link to comment
Share on other sites

  • Solution

Thanks for providing the demo, it provided a nice starting point.

 

There are probably a dozen different ways to do this. Personally, I'm not a big fan of adding and removing classes on elements. The approach I took is to give each item an animation (a TweenLite tween) and when you roll over and out that animation gets played and reversed respectively like so:

 

var images = $(".image"),
    items = $(".box");

items.each(function(index, element){
  element.animation = TweenLite.to(images[index], 1, {x:0, paused:true})
})

function over(){
  this.animation.play();
}

function out(){
  this.animation.reverse();
}

items.hover(over, out);

http://codepen.io/GreenSock/pen/gPgVbN?editors=001

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