简体   繁体   English

如何使用 Javascript 取消 HTML5 拖(放)操作?

[英]How to cancel an HTML5 drag (and drop) operation using Javascript?

I want to cancel an HTML5 drag operation based on some condition.我想根据某些条件取消 HTML5 拖动操作。 I have tried to search this but found nothing useful.我试图搜索这个,但没有发现任何有用的东西。 It seems that there is no way to cancel an HTML5 drag and drop using JavaScript.似乎无法使用 JavaScript 取消 HTML5 拖放。 return ing false in the dragstart doesn't seem to do anything.dragstart return ing false似乎没有做任何事情。 Any ideas?有任何想法吗?

You can cancel it by calling event.preventDefault() in the event handler.您可以通过在事件处理程序中调用event.preventDefault()来取消它。 For example this should work:例如,这应该有效:

<p id="p1" draggable="true" ondragstart="dragstart_handler(event);">This element is draggable.</p>

<script>
var enableDragDrop = true;
function dragstart_handler(ev) {
    if (!enableDragDrop) {
        ev.preventDefault();
    }
    console.log("dragStart");
}
</script>

I think you need to do something tricky to make it work, for I do not know how to stop a drag from js, but you can fake a drag with mousedown , mousedown , mouseup and a isDragging flag我认为您需要做一些棘手的事情才能使其工作,因为我不知道如何停止来自 js 的拖动,但是您可以使用mousedownmousedownmouseupisDragging标志来伪造拖动

some code look like:一些代码看起来像:

 var isDragging = false var fakeDraggingElement var currentDragging $('#item').mousedown(function(e){ e.preventDefault() isDragging = true currentDragging = this //change your cursor and make it loos like dragging $('#parent').css({cursor: 'move'}) //fake dragging element fakeDraggingElement = $(this).clone().css({ opacity:0.5, position:'absolute', top:e.pageY+offsetY, //parent offset left:e.pageX+offsetX,//parent offset }).appendTo(parent) }).mousemove(function(e){ if(!isDragging){ return } fakeDraggingElement.css({ top:e.pageY+offsetY, //parent offset left:e.pageX+offsetX,//parent offset }) }).mousedown(function(e){ if(!isDragging){ return } cancleDrag() //your old drop ondrop(currentDragging,e.target) }) function cancleDrag(){ isDragging = false $(currentDragging).remove() currentDragging = undefined }

EDIT: got it.编辑:明白了。 Its a bit roundabout, but works.它有点迂回,但有效。 Take advantage of the fact that the drag event is a cancellable mouse event, and cancelling it stops all further drag actions.利用拖动事件是可取消的鼠标事件这一事实,取消它会停止所有进一步的拖动操作。 Create a drag event on your condition and call preventDefault() .根据您的条件创建拖动事件并调用preventDefault() The following cancels it after 3 seconds:以下在 3 秒后取消它:

setTimeout(function(){document.getElementById("drag").ondrag = function(e){
    e.preventDefault();
    this.ondrag = null;
    return false;
};},3000);

Note first that I clear all ondrag events after it is cancelled, which you may need to adjust if you need other ondrag events, and second that this truly cancels all parts of the drag, so you don't get an ondragend event after, as shown in this jsfiddle: http://jsfiddle.net/MaxPRafferty/aCkbR/首先请注意,我在取消后清除了所有ondrag事件,如果您需要其他ondrag事件,您可能需要对其进行调整,其次,这确实取消了拖动的所有部分,因此您不会在之后收到ondragend事件,如在这个jsfiddle中显示: http : //jsfiddle.net/MaxPRafferty/aCkbR/

Source: HTML5 Spec: http://www.w3.org/html/wg/drafts/html/master/editing.html#dndevents来源:HTML5 规范: http : //www.w3.org/html/wg/drafts/html/master/editing.html#dndevents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM