Jump to content
Search Community

Lvl beginner : Animate only the card hovered on a grid

JoffreyPersia test
Moderator Tag

Go to solution Solved by akapowl,

Recommended Posts

  • Solution

Hello @JoffreyPersia

 

This is more a logic thing, than related to GSAP.

 

Instead of targetting all wrappers when creating your timeline, like you do now, you will have to create a timeline for each of those wrappers, simplest in a loop and when looping over them, target each specific wrapper and its respective paragraph only (also when it comes to the events-handling) - since you are using jQuery, that could maybe look something like this then

 

See the Pen 4c9d85a24497b277537c05d4ec246b9d by akapowl (@akapowl) on CodePen

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

@akapowl, I tried to replicate it in my website. 
I succeded to trigger correctly the animation. 
But I am facing a case that I couldn't trigger correctly
https://www.studio-artifex.fr/les-artifex/
When the mouse hover a card, the animation triggers correctly.
When I click on a card, the animation is finished, so it is good, it stays.
When the mouse leave, if the card has the class Expended, the animation stays and doesn't reverse. It is good.
If I click on an other card, I would like to reverse the animation of the card previously clicked. 

With the each function, I don't know how I can manage it. Have you an idea ? 
Is there a way to trigger a function when the element loose a class ? 

Thank you

 

contentWrapper = jQuery(".sa_card__contentContainer");

contentWrapper.each(function() {

  const thisContainer = jQuery(this);
  const thisNames = thisContainer.find('.sa_card__namesContainer div');
  var someoneElseIsActive = false;
	
  const timelineHover = gsap.timeline({
    defaults: {
      ease: "power2",
      duration: 0.3,
    },
    paused: true,
  })

  timelineHover.to(thisContainer, {opacity:1});
  timelineHover.from(thisNames, {y: 30, stagger:0.1, opacity:0}, "-=0.2")
  
  thisContainer
	  .on('mouseover', function(){ timelineHover.play() })
	  .on('mouseout', function(){
	  
	  	if( jQuery(this).parents(".sa_card__all").hasClass("expended")) {
      	
	  } else {
		  
      	timelineHover.reverse()
		  
	  }
  });

});

 

Link to comment
Share on other sites

 

This too is more of a logic question and these forums generally try to stay focussed on GSAP specific questions.

 

What you could do, is scope the variables to each of the wrappers so you can access them from outside ( not sure if the terminology is right but I hope it makes some sense at least ). I'm working with adding a class to the clicked wrapper here ( which could also be replaced by the use of a variable instead ), on click check if any of the wrappers has that class and if it has, reverse its timeline. Not sure if it is 100% correct or the best way to go, but it works.

 

See the Pen a7688626fe22789f11a8af111f96f9e3 by akapowl (@akapowl) on CodePen

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