Skip to content

Instantly share code, notes, and snippets.

@gujc71
Last active June 29, 2017 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gujc71/7da9892f70deab6a63e2e8e0843a02d4 to your computer and use it in GitHub Desktop.
Save gujc71/7da9892f70deab6a63e2e8e0843a02d4 to your computer and use it in GitHub Desktop.
Mouse Control Example
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mouse Control Example</title>
<style>
.selector {
position: absolute;
border: 1px dotted ;
}
</style>
<script>
var selector = null;
var sPos = null;
window.onload = function() {
selector = document.getElementsByClassName("selector")[0];
selector.style.display = "none";
document.onselectstart = function() { return false }
document.documentElement.onmousedown = mousedown;
document.documentElement.onmousemove = mousemove;
document.documentElement.onmouseup = mouseup;
}
function mousedown(ev){
sPos = {x: ev.pageX, y: ev.pageY};
selector.style.left = ev.pageX + "px";
selector.style.top = ev.pageY + "px";
selector.style.width = "0px";
selector.style.height = "0px";
selector.style.display = "";
}
function mousemove(ev){
if (sPos === null) { return;}
selector.style.width = (ev.pageX-sPos.x) + "px";
selector.style.height = (ev.pageY-sPos.y) + "px";
}
function mouseup(ev){
selector.style.width = (ev.pageX-sPos.x) + "px";
selector.style.height = (ev.pageY-sPos.y) + "px";
selector.style.display = "none";
sPos = null;
}
</script>
</head>
<body>
<div class="selector" style="left: 100px; top: 100px;">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment