Last active
September 25, 2015 17:53
-
-
Save antonkartashov/f58148c8ad317b667ff3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
index.html | |
<div id="div" hidden></div> | |
style.css | |
#div { | |
border: 1px dotted #000; | |
position: absolute; | |
} | |
*/ | |
var div = document.getElementById('div'), x1 = 0, y1 = 0, x2 = 0, y2 = 0; | |
function reCalc() { | |
var x3 = Math.min(x1,x2); | |
var x4 = Math.max(x1,x2); | |
var y3 = Math.min(y1,y2); | |
var y4 = Math.max(y1,y2); | |
div.style.left = x3 + 'px'; | |
div.style.top = y3 + 'px'; | |
div.style.width = x4 - x3 + 'px'; | |
div.style.height = y4 - y3 + 'px'; | |
} | |
onmousedown = function(e) { | |
div.hidden = 0; | |
x1 = e.clientX; | |
y1 = e.clientY; | |
reCalc(); | |
}; | |
onmousemove = function(e) { | |
x2 = e.clientX; | |
y2 = e.clientY; | |
reCalc(); | |
}; | |
onmouseup = function(e) { | |
div.hidden = 1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment