Drag and drop using basic jQuery framework

别等时光非礼了梦想. 提交于 2019-12-07 04:22:00

问题


Is it possible to achieve drag and drop using basic jQuery script (without using jQuery UI)?


回答1:


In HTML5, there's an attribute called "draggable".

<div draggable='true'>Drag me</div>
<div id='dropzone'>Drop it here!</div>

You could make use of the following events:

var onDragEnter = function(event) {
    event.preventDefault();
    $("#dropzone").addClass("dragover");
}, 

onDragOver = function(event) {
    event.preventDefault(); 
    if(!$("#dropzone").hasClass("dragover"))
        $("#dropzone").addClass("dragover");
}, 

onDragLeave = function(event) {
    event.preventDefault();
    $("#dropzone").removeClass("dragover");
},

onDrop = function(event) {
    event.preventDefault();
    $("#dropzone").removeClass("dragover");
    console.log(event.originalEvent.dataTransfer.files);
};

$("#dropzone")
.on("dragenter", onDragEnter)
.on("dragover", onDragOver)
.on("dragleave", onDragLeave)
.on("drop", onDrop);

Hope that helps for the moment.



来源:https://stackoverflow.com/questions/9581846/drag-and-drop-using-basic-jquery-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!