interact.js icon indicating copy to clipboard operation
interact.js copied to clipboard

cannot drag and drop div using your code in interact.io

Open mjrlgue opened this issue 9 years ago • 4 comments

Hi, I'am new to interact.js and I was manipulating it to use it in a school project, I don't know why it does not work for me: here is my codes: home.html

<html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="css/home.css">
</head>
<body>

	<div id="drag-1" class="draggable">
	  <p> You can drag one element </p>
	</div>
	<div id="drag-2" class="draggable">
	    <p> with each pointer </p>
	</div>
<script src="/javascript/dragndrop.js"></script>
<script src="/javascript/interact.js"></script>

</body>
</html>

css:

#drag-1, #drag-2 {
  width: 10%;
  height: 20%;
  min-height: 6.5em;
  margin: 10%;

  background-color: #29e;
  color: white;

  border-radius: 0.75em;
  padding: 4%;

  -webkit-transform: translate(0px, 0px);
          transform: translate(0px, 0px);
}

#drag-me::before {
  content: "#" attr(id);
  font-weight: bold;
}

finally dragndrop.js:


// target elements with the "draggable" class
interact('.draggable').draggable({
    // enable inertial throwing
    inertia: true,
    // keep the element within the area of it's parent
    restrict: {
      restriction: "parent",
      endOnly: true,
      elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
    },
    // enable autoScroll
    autoScroll: true,

    // call this function on every dragmove event
    onmove: dragMoveListener,
    // call this function on every dragend event
    onend: function (event) {
      var textEl = event.target.querySelector('p');

      textEl && (textEl.textContent =
        'moved a distance of '
        + (Math.sqrt(event.dx * event.dx +
                     event.dy * event.dy)|0) + 'px');
    }
  });

  function dragMoveListener (event) {
    var target = event.target,
        // keep the dragged position in the data-x/data-y attributes
        x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
        y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;

    // translate the element
    target.style.webkitTransform =
    target.style.transform =
      'translate(' + x + 'px, ' + y + 'px)';

    // update the posiion attributes
    target.setAttribute('data-x', x);
    target.setAttribute('data-y', y);
  }

  // this is used later in the resizing and gesture demos
  window.dragMoveListener = dragMoveListener;

should I add in drandrop.js: $( document ).ready(function() {.... ??

mjrlgue avatar Jan 03 '17 19:01 mjrlgue

and the cursor is not changing on re-size edges !!!

mjrlgue avatar Jan 03 '17 19:01 mjrlgue

If you haven't solved this issue yet, can you make a JSFiddle demo?

taye avatar Jan 27 '17 15:01 taye

Add this line: $( document ).ready(function() {....});

and it works just fine: Here is a fiddle

NewInGit avatar Jan 28 '17 13:01 NewInGit

if your issues has not yet been solved then put this in import section of your code;

import interact = require("interactjs/index"); declare var $: any;

once you do that add your following code under this statement //put it inside ngOnInit $('document').ready(function () { //put your interactio code here }

dilagurung avatar Apr 09 '18 11:04 dilagurung