Salut,

J'ai besoin d'aide sur ce script. L'objectif étant de redimmensionner dynamiquement une div en cliquant sur le bouton resize. Ceci n'est qu'une ébauche je n'ai pas encore terminé.

Par contre actuellement, cela fonctionne parfaitement sous ie6 par contre sur firefox je peux rédimensionner une fois. Dès que j'essaye de redimensionner une deuxième fois le curseur devient curseur interdit et la div continu de se redimensionner en suivant la souris même quand j'ai relaché le bouton de la souris.
Sous opéra 9.6, les curseurs de ne s'affichent pas très bien. Par contre le redimensionnement fonctionne bien


J'ai du passé à côté de quelques chose. Quelqu'un peut m'aider à réparer ces deux problème. merci d'avance.



Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
236
237
238
239
240
241
242
243
244
245
246
247
 
<html>
  <head>
    <script>
 
Resizer.BDIAGONAL_TOP_LEFT         = 0;
Resizer.BDIAGONAL_TOP_RIGHT        = 1;
Resizer.BDIAGONAL_BOTTOM_LEFT      = 2;
Resizer.BDIAGONAL_BOTTOM_RIGHT     = 3;
Resizer.BLEFT               = 4;
Resizer.BTOP             = 5;
Resizer.BRIGHT             = 6;
Resizer.BBOTTOM         = 7;
Resizer.TIMER            = 20;
function Resizer(element){
        this.buildResizer(element);
}
 
Resizer.prototype = {
        _displayStyleResize: function(element){
        element.style.borderWidth = "8px";
                element.style.borderColor = "blue";
                element.style.borderStyle = "solid";
        },
 
        _hideStyleResize: function(element){
                element.style.borderWidth = "";
                element.style.borderColor = "";
                element.style.borderStyle = "";
        },
 
    _setCursorPosition: function(e){
        var resizer = this.onmousemove.resizer;
        if (typeof e != "undefined" && e.pageX && e.pageY){
            resizer.cursorPosition.x = e.pageX;
            resizer.cursorPosition.y = e.pageY;
        }else if (typeof event != "undefined" && event.x && event.y){
            resizer.cursorPosition.x = event.x;
            resizer.cursorPosition.y = event.y;
        }
        document.getElementById("spanX").innerHTML = resizer.cursorPosition.x;
                document.getElementById("spanY").innerHTML = resizer.cursorPosition.y;
    },
 
        _attachBehaviourToEvent: function(func, obj){
        func.resizer = obj;
        return func;
        },
 
    _startTimer:function(direction, cursorX, cursorY){
        if (this.isMouseDown){
            var resizer = this;
            this.resizeTimer = window.setTimeout(function(){resizer._resize(direction, cursorX, cursorY);}, Resizer.TIMER);
        }
    },
 
    _stopTimer:function(e){
        var resizer = this.onmouseup.resizer;
        resizer.element.onmouseover = resizer._attachBehaviourToEvent(resizer._selectBorder, resizer);
        resizer.element.onmouseout = resizer._attachBehaviourToEvent(resizer._deselectBorder, resizer);
        window.clearTimeout(resizer.resizeTimer);
        resizer.isMouseDown = false;
        resizer.selectBorderTimer = window.setTimeout(function(){resizer._selectBorder(null);}, Resizer.TIMER);
    },
 
    _updateRealOffset: function(){
            if (typeof this.element.clientWidth != "undefined" && this.currentWidth != this.element.offsetWidth){
            this.correctBorder = this.element.offsetWidth - this.element.clientWidth;
            }
      },
 
    _deselectBorder:function(){
        var resizer = this.onmouseout.resizer;
        if (resizer.isMouseDown) return false;
        window.clearTimeout(resizer.selectBorderTimer);
        resizer.element.style.cursor = "default";
    },
 
    _selectBorder: function(e){
        var resizer = (typeof this.onmouseover == "undefined")?this:this.onmouseover.resizer;
        var element = resizer.element;
 
        //if (resizer.isMouseDown) return false;
        var border = resizer.border;
        var topp = resizer.currentTop;
        var left = resizer.currentLeft;
        var right = resizer.currentLeft + resizer.currentWidth + resizer.correctBorder;
        var bottom = resizer.currentTop + resizer.currentHeight + resizer.correctBorder;
        var x = resizer.cursorPosition.x;
        var y = resizer.cursorPosition.y;
        resizer.direction = -1;
 
        if (x >= left && x <= right && y >= topp && y <= bottom ){
            if (x <= left + border){
                if (y <= topp + border){
                    //Diagonal Haut gauche
                    resizer.direction = Resizer.BDIAGONAL_TOP_LEFT;
                    element.style.cursor = "nw-resize";
                }else if (y >= bottom - border) {
                    //Diagonal bas gauche
                    resizer.direction = Resizer.BDIAGONAL_BOTTOM_LEFT;
                    element.style.cursor = "sw-resize";
                }else{
                    //gauche
                    resizer.direction = Resizer.BLEFT;
                    element.style.cursor = "w-resize";
                }
            }else if (x >= right - border){
                if (y <= topp + border){
                    //Diagonal Haut droit
                    resizer.direction = Resizer.BDIAGONAL_TOP_RIGHT;
                    element.style.cursor = "ne-resize";
                }else if (y >= bottom - border) {
                    //Diagonal bas droit
                    resizer.direction = Resizer.BDIAGONAL_BOTTOM_RIGHT;
                    element.style.cursor = "se-resize";
                }else{
                    //droit
                    resizer.direction = Resizer.BRIGHT;
                    element.style.cursor = "e-resize";
                }
            }else if (y <= topp + border){
                //haut
                resizer.direction = Resizer.BTOP;
                element.style.cursor = "n-resize";
            }else if (y >= bottom - border){
                //bas
                resizer.direction = Resizer.BBOTTOM;
                element.style.cursor = "s-resize";
            }
        }
        if (resizer.direction == -1){
                       element.style.cursor = "default";
                }
        if (resizer.isMouseDown) return false;
        resizer.selectBorderTimer = window.setTimeout(function(){resizer._selectBorder(null);}, Resizer.TIMER);
        return false;
    },
 
    _initResize:function(e){
        var resizer = this.onmousedown.resizer;
        resizer.isMouseDown = true;
        resizer.element.onmouseover = null;
        resizer.element.onmouseout = null;
        window.clearTimeout(resizer.selectBorderTimer);
        resizer._resize(resizer.cursorPosition.x, resizer.cursorPosition.y);
    },
 
    _resize: function(cursorX, cursorY){
        if (!this.isMouseDown || this.direction == -1) return false;
        var x = this.cursorPosition.x;
        var y = this.cursorPosition.y;
        if (this.direction == Resizer.BLEFT || this.direction == Resizer.BDIAGONAL_TOP_LEFT || this.direction == Resizer.BDIAGONAL_BOTTOM_LEFT){
            this.element.style.width  = this.currentWidth  += cursorX - x;
            this.element.style.left   = this.currentLeft    = x - 3;
        }
        if (this.direction == Resizer.BRIGHT || this.direction == Resizer.BDIAGONAL_TOP_RIGHT || this.direction == Resizer.BDIAGONAL_BOTTOM_RIGHT){
            this.element.style.width  = this.currentWidth  += x - cursorX;
        }
        if (this.direction == Resizer.BTOP || this.direction == Resizer.BDIAGONAL_TOP_RIGHT || this.direction == Resizer.BDIAGONAL_TOP_LEFT){
            this.element.style.height = this.currentHeight += cursorY - y;
            this.element.style.top    = this.currentTop     = y - 3;
        }
        if (this.direction == Resizer.BBOTTOM || this.direction == Resizer.BDIAGONAL_BOTTOM_RIGHT || this.direction == Resizer.BDIAGONAL_BOTTOM_LEFT){
              this.element.style.height = this.currentHeight += y - cursorY;
        }
            this._startTimer(x, y);
    },
 
    startResize: function(){
        this._displayStyleResize(this.element);
        this.element.style.position = "absolute";
        this._updateRealOffset();
        document.onmousemove = this._attachBehaviourToEvent(this._setCursorPosition, this);
        document.onmouseup = this._attachBehaviourToEvent(this._stopTimer, this);
    document.onmousedown = this._attachBehaviourToEvent(this._initResize, this);
        this.element.onmouseover = this._attachBehaviourToEvent(this._selectBorder, this);
        this.element.onmouseout = this._attachBehaviourToEvent(this._deselectBorder, this);
    },
 
    stopResize: function(){
        this._hideStyleResize(this.element);
        document.onmousedown = null;
        document.onmousemove = null;
        document.onmouseup = null;
    },
 
    toggleResize: function(){
        if (this.isStarted){
            this.stopResize();
        }else{
            this.startResize();
        }
        this.isStarted = !this.isStarted;
    },
 
    displayCurrentPosition: function(){
        alert("X --> " + this.resizer.cursorPosition.x + "     Y --> " + this.resizer.cursorPosition.y);
    },
 
        buildResizer: function(element){
                this.element         = element;
        this.isStarted         = false;
        this.cursorPosition     = {x:0, y:0};
        this.border         = 11;
        this.resizeTimer     = null;
        this.selectBorderTimer    = null;
        this.isMouseDown     = false;
        this.currentWidth     = element.offsetWidth;
        this.currentHeight     = element.offsetHeight;
        this.currentTop     = element.offsetTop;
        this.currentLeft     = element.offsetLeft;
        this.correctBorder     = 0; //for ie
        this.direction         = -1;
 
        element.resizer         = this;
                element.isResizerInitialized     = true;
        }
} 
 
function resize(element){
    if (!element.isResizerInitialized){
        new Resizer(element);
    }
    element.resizer.startResize();
}
    </script>
    <style>
      .maDiv{
        width:200px;
        height:200px;
        border-width : 1px;
        border-style : solid;
        border-color : green;
      }
    </style>
  </head>
  <body>
    <div>X:<span id="spanX"></span>&nbsp;&nbsp;Y:<span id="spanY"></span></div>
    <div>MOUSEDOWN X:<span id="spanXMD"></span>&nbsp;&nbsp;Y:<span id="spanYMD"></span></div>
    <div>DIRECTION:<span id="spanDir"></span></div>
    <div>Width:<span id="spanWidth"></span>&nbsp;&nbsp;Height:<span id="spanHeight"></span>&nbsp;&nbsp;DiffX:<span id="spanDiffX"></span>&nbsp;&nbsp;DIFFY:<span id="spanDiffY"></span></div>
    <div id="maDiv" class="maDiv"></div>
    <button onclick="resize(document.getElementById('maDiv'));">resize</button>
    <button onclick="alert(document.getElementById('maDiv').style.width);">offset</button>
  </body>
</html>