gpt4 book ai didi

javascript - Vue.Draggable (sortable.js) 中的 ondragover 等价物

转载 作者:行者123 更新时间:2023-11-29 17:38:09 29 4
gpt4 key购买 nike

有没有办法检测是否有东西被拖过元素?或者触发悬停事件?找到了一些有关通过 onMove 向拖过的元素添加类的信息,但它似乎对我不起作用。

最佳答案

我用解决方案制作了一个 JSBin:https://jsbin.com/xuwocis/edit?html,js,output

var sorting = false;

new Sortable(el, {
onStart: function() {
sorting = true;
},
onEnd: function() {
sorting = false;
// remove styling
targetElement.style.backgroundColor = '';
},
// forceFallback:true
});

// For native drag&drop
targetElement.addEventListener('dragover', function(evt) {
evt.preventDefault();
});

targetElement.addEventListener('dragenter', function(evt) {
if (sorting && !targetElement.contains(evt.relatedTarget)) {
// Here is where you add the styling of targetElement
targetElement.style.backgroundColor = 'red';
}
});

targetElement.addEventListener('dragleave', function(evt) {
if (sorting && !targetElement.contains(evt.relatedTarget)) {
// Here is where you remove the styling of targetElement
targetElement.style.backgroundColor = '';
}
});


// For fallback
targetElement.addEventListener('mouseenter', function(evt) {
if (sorting) {
// Here is where you change the styling of targetElement
targetElement.style.backgroundColor = 'red';
}
});

targetElement.addEventListener('mouseleave', function(evt) {
if (sorting) {
// Here is where you remove the styling of targetElement
targetElement.style.backgroundColor = '';
}
});

el.addEventListener('touchmove', function(evt) {
if (!sorting) { return; }
var x = evt.touches[0].clientX;
var y = evt.touches[0].clientY;
var elementAtTouchPoint = document.elementFromPoint(x, y);
if (elementAtTouchPoint === targetElement ||
// In case of a ghost element, the element at touch point
// is the ghost element and thus we need to check if the parent
// of the ghost element is the targetElement.
elementAtTouchPoint.parentNode === targetElement) {
targetElement.style.backgroundColor = 'red';
} else {
// Here is where you remove the styling of targetElement
targetElement.style.backgroundColor = '';
}
});

基本上,如果使用 SortableJS 进行排序,您会为回退执行 mouseenter 和 mouseleave 事件,并为本地拖放执行 dragenter 和 dragleave 事件(忽略气泡)。如果您没有 forceFallback: true,您将需要两者。

关于javascript - Vue.Draggable (sortable.js) 中的 ondragover 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54917268/

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