When I want to make a div draggable, I couldn't avoid selecting the adjacent text

I am starting JS.
I have made draggable <div>

. When I drag and drop it, it will select one or two words next to <div>

or glint with a blue select box (see code snippet below).

It bothers the sensation of sensation a little. Most of all I want other normal words to be selected when dragging dhe <div>

.

Please help me optimize the code or tell me if there is a syntax error.

window.onload = function() {
  var parent = document.getElementById('parent');
  eventFunc.addEventListener(parent, 'mousedown', dragItem);
}

function dragItem(ev) {
  ev = ev || window.event;
  var element = eventFunc.target(ev);
  var spaceX = ev.clientX - element.offsetLeft,
    spaceY = ev.clientY - element.offsetTop,
    maxX = (document.documentElement.clientWidth || document.body.clientWidth) - element.offsetWidth,
    maxY = (document.documentElement.clientHeight || document.body.clientHeight) - element.offsetHeight;
  document.onmousemove = function(ev) {
    ev = ev || window.event;
    eventFunc.stopPropagation(ev);
    element.style.left = (ev.clientX - spaceX) + 'px';
    element.style.top = (ev.clientY - spaceY) + 'px';
    if (element.offsetLeft < 0) {
      element.style.left = 0;
    } else if (element.offsetLeft > maxX) {
      element.style.left = maxX + 'px'
    }
    if (element.offsetTop < 0) {
      element.style.top = 0;
    } else if (element.offsetTop > maxY) {
      element.style.top = maxY + 'px'
    }
  }
  document.onmouseup = function() {
    document.onmousemove = null;
    document.onmouseup = null;
  }
}
var eventFunc = {
  addEventListener: function(element, type, func) {
    if (addEventListener) {
      element.addEventListener(type, func, false);
    } else if (attachEvent) {
      element.attachEvent('on' + type, func);
    } else {
      element['on' + type] = func;
    }
  },
  target: function(ev) {
    return ev.target || ev.srcElement;
  },
  stopPropagation: function(ev) {
    if (ev.stopPropagation) {
      ev.stopPropagation();
    } else {
      ev.cancelBubble = true;
    }
  },
  preventDefault: function(ev) {
    if (ev.preventDefault) {
      ev.preventDefault();
    } else {
      ev.returnValue = false;
    }
  }
}
      

#parent {
  width: 400px;
  height: 300px;
  position: absolute;
  border: 3px solid #ccc;
  top: 0;
  left: 0;
  cursor: all-scroll;
  text-align: center;
  font: bold 35px/35px'Segoe UI', arial, helvetica, sans-serif;
  color: #ccc;
}
#children {
  text-align: left;
  color: initial;
  font: initial;
  cursor: auto;
  margin: 1em;
  border: 1px solid red;
}
      

Outside Words
<div id="parent">
  <div id="children">I'm The Children Element. I'm The Children Element. I'm The Children Element. I'm The Children Element. I'm The Children Element. I'm The Children Element. I'm The Children Element. I'm The Children Element. I'm The Children Element.</div>
  DRAG ME
</div>
</body>
      

Run codeHide result


+3


source to share


1 answer


inside your function dragItem

.... add the following line ...

function dragItem(ev) {
   ev.preventDefault()
}

      

[edit (doing the rest of the question)]

To select the content of any piece of content ... you can use this function ...



function selectNode(el) {
  var range = document.createRange()
  range.selectNodeContents(el)
  var sel = window.getSelection()
  sel.removeAllRanges()
  sel.addRange(range)
}

      

// use it by passing node

selectNode(document.getElementById('parent'))

      

0


source







All Articles