I've been looking for drag-and-drop examples/tutorials for HTML5, but all of them so far involve an object that fades as it's being dragged and without being constrained to any axis. I was wondering if it's possible to have the actual object itself be dragged as opposed to a ghost of it and whether I can constrain it to X or Y axis?
Thanks!
Yes, easily, by writing it yourself.
elem.onmousedown = function(e) {
e = e || window.event;
var start = 0, diff = 0;
if( e.pageX) start = e.pageX;
else if( e.clientX) start = e.clientX;
elem.style.position = 'relative';
document.body.onmousemove = function(e) {
e = e || window.event;
var end = 0;
if( e.pageX) end = e.pageX;
else if( e.clientX) end = e.clientX;
diff = end-start;
elem.style.left = diff+"px";
};
document.body.onmouseup = function() {
// do something with the action here
// elem has been moved by diff pixels in the X axis
elem.style.position = 'static';
document.body.onmousemove = document.body.onmouseup = null;
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With