1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans titre</title>
</head>
<body>
<style type="text/css">
#fond { position: relative; width: 320px; height: 40px; background-color: #CCCCCC; border: 1px solid #666666; }
.box { position: absolute; background-color: #990000; }
</style>
<div id='fond'></div>
<h3 id='h'></h3>
</body>
</html>
<script language="javascript" type="text/javascript">
var MIN_WIDTH = 8;
var MIN_HEIGHT = 8;
var fond = document.getElementById('fond');
var box;
fond.onmousedown = mouseDown;
fond.onmouseup = mouseUp;
fond.onmousemove = mouseMove;
/** objet box */
function Box(left, top, width, height) {
this.left = left;
this.top = top;
this.width = width;
this.height = height;
this.element = document.createElement('div');
this.element.className = 'box';
}
Box.prototype.update = function() {
this.element.style.left = this.left + 'px';
this.element.style.top = this.top + 'px';
this.element.style.width = this.width + 'px';
this.element.style.height = this.height + 'px';
}
Box.prototype.resize = function(width, height) {
if(width>=MIN_WIDTH) this.width = width;
if(height>=MIN_HEIGHT) this.height = height;
this.update();
}
/** détermine la position x, y d'un élément dans la page */
function getElementPosition(element) {
var offsetLeft = 0;
var offsetTop = 0;
while (element) {
offsetLeft += element.offsetLeft;
offsetTop += element.offsetTop;
element = element.offsetParent;
}
if (navigator.userAgent.indexOf("Mac") != -1 &&
typeof document.body.leftMargin != "undefined") {
offsetLeft += document.body.leftMargin;
offsetTop += document.body.topMargin;
}
return {left:offsetLeft, top:offsetTop};
}
/** retourne les coodronnée de la souris relatives à un element */
function getRelativeMouse(elemID, evt) {
var pos = getElementPosition(elemID);
return {left:evt.pageX - pos.left, top:evt.pageY - pos.top, buttons:evt.button};
}
function mouseMove(e) {
mouse = getRelativeMouse(this, e);
if(box) box.resize(mouse.left - box.left, mouse.top - box.top);
document.getElementById('h').innerHTML = mouse.left + 'x' + mouse.top;
}
function mouseDown(e) {
mouse = getRelativeMouse(this, e);
box = new Box(mouse.left, mouse.top, MIN_WIDTH, MIN_HEIGHT);
this.appendChild(box.element);
document.getElementById('h').innerHTML = mouse.left + 'x' + mouse.top;
}
function mouseUp(e) {
if(box) {
//appel de la page php qui va créer la box dans la db et retourner une page qui affiche toutes le box existantes
//document.location.href = 'page.php?top='+box.top+'&left='+box.left+'&width='+box.width+'&height='+box.height';
}
box = false;
mouse = getRelativeMouse(this, e);
document.getElementById('h').innerHTML = mouse.left + 'x' + mouse.top;
}
</script> |
Partager