40 likes | 57 Views
Draggable Rectangle. <style type="text/css"> #div1 { position: absolute; } </style> ... <div id="div1" onmousedown="mouseDown(event);" onmousemove="mouseMove(event);" onmouseup="mouseUp(event);">Drag Me!</div>. Event Handlers. isMouseDown = false;
E N D
Draggable Rectangle <style type="text/css"> #div1 { position: absolute; } </style> ... <div id="div1" onmousedown="mouseDown(event);" onmousemove="mouseMove(event);" onmouseup="mouseUp(event);">Drag Me!</div> CS 142 Lecture Notes: Forms
Event Handlers isMouseDown = false; function mouseDown(event) { mouseX = event.clientX; mouseY = event.clientY; isMouseDown = true; } function mouseMove(event) { if (!isMouseDown) { return; } element = document.getElementById("div1"); element.style.left = (element.offsetLeft + (event.clientX - mouseX)) + "px"; element.style.top = (element.offsetTop + (event.clientY - mouseY)) + "px"; mouseX = event.clientX; mouseY = event.clientY; } function mouseUp(event) { isMouseDown = false; } CS 142 Lecture Notes: Forms
Which Element Gets Event? <body> <table> <tr> <td>xyz</td> </tr> </table> </body> Click on this CS 142 Lecture Notes: Forms