gpt4 book ai didi

javascript - 使用 InteractJS 库没有行为

转载 作者:行者123 更新时间:2023-12-03 09:01:18 26 4
gpt4 key购买 nike

我在尝试使用该库作为 InteractJS 时遇到问题.

现在my code正如您所看到的,正在调用成功完成示例所需的文件:

<link href = "css/drag-drop.css" rel = "stylesheet">

<script src = "js/drag-drop.js"> </ script>

但是,行为与我预期的不同,因为元素出现在页面上,但我没有与它们交互。

HTML代码如页面所示:

    

<div id = "no-drop" class = "drag-drop draggable"> # no-drop </ div>
    
<div id = "yes-drop" class = "drag-drop draggable"> # yes-drop </ div>
    
<div id = "outer-dropzone" class = "dropzone">

# outer-dropzone
<div id = "inner-dropzone" class = "dropzone"> # inner-dropzone </ div>
    
</ div>

我不明白我做错了什么。

最佳答案

您还需要添加相关的 javascript 来执行您需要的操作。就您而言,由于您是从示例之一开始:

// 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 }
},

// 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 demo
window.dragMoveListener = dragMoveListener;


// enable draggables to be dropped into this
interact('.dropzone').dropzone({
// only accept elements matching this CSS selector
accept: '#yes-drop',
// Require a 75% element overlap for a drop to be possible
overlap: 0.75,

// listen for drop related events:

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

// feedback the possibility of a drop
dropzoneElement.classList.add('drop-target');
draggableElement.classList.add('can-drop');
draggableElement.textContent = 'Dragged in';
},
ondragleave: function (event) {
// remove the drop feedback style
event.target.classList.remove('drop-target');
event.relatedTarget.classList.remove('can-drop');
event.relatedTarget.textContent = 'Dragged out';
},
ondrop: function (event) {
event.relatedTarget.textContent = 'Dropped';
},
ondropdeactivate: function (event) {
// remove active dropzone feedback
event.target.classList.remove('drop-active');
event.target.classList.remove('drop-target');
}
});

关于javascript - 使用 InteractJS 库没有行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com