0

I got most of this code from W3Schools, which makes it even stranger why it isnt working.

<!DOCTYPE html>
<head>
<script src="drag.js"></script>
<style>
body {
  overflow: hidden;
}
#mydiv {
  position: absolute;
  z-index: 9;
  background-color: #f1f1f1;
  text-align: center;
  border: 1px solid #d3d3d3;
}

#mydivheader {
  padding: 10px;
  cursor: move;
  z-index: 10;
  background-color: #2196F3;
  color: #fff;
}
</style>
<div id="mydiv">
  <div id="mydivheader">Click here to move</div>
  <p>Lorem Ipsum</p>
</div>

I think it might be something else in the HTML thats stopping it from working for some reason. I have tried moving it out of my container div, but it still gives the error in the title.

In "drag.js" I have this code which is an exact paste from W3Schools

dragElement(document.getElementById("mydiv"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").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;
  }
}
  • "I got most of this code from W3Schools, which makes it even stranger why it isnt working." Let's just say that W3Schools is... not that great. – Thomas Aug 28 '20 at 09:45
  • Close your tag. It works: https://jsfiddle.net/7q5s9unc/ – NBlack Aug 28 '20 at 09:50
  • Just include your script before `

    ` and it will work... and let me add that W3Schools helped so many people.

    – skobaljic Aug 28 '20 at 09:50
  • I tried turning your code into an HTML/CSS/JS snippet (there's a button for that, try editing your post) and it works fine. – Thomas Aug 28 '20 at 09:50
  • @NBlack — The end tag for the head element is optional, that won't help. – Quentin Aug 28 '20 at 09:54
  • @Thomas — Since the problem, as described by the duplicate question, is the position of the ` – Quentin Aug 28 '20 at 09:55
  • @Quentin if you not close tag how – NBlack Aug 28 '20 at 10:00
  • 1
    @NBlack — The start of the body element implicitly ends the head element. – Quentin Aug 28 '20 at 10:02

0 Answers0