gpt4 book ai didi

javascript - 使用 Interact.js 进入 Dropzone

转载 作者:行者123 更新时间:2023-11-30 12:33:36 25 4
gpt4 key购买 nike

我是 interactjs 的新手,如果它在 dropzone 内被捕捉,我正试图让星星落在树的顶部(参见 jsfiddle)。我该怎么做?

Javascript:

interact('.draggable')
.draggable({
onmove: function (event) {
var target = event.target,
x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;

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

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
},
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');
}
})
.inertia(true)
.restrict({
drag: "parent",
endOnly: true,
elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
});



// enable draggables to be dropped into this
interact('.tree').dropzone({
// only accept elements matching this CSS selector
accept: '#star',
// 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');
$('.tree:not(.drop-target)').find('img').animate({
opacity: .5,
height: "160px"
});

},
ondragleave: function (event) {
var draggableElement = event.relatedTarget,
dropzoneElement = event.target;
// remove the drop feedback style
event.target.classList.remove('drop-target');
event.relatedTarget.classList.remove('can-drop');
$('.tree:not(.drop-target)').find('img').animate({
opacity: 1,
height: "186px"
});
},
ondrop: function (event) {
//Dropped event
},
ondropdeactivate: function (event) {
// remove active dropzone feedback
event.target.classList.remove('drop-active');
event.target.classList.remove('drop-target');
}
})
.snap({
mode: 'anchor',
grid: { x: 20, y: 20 },
range: Infinity
});

图书馆: http://interactjs.io/

fiddle : http://jsfiddle.net/hpq7rpnh/2/

最佳答案

以上不适用于更新后的 API。我 fork 了 Taye(interact.js 的作者)的一支笔并让它工作。

在此处查看可能的解决方案:https://codepen.io/eljefedelrodeodeljefe/pen/vybegM

如果我没看错,需要在 dragenter 上设置目标,无论如何,新 API 的解决方案在我看来,从集成商 POV 来看要冗长得多。

event.draggable.draggable({
snap: {
targets: [dropCenter]
}
});

关于javascript - 使用 Interact.js 进入 Dropzone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26840497/

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