Skip to content

Instantly share code, notes, and snippets.

@bytespider
Created January 18, 2011 21:11
Show Gist options
  • Save bytespider/785158 to your computer and use it in GitHub Desktop.
Save bytespider/785158 to your computer and use it in GitHub Desktop.
Appcelerator Titanium window drag
/*
Bullet Proof window drag
This is the most performant window dragging code
I could come up with. All the example on
developer.appcelerator.com we laggy
Version 2: More contained version
*/
var toolbarHandle = document.getElementById('toolbar');
toolbarHandle.addEventListener('mousedown', function (e){
var isDragging = true;
var mousePosition = {x:event.clientX, y:event.clientY};
document.addEventListener('mousemove', drag, false);
document.addEventListener('mouseup', function (e){
document.removeEventListener('mousemove', drag, false);
document.removeEventListener('mouseup', arguments.callee, false);
}, false);
function drag(event) {
var wnd = Titanium.UI.currentWindow;
var curentPosition = {x:wnd.getX(), y:wnd.getY()};
curentPosition.x += event.clientX - mousePosition.x;
curentPosition.y += event.clientY - mousePosition.y;
wnd.moveTo(curentPosition.x, curentPosition.y);
}
}, false);
@bytespider
Copy link
Author

You may need to prevent drag on child elements of the toolbar as required, for example search boxes and toolbar buttons. Adding a "mousedown" event with event.stopPropagation(); should do the trick.

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