Jump to content
Search Community

animation on hover and click functions

raizo test
Moderator Tag

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

 

I have been stuck because of this click function I want to work.

 

Basically what I want to achieve is if you hover on the item the animation wil start and when unhover the animation wil reverse, this happens to work really good.

 

The part i'm having problems with is the click function. if the user click on the button the div that is related to the button will be shown.

 

Right now if the button is clicked the div will hide or not being shown. 

I think the reason for the related div not being shown is because of the Gsap animation.

I could help some advice for keeping me in the good direction how I could make this work.

 

see my codepen

See the Pen jBGYPj by spaarwt001 (@spaarwt001) on CodePen

Link to comment
Share on other sites

Touch events are not available in IE/Edge. They use pointer events, which Chrome recently adopted. So to do "touchstart" in IE/Edge and Chrome/Opera, you should use "pointerdown" instead.

 

Here's a good article about using pointer events with touch/mouse events for a fallback.

https://developers.google.com/web/fundamentals/design-and-ui/input/touch/

 

And if you're a Mac user, please make sure to properly test mouse and touch usage on a Windows computer with a touchscreen. It's a commonly overlooked test that breaks a lot of sites I come across.

  • Like 5
Link to comment
Share on other sites

Hi Manfred,

 

Thanks for the assist and nice demo!

 

----

 

Raizo,

 

When I click a menu item I see this console error:

 

Uncaught Error: Syntax error, unrecognized expression: #work_main[object Object]

 

It seems this line 91 is not doing what you expect

$('#work_main' + $newClicked).show();

I think you expect $newClicked to be a number so that the jQuery selector will be evaluated as something like #work_main1, #work_main2, etc.

But right $newClicked is an Object. 

  • Like 2
Link to comment
Share on other sites

I tried logging some of your variables and I don't think things are working the way you expect.

For instance newClicked inside your click handler is undefined.

I think the root of the problem is this:

$newClicked = $('target')

I don't follow all the conditional logic in your demo so I'm hesitant to try to fix it all.

However, here is a very reduced example of how you click on your buttons, grab the number you need from the target attribute and use it to show a div.

I think it will help you see how to make some adjustments to your code

 

 

var currentSection;
TweenLite.set(".targetDiv", {display:"none"})


 $('.bttns').on('click touchstart', function(e) { 
   console.log("you clicked on ");
   console.log(e.currentTarget);
   console.log("the number you want is " + e.currentTarget.getAttribute("target"));
   
   var sectionNumber = e.currentTarget.getAttribute("target")
   
   if(currentSection){
     TweenLite.set(currentSection, {display:"none"})
   }
   currentSection = $("#work_main" + sectionNumber)
   TweenLite.set(currentSection, {display:"block"})
 });

 

http://codepen.io/GreenSock/pen/NpLYxz?editors=0011

  • Like 2
Link to comment
Share on other sites

I tried logging some of your variables and I don't think things are working the way you expect.

For instance newClicked inside your click handler is undefined.

I think the root of the problem is this:

$newClicked = $('target')

I don't follow all the conditional logic in your demo so I'm hesitant to try to fix it all.

However, here is a very reduced example of how you click on your buttons, grab the number you need from the target attribute and use it to show a div.

I think it will help you see how to make some adjustments to your code

 

 

var currentSection;
TweenLite.set(".targetDiv", {display:"none"})


 $('.bttns').on('click touchstart', function(e) { 
   console.log("you clicked on ");
   console.log(e.currentTarget);
   console.log("the number you want is " + e.currentTarget.getAttribute("target"));
   
   var sectionNumber = e.currentTarget.getAttribute("target")
   
   if(currentSection){
     TweenLite.set(currentSection, {display:"none"})
   }
   currentSection = $("#work_main" + sectionNumber)
   TweenLite.set(currentSection, {display:"block"})
 });

 

See the Pen NpLYxz?editors=0011 by GreenSock (@GreenSock) on CodePen

Thank you Carl, this is a much better solution to my click and display related div way. But the problem i'm dealing with is that in my animation the div will animate from display none to display block when you hover over the button. but the div will also show when I click the button. But because of the animation the div will set to display none again once you unhover the button, this is also when you clicked the button. How can I force the div to stay visible when it's clicked?

Link to comment
Share on other sites

i don't know if I follow completely, but it sounds like you need to set a variable that tracks which animations were clicked. When you hover off you'll need some logic that prevents any animation that was triggered from a click to stay put and not reverse.

Or maybe you are more comfortable applying a class to the button when you click it like "clicked" and then on your hover off function you check to see if the element you are hovering off has that class and if so you don't reverse its animation.

 

Perhaps some of the stuff I posted in your other topic will help. 

  • Like 2
Link to comment
Share on other sites

Hi Blake,

 

it took some time to study the IE/Edge way of touch. At least "Getting touchy" (https://patrickhlauke.github.io/getting-touchy-presentation/) made me mad.

But I tried to fix it: 

See the Pen qrgVve by mikeK (@mikeK) on CodePen

 .

 

Hi Carl,

 

thanks for your comment. I like the trick "all and not(thisXXXX)" to handle the task - its my world of semantic logic.

Does that have influence on performance?

 

Kind regards from Hamburg

Manfred

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