Précédent   Forum des professionnels en informatique > Webmasters - Développement Web > JavaScript
JavaScript Forum programmation JavaScript. Lire : Cours JavaScript, FAQ JavaScript, Toutes les FAQ JavaScript et Sources JavaScript
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 21/06/2011, 18h50   #1
Candidat au titre de Membre du Club
 
Inscription : mars 2009
Messages : 33
Détails du profil
Informations forums :
Inscription : mars 2009
Messages : 33
Points : 10
Points : 10
Par défaut Un drag qui drag trop !

Bonjour,

J'essaye de mettre au point une map draguable en position absolute dans un conteneur en position relative. Celà fonctionne très bien pour Firefox, chrome et IE, par contre pas sur Opera. Sur le site de l'auteur pourtant, avec Opera son exemple fonctionne correctement. Le mien lui, ne stop pas le drag au bord droite et bas de la map.

Le lien témoin : clic

Mon code JS :
Code :
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
 
<script type="text/javascript">
function Position(x, y)
{
  this.X = x;
  this.Y = y;
 
  this.Add = function(val)
  {
    var newPos = new Position(this.X, this.Y);
    if(val != null)
    {
      if(!isNaN(val.X))
        newPos.X += val.X;
      if(!isNaN(val.Y))
        newPos.Y += val.Y
    }
    return newPos;
  }
 
  this.Subtract = function(val)
  {
    var newPos = new Position(this.X, this.Y);
    if(val != null)
    {
      if(!isNaN(val.X))
        newPos.X -= val.X;
      if(!isNaN(val.Y))
        newPos.Y -= val.Y
    }
    return newPos;
  }
 
  this.Min = function(val)
  {
    var newPos = new Position(this.X, this.Y)
    if(val == null)
      return newPos;
 
    if(!isNaN(val.X) && this.X > val.X)
      newPos.X = val.X;
    if(!isNaN(val.Y) && this.Y > val.Y)
      newPos.Y = val.Y;
 
    return newPos;
  }
 
  this.Max = function(val)
  {
    var newPos = new Position(this.X, this.Y)
    if(val == null)
      return newPos;
 
    if(!isNaN(val.X) && this.X < val.X)
      newPos.X = val.X;
    if(!isNaN(val.Y) && this.Y < val.Y)
      newPos.Y = val.Y;
 
    return newPos;
  }
 
  this.Bound = function(lower, upper)
  {
    var newPos = this.Max(lower);
    return newPos.Min(upper);
  }
 
  this.Check = function()
  {
    var newPos = new Position(this.X, this.Y);
    if(isNaN(newPos.X))
      newPos.X = 0;
    if(isNaN(newPos.Y))
      newPos.Y = 0;
    return newPos;
  }
 
  this.Apply = function(element)
  {
    if(typeof(element) == "string")
      element = document.getElementById(element);
    if(element == null)
      return;
    if(!isNaN(this.X))
      element.style.left = this.X + 'px';
    if(!isNaN(this.Y))
      element.style.top = this.Y + 'px';
  }
}
 
function hookEvent(element, eventName, callback)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.addEventListener)
  {
    element.addEventListener(eventName, callback, false);
  }
  else if(element.attachEvent)
    element.attachEvent("on" + eventName, callback);
}
 
function unhookEvent(element, eventName, callback)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.removeEventListener)
    element.removeEventListener(eventName, callback, false);
  else if(element.detachEvent)
    element.detachEvent("on" + eventName, callback);
}
 
function cancelEvent(e)
{
  e = e ? e : window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}
 
function getMousePos(eventObj)
{
  eventObj = eventObj ? eventObj : window.event;
  var pos;
  if(isNaN(eventObj.layerX))
    pos = new Position(eventObj.offsetX, eventObj.offsetY);
  else
    pos = new Position(eventObj.layerX, eventObj.layerY);
  return correctOffset(pos, pointerOffset, true);
}
 
function getEventTarget(e)
{
  e = e ? e : window.event;
  return e.target ? e.target : e.srcElement;
}
 
function absoluteCursorPostion(eventObj)
{
  eventObj = eventObj ? eventObj : window.event;
 
  if(isNaN(window.scrollX))
    return new Position(eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft,
      eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
  else
    return new Position(eventObj.clientX + window.scrollX, eventObj.clientY + window.scrollY);
}
 
function dragObject(element, attachElement, lowerBound, upperBound, startCallback, moveCallback, endCallback, attachLater)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
      return;
 
  var cursorStartPos = null;
  var elementStartPos = null;
  var dragging = false;
  var listening = false;
  var disposed = false;
 
  function dragStart(eventObj)
  {
    if(dragging || !listening || disposed) return;
    dragging = true;
 
    if(startCallback != null)
      startCallback(eventObj, element);
 
    cursorStartPos = absoluteCursorPostion(eventObj);
 
    elementStartPos = new Position(parseInt(element.style.left), parseInt(element.style.top));
 
    elementStartPos = elementStartPos.Check();
 
    hookEvent(document, "mousemove", dragGo);
    hookEvent(document, "mouseup", dragStopHook);
 
    return cancelEvent(eventObj);
  }
 
  function dragGo(eventObj)
  {
    if(!dragging || disposed) return;
 
    var newPos = absoluteCursorPostion(eventObj);
    newPos = newPos.Add(elementStartPos).Subtract(cursorStartPos);
    newPos = newPos.Bound(lowerBound, upperBound)
    newPos.Apply(element);
    if(moveCallback != null)
      moveCallback(newPos, element);
 
    return cancelEvent(eventObj);
  }
 
  function dragStopHook(eventObj)
  {
    dragStop();
    return cancelEvent(eventObj);
  }
 
  function dragStop()
  {
    if(!dragging || disposed) return;
    unhookEvent(document, "mousemove", dragGo);
    unhookEvent(document, "mouseup", dragStopHook);
    cursorStartPos = null;
    elementStartPos = null;
    if(endCallback != null)
      endCallback(element);
    dragging = false;
  }
 
  this.Dispose = function()
  {
    if(disposed) return;
    this.StopListening(true);
    element = null;
    attachElement = null
    lowerBound = null;
    upperBound = null;
    startCallback = null;
    moveCallback = null
    endCallback = null;
    disposed = true;
  }
 
  this.GetLowerBound = function()
  { return lowerBound; }
 
  this.GetUpperBound = function()
  { return upperBound; }
 
  this.StartListening = function()
  {
    if(listening || disposed) return;
    listening = true;
    hookEvent(attachElement, "mousedown", dragStart);
  }
 
  this.StopListening = function(stopCurrentDragging)
  {
    if(!listening || disposed) return;
    unhookEvent(attachElement, "mousedown", dragStart);
    listening = false;
 
    if(stopCurrentDragging && dragging)
      dragStop();
  }
 
  this.IsDragging = function(){ return dragging; }
  this.IsListening = function() { return listening; }
  this.IsDisposed = function() { return disposed; }
 
  if(typeof(attachElement) == "string")
    attachElement = document.getElementById(attachElement);
  if(attachElement == null)
    attachElement = element;
 
  if(!attachLater)
    this.StartListening();
}</script>
Le code qui lance le tout :
Code :
1
2
3
4
5
6
7
8
9
 
<script type="text/javascript">
                    var el = document.getElementById('cityMap');
                    var parent = el.parentNode;
                    var leftEdge = parent.clientWidth - el.clientWidth;
                    var topEdge = parent.clientHeight - el.clientHeight;
                    var dragObj = new dragObject(el, null,
                    new Position(leftEdge, topEdge), new Position(0, 0));
</script>
le code html et le css :
Code :
1
2
3
4
5
 
<div id="cityMapcontener">
       <div id="cityMap" >
       </div>
</div>
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
#cityMapcontener {
    width: 325px;
    height: 300px;
    float: right;
    position: relative;
    overflow: hidden;
}
 
#cityMap {
    width:  1800px;
    height: 1200px;
    position: absolute;
    cursor: move;
    top: 0px;
    left: 25px;
    background-image: url('map.jpg');
}
Bootax est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/06/2011, 15h47   #2
Expert Confirmé
 
Avatar de javatwister
 
Homme
danseur
Inscription : août 2003
Messages : 2 667
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Calvados (Basse Normandie)

Informations professionnelles :
Activité : danseur

Informations forums :
Inscription : août 2003
Messages : 2 667
Points : 3 035
Points : 3 035
très digeste ce code pour bouger une image, bravo

par ailleurs, le drag continue quand tu te balade hors du cadre (et je ne parle pas d'opéra);
javatwister est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/06/2011, 19h13   #3
Modérateur
 
Avatar de NoSmoking
 
Homme
Inscription : janvier 2011
Messages : 2 943
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Isère (Rhône Alpes)

Informations forums :
Inscription : janvier 2011
Messages : 2 943
Points : 4 770
Points : 4 770
Bonjour,
une page en ligne serait par contre la bien venue pour appréhender le comportement, car l'exemple fonctionne nickel.

Ce que tu nous livre n'est pas suffisant, il n'y à pas d'IMG dans tes DIVs et c'est elle qui move.
NoSmoking est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/06/2011, 20h43   #4
Expert Confirmé
 
Avatar de javatwister
 
Homme
danseur
Inscription : août 2003
Messages : 2 667
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Calvados (Basse Normandie)

Informations professionnelles :
Activité : danseur

Informations forums :
Inscription : août 2003
Messages : 2 667
Points : 3 035
Points : 3 035
Je répète en clair:
"270 lignes de script pour faire bouger une image? Non, tu ferais aussi bien de photocopier ton image et de la bouger sur ton écran en plaquant ta main dessus."

Ce qui est inquiétant, c'est que tu ne trouves rien de choquant dans ta demande.
javatwister est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/06/2011, 10h20   #5
Membre habitué
 
Homme
Étudiant
Inscription : mai 2011
Messages : 226
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : mai 2011
Messages : 226
Points : 126
Points : 126
Quoi ?! Je me suis fait benner mon SUPER calembour !

Bref,même si je suis pas completement d'accord avec la manière dont il l'exprime, je suis assez d'accord avec javatwister sur le fait qu'en nous posant comme ça tes 200 lignes de codes, ça facilite pas la vie de ceux qui sont prêts à t'aider...

Bon, à part ça, as-tu regardé du côté de JQuery. Il y a des méthodes de dragage toutes faites normalement. Ca devrait sacrément te simplifier la vie.

Bonne chance
Sharcoux est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 22h59.


 
 
 
 
Partenaires

Hébergement Web