Search code examples
javascriptpopupdraggable

how to make a draggable element be able to close with a button?


I want to make a popup that's draggable (I got that part), but I want it to be able to be closed and opened. I want the button to be within the draggable element. This is what I got so far. Anything would be really helpful because I don't know what I'm doing wrong.

P.s. I can only use HTML, CSS and JavaScript, not jQuery

var mydragg = function() {
  return {
    move: function(divid, xpos, ypos) {
      divid.style.left = xpos + 'px';
      divid.style.top = ypos + 'px';
    },
    startMoving: function(divid, container, evt) {
      evt = evt || window.event;
      var posX = evt.clientX,
        posY = evt.clientY,
        divTop = divid.style.top,
        divLeft = divid.style.left,
        eWi = parseInt(divid.style.width),
        eHe = parseInt(divid.style.height),
        cWi = parseInt(document.getElementById(container).style.width),
        cHe = parseInt(document.getElementById(container).style.height);
      document.getElementById(container).style.cursor = 'move';
      divTop = divTop.replace('px', '');
      divLeft = divLeft.replace('px', '');
      var diffX = posX - divLeft,
        diffY = posY - divTop;
      document.onmousemove = function(evt) {
        evt = evt || window.event;
        var posX = evt.clientX,
          posY = evt.clientY,
          aX = posX - diffX,
          aY = posY - diffY;
        if (aX < 0) aX = 0;
        if (aY < 0) aY = 0;
        if (aX + eWi > cWi) aX = cWi - eWi;
        if (aY + eHe > cHe) aY = cHe - eHe;
        mydragg.move(divid, aX, aY);
      }
    },
    stopMoving: function(container) {
      var a = document.createElement('script');
      document.getElementById(container).style.cursor = 'default';
      document.onmousemove = function() {}
    },
  }
}();

function close() {
document.getElementById('elem').style.display = 'none';
    }
.container {
  position: absolute;
  background-color: blue;
}

.elem {
  position:absolute;
  background-color: green;
  -webkit-user-select: none;
  -moz-user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
  -khtml-user-select: none;
  user-select: none;
}
<div class="container" id='container' style="width: 600px;height: 400px;top:50px;left:50px;">
  <div class="elem" id="elem" onmousedown='mydragg.startMoving(this,"container",event);' onmouseup='mydragg.stopMoving("container");' style="width: 200px;height: 100px;">    
    <button onClick="close()"> x </button>
  </div>
</div>


Solution

  • The close keyword is reserved that why the function is not called. You need to change the function name.

    var mydragg = function() {
      return {
        move: function(divid, xpos, ypos) {
          divid.style.left = xpos + 'px';
          divid.style.top = ypos + 'px';
        },
        startMoving: function(divid, container, evt) {
          evt = evt || window.event;
          var posX = evt.clientX,
            posY = evt.clientY,
            divTop = divid.style.top,
            divLeft = divid.style.left,
            eWi = parseInt(divid.style.width),
            eHe = parseInt(divid.style.height),
            cWi = parseInt(document.getElementById(container).style.width),
            cHe = parseInt(document.getElementById(container).style.height);
          document.getElementById(container).style.cursor = 'move';
          divTop = divTop.replace('px', '');
          divLeft = divLeft.replace('px', '');
          var diffX = posX - divLeft,
            diffY = posY - divTop;
          document.onmousemove = function(evt) {
            evt = evt || window.event;
            var posX = evt.clientX,
              posY = evt.clientY,
              aX = posX - diffX,
              aY = posY - diffY;
            if (aX < 0) aX = 0;
            if (aY < 0) aY = 0;
            if (aX + eWi > cWi) aX = cWi - eWi;
            if (aY + eHe > cHe) aY = cHe - eHe;
            mydragg.move(divid, aX, aY);
          }
        },
        stopMoving: function(container) {
          var a = document.createElement('script');
          document.getElementById(container).style.cursor = 'default';
          document.onmousemove = function() {}
        },
      }
    }();
    
    function closePopup() {
    document.getElementById('elem').style.display = 'none';
    }
    
    function openPopup() {
    document.getElementById('elem').style.display = 'block';
    }
    .container {
      position: absolute;
      background-color: blue;
    }
    
    .elem {
      position:absolute;
      background-color: green;
      -webkit-user-select: none;
      -moz-user-select: none;
      -o-user-select: none;
      -ms-user-select: none;
      -khtml-user-select: none;
      user-select: none;
    }
    <div class="container" id='container' style="width: 600px;height: 400px;top:50px;left:50px;">
      <div class="elem" id="elem" onmousedown='mydragg.startMoving(this,"container",event);' onmouseup='mydragg.stopMoving("container");' style="width: 200px;height: 100px;">    
        <button onClick="closePopup()"> x </button>
      </div>
    </div>
    <button onclick="openPopup()">Open Popup</button>