edit delete favorite
Posted by eric on Nov 13, 2007
To make a window draggable by clicking on the window's contents, you can use the mousedown and mousemove events. The following code does not care which element is clicked on, simply responding to all mousedown events equally. You could improve this code by checking the event.target element and only setting the startPos if the element matches some criteria.
var startPos=0;
var mouseDown = function(event) {
    startPos = [ event.clientX, event.clientY];
}
var mouseMove = function(event) {
   if (startPos != 0) {
       var newX = event.screenX-startPos[0];
       var newY = event.screenY-startPos[1];
       window.moveTo(newX,newY);
   }
}
var mouseUp = function(event) {
   startPos=0;
}

window.addEventListener("mousedown",mouseDown, false);
window.addEventListener("mouseup",mouseUp, false);
window.addEventListener("mousemove",mouseMove, false);
 
Must be logged in to add a comment or review

Comments

Please submit any bugs/features and report abuse. Thanks!
Spacer
Spacer
Spacer
Top Users
Spacer
Top Tags
Spacer