Jump to content
Search Community

zIndex Boost with GSAP Observer?

pietM test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

  • Solution

Hi,

 

How about this?

let zIndex = 1000;
gsap.utils.toArray(".item").forEach((item) => {

  let observer = Observer.create({
    type: "touch,pointer",
    target: item,
    onPress(self) {
      onMove(self);
      isPressed = true;
      updateCursor();
      // Increase the z-index value and apply it to the target element
      zIndex++;
      gsap.set(item, { zIndex });
    },
    onRelease() {
      isPressed = false;
    },
    onMove,
    onDrag(self) {
      // we're dragging the item
    }
  });
});

I removed most of the code that is not needed to illustrate my point. Here is a fork of your demo:

See the Pen ExJgaBy by GreenSock (@GreenSock) on CodePen

 

Also you can use Draggable, it has z-index boosting baked right into it so you don't have to worry about that. You'll have to deal with the resizing bit but it mostly works the way I forked your demo:

See the Pen MWRjYZN by GreenSock (@GreenSock) on CodePen

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

Thanks for taking the time, @Rodrigo!

This is very helpful and appreciated. I know about Draggable's built-in boosting, but was unclear about how to achieve this using the GSAP Observer.  This is what I needed:

 

// Increase the z-index value and apply it to the target element
      zIndex++;
      gsap.set(item, { zIndex });

 

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