gpt4 book ai didi

javascript - 如何限制将可拖动元素拖放到预定义的 div 元素中?

转载 作者:行者123 更新时间:2023-12-05 00:29:57 26 4
gpt4 key购买 nike

我正在使用 interact.js我想在预定义的 div .dropzone 中启用可拖放对象, 从而落在 .dropzone 之外将被禁用。我介绍了restriction: ".dropzone", drag: document.getElementById('dropzone'), ,但是它不能正常工作。

我创建了 this JFIDDLE更清楚(我更新了链接,因为旧链接不正确)。

JavaScript:

// target elements with the "draggable" class
interact('.draggable').draggable({
inertia: true,
restrict: {
restriction: ".dropzone",
drag: document.getElementById('dropzone'),
endOnly: true,
elementRect: {
top: 0,
left: 0,
bottom: 1,
right: 1
}
},
autoScroll: true,
onmove: function(event) {
var target = event.target;
var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;

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

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
},
onend: function(event) {
console.log(event);
}
})
.on('move', function(event) {
var interaction = event.interaction;
if (interaction.pointerIsDown && !interaction.interacting() && event.currentTarget.getAttribute('clonable') != 'false') {
var original = event.currentTarget;
var clone = event.currentTarget.cloneNode(true);
var x = clone.offsetLeft;
var y = clone.offsetTop;
clone.setAttribute('clonable', 'false');
clone.style.position = "absolute";
clone.style.left = original.offsetLeft + "px";
clone.style.top = original.offsetTop + "px";
original.parentElement.appendChild(clone);
interaction.start({
name: 'drag'
}, event.interactable, clone);
}
})
.resizable({
edges: {
left: true,
right: true,
bottom: true,
top: true
}
})
.on('resizemove', function(event) {
var target = event.target;
x = (parseFloat(target.getAttribute('data-x')) || 0),
y = (parseFloat(target.getAttribute('data-y')) || 0);

// update the element's style
target.style.width = event.rect.width + 'px';
target.style.height = event.rect.height + 'px';

// translate when resizing from top or left edges
x += event.deltaRect.left;
y += event.deltaRect.top;

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

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
//target.textContent = event.rect.width + '×' + event.rect.height;
});

// enable draggables to be dropped into this
interact('.dropzone').dropzone({
// Require a 50% element overlap for a drop to be possible
overlap: 0.50,

// 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');
},
ondragleave: function(event) {
// remove the drop feedback style
event.target.classList.remove('drop-target');
},
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');
}
});

$(".dropzone").html("<img src='https://s-media-cache-ak0.pinimg.com/originals/fb/d5/55/fbd5556e0e364b31166bebfce433c14e.jpg'>");


function popupBaseDetails(div, e) {
e.preventDefault();
div.querySelector(".basepopup").classList.toggle("show");
}

CSS:
.dropzone {
background-color: #e9ebed;
padding: 10px;
width: 100%;
height: 600px;
overflow-y: scroll;
border: dashed 4px transparent;
float: left;
}

.drop-active {
border-color: #aaa;
}

.drop-target {
background-color: #3f5265;
color: #FFF;
border-color: #fff;
border-style: solid;
}

HTML:
<div style="height:280px">
<div id="dropzone" class="dropzone"></div>
</div>

最佳答案

您可以使用限制修饰符将可拖动元素限制到放置区:

interact('.draggable').draggable({
inertia: true,
modifiers: [
interact.modifiers.restrictRect({
restriction: document.getElementById('dropzone'),
endOnly: true
})
],
listeners: {
start(event) {
var interaction = event.interaction;
if (event.currentTarget.getAttribute('clonable') != 'false') {
var original = event.currentTarget;
var clone = event.currentTarget.cloneNode(true);
var x = clone.offsetLeft;
var y = clone.offsetTop;
clone.setAttribute('clonable', 'false');
clone.style.position = "absolute";
clone.style.left = original.offsetLeft + "px";
clone.style.top = original.offsetTop + "px";
original.parentElement.appendChild(clone);
interaction.start({
name: 'drag'
}, event.interactable, clone);
}
},
move(event) {
var target = event.target;
var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;

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

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
}
},
autoScroll: true
});

关于javascript - 如何限制将可拖动元素拖放到预定义的 div 元素中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283222/

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