Jump to content
Search Community

center cursor image on hover element

pixelarchitect test
Moderator Tag

Go to solution Solved by nico fonseca,

Recommended Posts

I am using GSAP to show an image on hover that follows the cursor while moving around, it all works fine but I can't get the image to 'center center' the cursor, what am I missing here?

Edit:
I accidentally updated the original codepen that had the wrong code, I don't have a back-up of the old -not working- version but if you are looking for the solution you can find it down below and read how we got there in the rest of the topic

Solution codepen:

See the Pen abWeQJa by pixelarchitect (@pixelarchitect) on CodePen

Edited by pixelarchitect
Updated the original codepen
Link to comment
Share on other sites

Hey @pixelarchitect , the problem is that you set a position from css: 
 

  transform: translateX(-50%) translateY(-50%);
  top: 50%;
  left: 50%;

and when you try to animate with GSA, it will add something like transform: translate(-50%, -50%)   translate(mouseX,mouseY) to your element.


I forked your pen with some changes 🙂

See the Pen 7ff6ac5a22d58a99e0571ad53d7a2acc by nicofonseca (@nicofonseca) on CodePen



I hope this help you! 💚

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

What an awesome solutions are presented up here, thank you all so much. I was playing a bit more with this and stumbled on a few "bugs" (dont know how to call them differently).

1. When the container has a padding the cursor image is automatically off center (the amount of pixels of the padding)
2. When you re-use the container (right after) again, it doubles that padding space

You can see the two bugs in the codepen down below.

I could resolve the padding issue by adding a -40 to the 'setY(e.pageY -40);' but I'm wondering if there is a cleaner solution, especially keeping point 2 in mind.

See the Pen ZEyzQWm by pixelarchitect (@pixelarchitect) on CodePen

Link to comment
Share on other sites

  • Solution

@pixelarchitect 
 

Just in case you are not aware: when you have a child with position absolute, the father needs to have position relative or fixed, otherwise the child will position itself in relation with the first father it finds with the needed positions or with the body, like in this case. Also, when you have an element with position absolute you need to set its top and left as 0 in order to force those x/y coordinates on the browser.


On the other hand, now your coordinates will be related to its container so you need to calculate the top value in relation with the base top of the container.


See the Pen 2b1d32aaa2e551b0930725caec23381b by nicofonseca (@nicofonseca) on CodePen

  • Like 5
Link to comment
Share on other sites

I would probably just call it unwanted behaviour :) 

 

It would probably be best to set you initial transforms via gsap from the get-go - that can save you some headaches in general.

 

For what I understand it is you're going for, you could set your image to position: fixed (alongside top: 0 and left: 0) instead of absolute via CSS, and use clientX/clientY instead of pageX/pageY in your JS - that makes it work just alright for me.

 

Is that more like what you're going for @pixelarchitect

 

See the Pen 21b35858191b221e86c4a3fd79e80350 by akapowl (@akapowl) on CodePen

 

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/offsetX

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageX

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/clientX

 

 

Edit: Just saw, Nico came back just before I was about to hit submit :) 

 

  • Like 3
Link to comment
Share on other sites

  • 1 year later...

Hello, I discovered this nice solution that I want to use in my portfolio. To give you some context, I use Webflow and custom code often. I'm learning javascript and I am new to gsap. Basically, I just wanted to add some smoothing to the cursor movement and delay to the image relative to the cursor. In the code, how can I add some smoothness and duration? 

 

Your help is very appreciated. 

 

Thank you,

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