无法得到interact.js拖动;删除与Bootstrap元素一起使用的交互

时间:2015-02-25 05:58:27

标签: javascript jquery twitter-bootstrap drag-and-drop interact.js

我正试图让interact.js使用Bootstrap列表项。我有一个非常简单的设置,但不幸的是我无法让它工作。

HTML(div包含两个Bootstrap面板 - 一个带有一些可拖动的列表项,另一个带有两个dropzones):

<div id="visualizer-panel">
    <div class="leftStuff panel">
        <div class="panel-heading">
            <h4 class="panel-title">Draggables</h4>
        </div>
        <div class="panel-body">
            <ul class="list-group">
                <li class="list-group-item draggable">asdf</li>
                <li class="list-group-item draggable">bbq</li>
            </ul>
        </div>
    </div>
    <div class="rightStuff panel">
        <div class="panel-heading">
            <h4 class="panel-title">Drop Areas</h4>
        </div>
        <div class="panel-body">
            <div class="dropzone">drop stuff here</div>
            <div class="dropzone">drop more stuff here</div>
        </div>
    </div>
</div>

CSS:

#visualizer-panel .leftStuff {
    float: left;
}
#visualizer-panel .rightStuff {
    float: right;
}

/* Visual cursor hint for draggable */
#visualizer-panel .draggable:hover {
    cursor: move;
}

/* Drag & Drop */
#visualizer-panel .draggable {
    -webkit-transform: translate(0px, 0px);
    transform: translate(0px, 0px);
}
#visualizer-panel .drop-active { border-color: red; }
#visualizer-panel .drag-drop.can-drop { background-color: #4e4; }
#visualizer-panel .drop-target {  background-color: #29e;  }

和Javascript:

// Initialize Interact.js Drag & Drop
interact('.draggable').draggable({
    inertia: true,
    restrict: {
      restriction: "#visualizer-panel",
      endOnly: true,
      elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
    },
    onend: function(event) {
        console.log(event);
    }
});

interact('.dropzone').dropzone({
    accept: '.draggable',
    overlap: 0.01,

    // add / remove dropzone feedback
    ondropactivate: function (event) {
        event.target.classList.add('drop-active');
    },
    ondropdeactivate: function (event) {
        event.target.classList.remove('drop-active');
    },

    // add / remove dropzone feedback
    ondragenter: function (event) {
        var draggableElement = event.relatedTarget,
            dropzoneElement = event.target;
        dropzoneElement.classList.add('drop-target');
        draggableElement.classList.add('can-drop');
    },
    ondragleave: function (event) {
        event.target.classList.remove('drop-target');
        event.relatedTarget.classList.remove('can-drop');
    },

    // drop successful
    ondrop: function (event) {
        console.log(event);
    }
});

以下是上述代码的jsfiddle链接:http://jsfiddle.net/hf27hn0c/6/

正如你所看到的,不可能拖拽和将列表项放入dropzones。我知道我可能不得不在拖动开始时手动从父列表中删除项目,但是现在我甚至无法获得各种dragenterdragleave或关键{{1事件正常工作。

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:3)

interact('.dropzone').dropzone({
    accept: '.draggable',
    overlap: 0.01,
    ...

您需要在dropzone上重叠1%的draggable才有资格获得下降。因为没有移动拖拽,所以不会发生重叠。

解决方案是添加dragmove以移动可拖动或将dropzone的重叠更改为'pointer'http://jsfiddle.net/hf27hn0c/7/