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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<style type="text/css">
.square {
border: 1px solid #FF0000;
position: absolute;
}
.squareBlue{
border: 1px solid green;
background-color:#ccc;
position: absolute;
}
</style>
<script type="text/JavaScript">
var d;
var posx;
var posy;
var initx=false;
var inity=false;
var MyObj = null;
var newPosX = null;
var newPosY = null;
var objectToDrag = null;
var minLeft = null;
var maxRight = null;
var minTop = null;
var maxBottom = null;
//Récupération de la position de la souris en fonction de la div parente
function getMouse(obj,e){
MyObj = obj;
//obj.onmouseout = function(){endDrag()};
posx=0;posy=0; //Initialisation des variable
var ev=(!e)?window.event:e;//On test l'évènement si pour firefox ou ie
//Firefox
if (ev.pageX){
posx=ev.pageX+window.pageXOffset;
posy=ev.pageY+window.pageYOffset;
}
//Ie
else if(ev.clientX){
posx=ev.clientX+document.body.scrollLeft;
posy=ev.clientY+document.body.scrollTop;
}
//Ajout d'une méthode sur la div : dès que l'on click , on lance la création d'une div
obj.onmousedown=function(){
//Initialisation de la position de la souris
initx=posx;
inity=posy;
//Création d'une div
d = document.createElement('div');
d.className='square'
d.onmousedown= function(event){beginDrag(this,event);};
document.onmouseup=function(){endDrag()};
d.onmousemove = function(event){drag(event)};
//Positionnement
d.style.left= initx +'px';
d.style.top=inity+'px';
//Ajout dans le document ( on aurait pu l'ajouter dans la div mais plus contraignant .... )
document.getElementsByTagName('body')[0].appendChild(d);
document.getElementById('debug').innerHTML += "Creation div" + "<br />";
}
//On ajoute une méthode pour détecter le relachement de la souris sur la div parente
obj.onmouseup=function(){initx=false;inity=false;}
//On test si la souris est mousedown on modifit en live la position et la taille de la div que l'on vient de créer
if(initx){
d.style.width=Math.abs(posx-initx) +'px';
d.style.height=Math.abs(posy-inity)+'px';
d.style.left=posx-initx<0?posx + 5 +'px':initx-5+'px';
d.style.top=posy-inity<0?posy+5+'px':inity-5+'px';
document.getElementById('toto').innerHTML = "posx : " + posx+"<br />"
+"posy : " + posy+"<br />";
}
}
function redimBox(){
d.style.width=Math.abs(posx-initx) +'px';
d.style.height=Math.abs(posy-inity)+'px';
d.style.left=posx-initx<0?posx + 5 +'px':initx-5+'px';
d.style.top=posy-inity<0?posy+5+'px':inity-5+'px';
}
function findPos(obj) {
//position x / y de l'objet
var x = obj.offsetLeft || 0;
var y = obj.offsetTop || 0;
//tant qu'il y a un parent, on ajoute la position de son parent
while (obj = obj.offsetParent) {
x += obj.offsetLeft
y += obj.offsetTop
}
//Ici on retour x ou y ( ou les deux dans un tableau ou un hash
return new Array(x,y);
}
function getPositionCurseur(e){
var ev = (!e)?window.event:e;//On test l'évènement si pour firefox ou ie
//ie
if(document.all){
curX = ev.clientX;
curY = ev.clientY;
}
//netscape 4
if(document.layers){
curX = ev.pageX;
curY = ev.pageY;
}
//mozilla
if(document.getElementById){
curX = ev.clientX;
curY = ev.clientY;
}
}
function beginDrag(p_obj,e){
isDragging = true;
objectToDrag = null;
delete(objectToDrag);
objectToDrag = p_obj;
getPositionCurseur(e);
ecartX = curX - parseInt(objectToDrag.style.left);
ecartY = curY - parseInt(objectToDrag.style.top);
}
function drag(e){
//Récupération des points
minLeft = MyObj.offsetLeft ;
maxRight = minLeft + MyObj.offsetWidth ;
minTop = MyObj.offsetTop ;
maxBottom = minTop+MyObj.offsetHeight;
if(isDragging == true){
getPositionCurseur(e);
newPosX = curX - ecartX;
newPosY = curY - ecartY;
if(newPosX < (minLeft+5)){
objectToDrag.style.left = minLeft+5 + "px";
}
else if((newPosX + objectToDrag.offsetWidth) > (maxRight-5)){
objectToDrag.style.left = maxRight - objectToDrag.offsetWidth - 5 + "px";
}
else{
objectToDrag.style.left = newPosX + 'px';
}
if(isInY()){
objectToDrag.style.top = newPosY + 'px';
}
objectToDrag.className='squareblue';
}
}
function isInXMin(){
var etat = true;
if(newPosX < (minLeft+5)){
etat = false ;
}
if((newPosX + objectToDrag.offsetWidth) > (maxRight-5)){
etat = false ;
}
document.getElementById('toto').innerHTML = etat;
return etat;
}
function isInY(){
//Récupération des points
var minTop = MyObj.offsetTop ;
var maxBottom = minTop+MyObj.offsetHeight;
var etat = true;
if(newPosY < (minTop+5)){
etat = false ;
}
if((newPosY + objectToDrag.offsetHeight) > (maxBottom-5)){
etat = false ;
}
document.getElementById('toto').innerHTML = etat;
return etat;
}
function endDrag(){
isDragging = false;
if(objectToDrag != null)objectToDrag.className='square';
objectToDrag = null;
}
var isDragging = false;
//Function permettant d'oculter un bug si l'on relache le bouton sur une div ou en dehors de la div parente
function initXFalse(){
initx = false;
inity = false;
}
function Add_Event( obj_, event_, func_ ){
//-- Firefox
if( obj_.addEventListener )
obj_.addEventListener( event_, func_, false);
//-- pour IE
if( obj_.attachEvent )
obj_.attachEvent( "on" + event_, func_); // ajout de "on" devant l'event
}
</script>
</head>
<body onload="Add_Event(document, 'mouseup', initXFalse);Add_Event(document, 'mousemove', drag);">
<div id="debug" style="width:500px;height:100px;overflow:auto;border:1px solid red">
</div>
<div style="position:absolute;margin-top:-200px;top:50%;margin-left:-200px;left:50%;width:400px;height:400px; border:2px dashed #666;" onmousemove="getMouse(this,event)">
</div>
<div style="width:500px;height:100px;overflow:auto;border:1px solid red" id="toto">
</div>
</body>
</html> |
Partager