Maus Fugue
Maus Fugue

Reputation: 11

How to change the initial position of draggable pictures?

I found this code here https://www.w3schools.com/code/tryit.asp?filename=G75VCGLH4SCA

Only the position of picture #one and picture #two is working. How can I get the others to work as well?

//Make the DIV element draggagle:
var offset = 5;
var mydivs = document.getElementsByClassName("mydiv");
for (var i = 0; i < mydivs.length; i++) {
  dragElement(mydivs[i]);
  mydivs[i].style.left = offset + "px";
  offset = offset + mydivs[i].offsetWidth + 5;
}

function dragElement(elmnt) {
  var pos1 = 0,
    pos2 = 0,
    pos3 = 0,
    pos4 = 0;
  if (elmnt.getElementsByClassName("mydivheader")[0]) {
    /* if present, the header is where you move the DIV from:*/
    elmnt.getElementsByClassName(
      "mydivheader"
    )[0].onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = elmnt.offsetTop - pos2 + "px";
    elmnt.style.left = elmnt.offsetLeft - pos1 + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
.mydiv {
  position: absolute;
  z-index: 9;
}

.mydivheader {
  padding: 10px;
  cursor: move;
  z-index: 10;
}

#one {
  position: absolute;
  top: 300px;
  left: 1000px;
}

#two {
  position: absolute;
  top: 500px;
  left: 400px;
}

#three {
  top: 459 px;
  left: 100 px;
}

#four {
  position: absolute;
  top: 25 px;
  left: 897 px;
}

#five {
  position: absolute;
  top: 25 px;
  left: 174 px;
}
<body>

  <div class="mydiv">
    <div class="mydivheader" id="one">
      <img src="Screenshot 2021.png" alt="X" width="500" height="333"></div>
  </div>
  <div class="mydiv">
    <div class="mydivheader" id="two">
      <img src="connections5.jpg" alt="Y" width="500" height="333"></div>
  </div>
  <div class="mydiv">
    <div class="mydivheader" id="three">
      <img src="untitled6.png" alt="W" width="500" height="333"></div>
  </div>
  <div class="mydiv">
    <div class="mydivheader" id="four">
      <img src="ViewCapture20210416_184235.jpg" alt="Z" width="500" height="333"></div>
  </div>
  <div class="mydiv">
    <div class="mydivheader" id="five">
      <img src="01_elevation_south.jpg" alt="V" width="500" height="333"></div>
  </div>
</body>

Upvotes: 1

Views: 38

Answers (1)

&#201;tienne Miret
&#201;tienne Miret

Reputation: 6650

A CSS length may not contain any space:

The format of a length value (denoted by in this specification) is a <number> (with or without a decimal point) immediately followed by a unit identifier (e.g., px, em, etc.). After a zero length, the unit identifier is optional.

(emphasis is mine)

So you need to change your last three CSS blocks to:

#three {
  top: 459px;
  left: 100px;
}

#four {
  position: absolute;
  top: 25px;
  left: 897px;
}

#five {
  position: absolute;
  top: 25px;
  left: 174px;
}

Upvotes: 1

Related Questions