Drag & drop JS

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Pagina: 1 2 volgende »

- Raoul -

- Raoul -

22/08/2010 23:24:13
Quote Anchor link
Hoiz
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<html>
<head>
<style type="text/css">
 #DragDiv
 {
  height:100px;
  width:100px;
  background-color:blue;
  position:relative;
 }
</style>
<script type="text/javascript">
 var DragDiv;
 var Dragging = false;
 window.onload = function()
 {
  DragDiv = document.getElementById("DragDiv");
  DragDiv.onmousedown = function()
  {
   Dragging = true;
  }
 }
 
 document.onmousemove = function(e)
 {
  if (!e) e = window.event;
  if (Dragging)
  {
   DragDiv.style.left = e.clientX + "px";
   DragDiv.style.top = e.clientY + "px";
document.getElementById(id).value = e.clientY+" en "+e.clientX;
  }
 }
 
 document.onmouseup = function()
 {
  Dragging = false;
 }
</script>
<title>Simple Drag</title>
</head>

<body>
<div id="DragDiv"><script type="text/javascript">var id=1;</script>Versleep<input type="text" id="1"></div>
<div id="DragDiv"><script type="text/javascript">var id=2;</script>Versleep 2<input type="text" id="2"></div>
</body>


Dit gaat niet goed , als ik 2 ids erbij doe gaat het mis.
Hoe kan ik dit oplossen?
 
PHP hulp

PHP hulp

27/04/2024 10:17:31
 
Bas Php

Bas Php

22/08/2010 23:36:32
Quote Anchor link
iets meer uitleg zou niet ongewenst zijn
 
- Ariën  -
Beheerder

- Ariën -

22/08/2010 23:36:36
Quote Anchor link
je weet dat een ID uniek moet zijn?

En waarom gebruik je geen jQuery ervoor?
Gewijzigd op 22/08/2010 23:38:52 door - Ariën -
 
- Raoul -

- Raoul -

22/08/2010 23:38:26
Quote Anchor link
Hoe maak ik dit uniek?
 
- Ariën  -
Beheerder

- Ariën -

22/08/2010 23:39:01
Quote Anchor link
geen ID gebruiken, maar class. Een ID kan maar 1 keer bestaan.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
<div class="DragDiv"><script type="text/javascript">var id=1;</script>Versleep<input type="text" id="1"></div>
<div class="DragDiv"><script type="text/javascript">var id=2;</script>Versleep 2<input type="text" id="2"></div>

En je moet dan nog even je JS-code aanpassen dat het getElementByClass doet. (of in jQuery $.DragDiv)
Gewijzigd op 22/08/2010 23:41:10 door - Ariën -
 
- Raoul -

- Raoul -

22/08/2010 23:44:07
Quote Anchor link
@aar: dat werkt niet :S
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<html>
<head>
<style type="text/css">
 #DragDiv
 {
  height:100px;
  width:100px;
  background-color:blue;
  position:relative;
 }
</style>
<script type="text/javascript">
 var DragDiv;
 var Dragging = false;
 window.onload = function()
 {
  DragDiv = document.getElementByClass("DragDiv");
  DragDiv.onmousedown = function()
  {
   Dragging = true;
  }
 }
 
 document.onmousemove = function(e)
 {
  if (!e) e = window.event;
  if (Dragging)
  {
   DragDiv.style.left = e.clientX + "px";
   DragDiv.style.top = e.clientY + "px";
document.getElementById(id).value = e.clientY+" en "+e.clientX;
  }
 }
 
 document.onmouseup = function()
 {
  Dragging = false;
 }
</script>
<title>Simple Drag</title>
</head>

<body>
<div class="DragDiv"><script type="text/javascript">var id=1;</script>Versleep<input type="text" id="1"></div>
<div class="DragDiv"><script type="text/javascript">var id=2;</script>Versleep 2<input type="text" id="2"></div>
</body>
</html>
 
- Ariën  -
Beheerder

- Ariën -

22/08/2010 23:46:55
Quote Anchor link
En wat zegt de JS-console van Firefox?

En waarom gebruik je geen jQuery? In mijn ogen werkt het veel handiger...
Gewijzigd op 22/08/2010 23:47:34 door - Ariën -
 
- Raoul -

- Raoul -

22/08/2010 23:48:43
Quote Anchor link
Ik heb chrome.
Kan ik in jQuery een id koppelen? Ik kan nl. geen plugins aanpassen.
Kan ik ook de xy ontvangen?
 
- Ariën  -
Beheerder

- Ariën -

22/08/2010 23:51:06
Quote Anchor link
Staat vast wel in de manual.
 
- Raoul -

- Raoul -

22/08/2010 23:52:01
Quote Anchor link
Kan het gewoon niet via dat scriptje, kan ik hem al direct live zetten.
 
- Ariën  -
Beheerder

- Ariën -

23/08/2010 00:08:43
Quote Anchor link
Vast wel. Als je maar mijn vraag beantwoord over de JS-console ;-)?
 
- Raoul -

- Raoul -

23/08/2010 00:13:43
Quote Anchor link
Ik krijg de error
Uncaught TypeError: Object #<an HTMLDocument> has no method 'getElementByClass
Gewijzigd op 23/08/2010 00:15:04 door - Raoul -
 
- Ariën  -
Beheerder

- Ariën -

23/08/2010 00:14:49
Quote Anchor link
Chrome kent er ook eentje, gelukkig.
 
- Raoul -

- Raoul -

23/08/2010 00:17:46
Quote Anchor link
Ja, deze error:
Uncaught TypeError: Object #<an HTMLDocument> has no method 'getElementByClass
Gewijzigd op 24/08/2010 00:18:39 door - Raoul -
 
- Raoul -

- Raoul -

24/08/2010 00:18:24
Quote Anchor link
bump
 
Jelmer -

Jelmer -

24/08/2010 00:29:20
Quote Anchor link
Classes zijn niet uniek, dus het is getElementsByClassName. Wordt trouwens niet ondersteund door Internet Explorer.
 
- Raoul -

- Raoul -

24/08/2010 00:39:00
Quote Anchor link
Heb dit gedaan:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<html>
<head>
<style type="text/css">
 #DragDiv
 {
  height:100px;
  width:100px;
  background-color:blue;
  position:relative;
 }
</style>
<script type="text/javascript">
 var DragDiv;
 var Dragging = false;
 window.onload = function()
 {
  DragDiv = document.getElementsByClassName("DragDiv");
  DragDiv.onmousedown = function()
  {
   Dragging = true;
  }
 }
 
 document.onmousemove = function(e)
 {
  if (!e) e = window.event;
  if (Dragging)
  {
   DragDiv.style.left = e.clientX + "px";
   DragDiv.style.top = e.clientY + "px";
document.getElementById(id).value = e.clientY+" en "+e.clientX;
  }
 }
 
 document.onmouseup = function()
 {
  Dragging = false;
 }
</script>
<title>Simple Drag</title>
</head>

<body>
<div class="DragDiv"><script type="text/javascript">var id=1;</script>Versleep<input type="text" id="1"></div>
<div class="DragDiv"><script type="text/javascript">var id=2;</script>Versleep 2<input type="text" id="2"></div>
</body>
</html>


Maar werkt ook niet =S
 
Jelmer -

Jelmer -

24/08/2010 00:45:04
Quote Anchor link
Wat werkt er nog niet? Krijg je wel een foutmelding in de error console?

Bedenk je trouwens wel dat clientX en clientY de positie van de muis ten opzichte van het scherm zijn, en niet ten opzichte van de pagina. Dus als je je pagina naar beneden scrollt, kloppen de posities al niet meer. (tenzij je weer position:fixed gebruikt, wat wederom niet in Internet Explorer werkt)

Op Quirksmode staat een goed artikel hierover.
 
- Raoul -

- Raoul -

24/08/2010 00:46:38
Quote Anchor link
Wel ik kan niet slepen, als ik over de tekst ga gebeurt er niks.
Vreemd genoeg geeft de error console NIKS =S=S
Heeft er iemand een scriptje waar ik een id kan aankoppelen?
Gewijzigd op 25/08/2010 00:46:46 door - Raoul -
 
- Raoul -

- Raoul -

25/08/2010 00:47:04
Quote Anchor link
bump
 
SilverWolf NL

SilverWolf NL

25/08/2010 09:52:28
Quote Anchor link
#DragDiv => als je met classes werkt moet het dan niet .DragDiv zijn?
 

Pagina: 1 2 volgende »



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.