Jump to content
GreenSock

Joe Hakooz

Trigger Draggable onDrag Event

Go to solution Solved by Diaco,

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

I've got a draggable that uses the onDrag event. 

 

Is it possible to "trigger" the onDrag event from another DOM element? I can do it but inside the event this.x returns NaN. 

I think onDragScope might be helpful but not sure how. 

 

For example...

var draggables = Draggable.create(mydraggable, {type: "x", onDrag: updateDraggable });
function updateDraggable(){
    console.log(this.x) // should show a number. Doesn't work when triggered by my button.
}

$('#mybutton').click(function(){
    updateDraggable(draggables[0]); // yeah I know, lame attempt by using a parameter.
});

Thanks for any suggestions!

 

Link to comment
Share on other sites

  • Solution

Hi Joe Hakooz  :)

 

pls try this :

var D = Draggable.create( mydraggable ,{ type:"x", onDrag:updateDraggable });

function updateDraggable(){ console.log( this.x || D[0].x ) };

$('#btn').click( updateDraggable );
  • Like 1
Link to comment
Share on other sites

Thanks Diaco!

 

Very clever trick. Do you see any performance hit with using D[0].x over this.x ?

I ask because my updateDraggable is doing some fairly heavy lifting and I'd love to keep it as optimized as possible. 

Link to comment
Share on other sites

i don't think there's any considerable difference... :)

 

but i prefer to use this : this.x || D[0].x 

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