Jump to content
Search Community

Hover even listener doesnt work with GSAP

momo12 test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

  • Solution

Hi,

 

The issue is that you're using querySelector and not querySelectorAll. The querySelector method returns the first element it finds with the passed selector and then stops looking. The querySelectorAll.... well returns all the element it finds :D

 

This should work as expected:

var hover = document.querySelectorAll(".sss");

hover.forEach((el) => {
  el.addEventListener("mouseenter", (e) => {
    gsap.to(cursor, { scale: 0.5 });
    gsap.to(cursorBorder, { scale: 2 });
  });

  el.addEventListener("mouseout", (e) => {
    gsap.to(cursor, { scale: 1 });
    gsap.to(cursorBorder, { scale: 1 });
  });
});

Also you have  missing slashes in some closing tags:
 

<div class="section-1">
  <div class="img-1 sss"><div><!-- HERE -->
  <div class="space-wr"></div>
  <div class="img-2 sss"><div><!-- HERE -->
  <div class="space-wr"></div>
</div>

Happy Tweening!!!

  • Like 1
Link to comment
Share on other sites

3 minutes ago, Rodrigo said:

Hi,

 

The issue is that you're using querySelector and not querySelectorAll. The querySelector method returns the first element it finds with the passed selector and then stops looking. The querySelectorAll.... well returns all the element it finds :D

 

This should work as expected:

var hover = document.querySelectorAll(".sss");

hover.forEach((el) => {
  el.addEventListener("mouseenter", (e) => {
    gsap.to(cursor, { scale: 0.5 });
    gsap.to(cursorBorder, { scale: 2 });
  });

  el.addEventListener("mouseout", (e) => {
    gsap.to(cursor, { scale: 1 });
    gsap.to(cursorBorder, { scale: 1 });
  });
});

Also you have  missing slashes in some closing tags:
 

<div class="section-1">
  <div class="img-1 sss"><div><!-- HERE -->
  <div class="space-wr"></div>
  <div class="img-2 sss"><div><!-- HERE -->
  <div class="space-wr"></div>
</div>

Happy Tweening!!!

You're an angel! Thanks man!

Link to comment
Share on other sites

36 minutes ago, Rodrigo said:

Hi,

 

The issue is that you're using querySelector and not querySelectorAll. The querySelector method returns the first element it finds with the passed selector and then stops looking. The querySelectorAll.... well returns all the element it finds :D

 

This should work as expected:

var hover = document.querySelectorAll(".sss");

hover.forEach((el) => {
  el.addEventListener("mouseenter", (e) => {
    gsap.to(cursor, { scale: 0.5 });
    gsap.to(cursorBorder, { scale: 2 });
  });

  el.addEventListener("mouseout", (e) => {
    gsap.to(cursor, { scale: 1 });
    gsap.to(cursorBorder, { scale: 1 });
  });
});

Also you have  missing slashes in some closing tags:
 

<div class="section-1">
  <div class="img-1 sss"><div><!-- HERE -->
  <div class="space-wr"></div>
  <div class="img-2 sss"><div><!-- HERE -->
  <div class="space-wr"></div>
</div>

Happy Tweening!!!

@Rodrigo Sorry I have a simple question. The hovering doesn't last and sometimes the circle becomes small like there is no hover. 

Link to comment
Share on other sites

Hi,

 

The issue is that as soon as the circle catches up with the pointer it sits between the pointer and the image, so as soon as the pointer moves, even one pixel, the pointer is no longer over or inside the image, so the mouseleave event is triggered.

 

I can think of two solutions. One to set up a container for each image with a relative position, add the image and on top of that, with a large z-index value, another container that sits on top of the cursor followers and listen to the mouse enter/leave events in that element, something like this:

<div class="image-wrapper">
  <img src="" alt="" class="" />
  <!-- LISTEN TO THE EVENTS IN THIS NEXT DIV -->
  <div></div>
</div>

The other is to get the coordinates and size of each image with getBoundingClientRect() and check on the mouse move event if the pointer is between those coordinates and play/reverse the animation accordingly:
https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

 

A third option is to remove the dot in the center using autoAlpha: 0 or scale: 0:

el.addEventListener("mouseenter", (e) => {
  // THIS
  gsap.to(cursor, { scale: 0 });
  // OR THIS
  gsap.to(cursor, { scale: 0.5, autoAlpha: 0 });
  gsap.to(cursorBorder, { scale: 2 });
});

el.addEventListener("mouseout", (e) => {
  // THIS
  gsap.to(cursor, { scale: 1 });
  // OR THIS
  gsap.to(cursor, { scale: 1, autoAlpha: 1 });
  gsap.to(cursorBorder, { scale: 1 });
});

 

Happy Tweening!!!

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