Jump to content
Search Community

Make duplicate element follow original

Daniel Hult test
Moderator Tag

Recommended Posts

Hey guys,

Hope someone here can help me with a small issue Im having.

I want to create an effect where you copy an html element in JS and make that "move around" the original element on mousemove over the original element. Also want to clamp the number it can go in each direction by e.g 20px or something.. I've tried to use gsap's utils clamp function, but I don't couldn't get it working properly.

In the codepen at the moment I have the effect working in just one direction (just positive values) without any clamping logic.

Anybody that can help me out? :D 

See the Pen xxZermJ by daniel-hult (@daniel-hult) on CodePen

Link to comment
Share on other sites

Hey Daniel. I'm not quite sure what the exact end goal is, but you can use clamp like so inside of a tween's vars:

modifiers: {
  x: gsap.utils.clamp(-this.max, this.max),
  y: gsap.utils.clamp(-this.max, this.max),
}

Side notes:

  • It might be cleaner to use classes then a bunch of inline JS styles?
  • It might make sense to merge your duplicateContent and hoverEffect functions if this is all you're doing. But that's more so preference.
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...