1

I'm trying to create my own click and drag function in JavaScript without the use of jquery. I know that jquery is easy to implement, but I prefer my own code. What I have, as i click the div, then move the mouse, the div moves to the same spot and doesn't implement a "dragging" look to it. I'm not sure why this is. I want my outcome to be able to move the div over the image that way I can "crop" the image based on the div, etc. My code is:

index.js

function _(element) {
  return document.getElementById(element);
}

index.css

body {
  background-color: rgb(33, 66, 99);
  margin: 0px;
  padding: 0px;
}

img {
  position:absolute;
}

.selection {
  width: 200px;
  height: 200px;
  background-color: rgb(255,255,255);
  position: absolute;
}

index.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "UTF-8"/>
    <title>Image Cropping</title>
    <link rel = "stylesheet" href = "index.css"/>
    <script src = "index.js"></script>
  </head>
  <body>
    <div class = "image">
      <img src = "model.jpg" alt = "Model" id = "theImage"/>
      <div class = "selection" id = "selection"/>
    </div>
    <script>
      _("theImage").ondragstart = function() { return false; };
      var m = _("selection");
      m.addEventListener("mousedown", mouseDown, false);
      window.addEventListener("mouseup", mouseUp, false);

      function mouseUp() {
        window.removeEventListener("mousemove", move, true);
      }

      function mouseDown(e) {
        window.addEventListener("mousemove", move, true);
      }

      function move(e) {
        var x = m.style.left;
        var y = m.style.top;

        var mouseX = e.clientX;
        var mouseY = e.clientY;

        m.style.top += (mouseX - x) + "px";
        m.style.left += (mouseY - y) + "px";

        // Also tried: m.style.top = (mouseX - x) + "px";
        // And       : m.style.left = (mouseY - y) + "px";
      }
    </script>
  </body>
</html>
1

1 Answer 1

2

To add the "dragging look to it", you can:

  • change the cursor (cursor: move;)
  • keep the cursor's offset relative to the mouse

For the second one, I reused a function I created for one of my projects, for which I implemented drag and drop for mobile, not wanting to use a big library:

/*
 * Returns the given element's offset relative to the document.
 */
function realOffset(elem) {
    var top = 0, left = 0;
    while (elem) {
        top = top + parseInt(elem.offsetTop, 10);
        left = left + parseInt(elem.offsetLeft, 10);
        elem = elem.offsetParent;
    }
    return { top: top, left: left };
}

Using this function, the math becomes simple:

m.style.left = (mouseX - offset.left) + "px";
m.style.top  = (mouseY - offset.top) + "px";

Full demo

_("theImage").ondragstart = function () { return false; };

var m = _("selection"), offset;
m.addEventListener("mousedown", mouseDown, false);
window.addEventListener("mouseup", mouseUp, false);

function mouseUp() { window.removeEventListener("mousemove", move, true); }

function mouseDown(e) {
    // SAVE THE OFFSET HERE
    offset = {
    	left: e.pageX - realOffset(m).left,
        top: e.pageY - realOffset(m).top
    };
    window.addEventListener("mousemove", move, true);
}

function move(e) {
    // REUSE THE OFFSET HERE
    m.style.left  = (e.pageX - offset.left) + "px";
    m.style.top = (e.pageY - offset.top) + "px";
}

/*
 * Returns the given element's offset relative to the document.
 */
function realOffset(elem) {
    var top = 0, left = 0;
    while (elem) {
        top = top + parseInt(elem.offsetTop, 10);
        left = left + parseInt(elem.offsetLeft, 10);
        elem = elem.offsetParent;
    }
    return { top: top, left: left };
}

function _(element) { return document.getElementById(element); }
body {
  background-color: rgb(33, 66, 99);
  margin: 0px;
  padding: 0px;
}

img {
  position:absolute;
}

.selection {
  width: 200px;
  height: 200px;
  background-color: rgba(255,255,255,.5);
  position: absolute;
  cursor: move;
}
<div class="image">
    <img src="http://i.imgur.com/vxkljMP.jpg" alt="Model" id="theImage" />
    <div class="selection" id="selection"></div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.