Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to update position #147

Closed
franciscolourenco opened this issue Jan 8, 2017 · 7 comments
Closed

Method to update position #147

franciscolourenco opened this issue Jan 8, 2017 · 7 comments
Assignees

Comments

@franciscolourenco
Copy link

Would be useful to have a method to set the position programatically.

Related to #146

@desandro
Copy link
Owner

You can set position with left/top CSS. See demo http://codepen.io/desandro/pen/mRQdwZ

// jQuery
$draggable.css({
  left: 200,
  top: 100
});

// vanilla JS
dragElem.style.left = '200px';
dragElem.style.top = '100px';

@franciscolourenco
Copy link
Author

franciscolourenco commented Feb 10, 2017

The problem is Dragabilly.position becomes out of sync

@cmalven
Copy link

cmalven commented Feb 10, 2017

You have to both set the CSS position and manually update the draggable. This worked for me:

draggable.position.x = newPosition;
draggableEl.style.left = newPosition + 'px';

That being said, the fact that you have to do what feels like reaching into the Draggable internals to do this (and seems pretty fragile) makes me think a method to do this should be built in.

@franciscolourenco
Copy link
Author

franciscolourenco commented Feb 10, 2017

@cmalven ;)

So this issue is a proposal for an abstraction which allows you to update both at once.

Either with setters:

draggable.position = {x, y}
// or
draggable.position.x = x
draggable.position.y = y

Or with a method

draggable.setPosition(x, y)

Or both

@desandro
Copy link
Owner

desandro commented Feb 10, 2017

How about:

// add this code
Draggabilly.prototype.setPosition = function( x, y ) {
  this.position.x = x;
  this.position.y = y;
  this.setLeftTop();
};

// then you can use setPosition method
draggie.setPosition( 200, 100 )

See demo http://codepen.io/desandro/pen/qRQYYB

Add a 👍 reaction if you would like to see this feature added to Draggabilly.

@mubaidr
Copy link

mubaidr commented Feb 23, 2017

Works fine! Thank you! 👍

@desandro desandro self-assigned this Mar 22, 2018
desandro added a commit that referenced this issue Mar 27, 2018
@desandro
Copy link
Owner

🎉 Draggabilly v2.2.0 has been released new setPosition method added. 🎉

Please update! Closing this feature request as resolved. Please open a new issue if you run into trouble with setPosition. Thanks all for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants