admin管理员组文章数量:1316016
I am working on improving a drag and drop implementation by creating an indicator (a silhouette of the draged element) that moves along a set flex grid as you drag and move through the grid. In the current implementation the dragged element in place and when I drop on the drop target it updates the flex order. What I am looking to do is hide the element pletely to give a better visual cue as to where the element will be placed. The problem is if I hide the element on drag start, the drag workflow ends abruptly, not even triggering the subsequent drag operations.
I am working on improving a drag and drop implementation by creating an indicator (a silhouette of the draged element) that moves along a set flex grid as you drag and move through the grid. In the current implementation the dragged element in place and when I drop on the drop target it updates the flex order. What I am looking to do is hide the element pletely to give a better visual cue as to where the element will be placed. The problem is if I hide the element on drag start, the drag workflow ends abruptly, not even triggering the subsequent drag operations.
Share Improve this question asked Jan 28, 2016 at 17:11 indigo0086indigo0086 4316 silver badges13 bronze badges1 Answer
Reset to default 9If I understand your question correctly you want to hide the element being dragged during the drag operation. This can be acplished by hiding the element when the dragover event is fired. Here is an example:
JSFiddle (Drag the orange box)
Javascript:
;
(function($, undefined) {
var dragging;
$(function() {
$('div.flex-grid').on({
'dragstart': dragstart,
'dragend': dragend
}, 'div.drag').on({
'dragover dragenter': dragover,
'dragleave': dragleave,
'drop': drop
}, 'div.drop');
});
function dragstart(e) {
e.stopPropagation();
var dt = e.originalEvent.dataTransfer;
if (dt) {
dt.effectAllowed = 'move';
dt.setData('text/html', '');
dragging = $(this);
}
}
function dragover(e) {
e.stopPropagation();
e.preventDefault();
var dt = e.originalEvent.dataTransfer;
if (dt && dragging) {
dt.dropEffect = 'move';
$(this).css({
'background-color': 'yellow'
});
dragging.hide();
}
return false;
}
function dragleave(e) {
e.stopPropagation();
$(this).css({
'background-color': '#fff'
});
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
if (dragging) {
var dropzone = $(this);
dragging.data('dropzone', dropzone);
dragging.trigger('dragend');
}
return false;
}
function dragend(e) {
if (dragging) {
var dropzone = dragging.data('dropzone');
if (dropzone) {
dropzone.append(dragging);
}
dragging.show();
}
$('div.drop').css({
'background-color': '#fff'
});
dragging = undefined;
}
}(jQuery));
HTML:
<div class="flex-grid">
<div class="flex-row">
<div class="drop">
<div class="drag" draggable="true"></div>
</div>
<div class="drop"></div>
<div class="drop"></div>
</div>
<div class="flex-row">
<div class="drop"></div>
<div class="drop"></div>
<div class="drop"></div>
</div>
<div class="flex-row">
<div class="drop"></div>
<div class="drop"></div>
<div class="drop"></div>
</div>
</div>
CSS:
* {
border-style: none;
padding: 0;
margin: 0;
}
.flex-grid {
display: flex;
display: -webkit-flex;
flex-direction: column;
-webkit-flex-direction: column;
}
.flex-row {
display: flex;
display: -webkit-flex;
flex-direction: row;
-webkit-flex-direction: row;
}
.drop,
.drag {
display: block;
width: 100px;
height: 100px;
}
.drag {
background-color: orange;
cursor: move;
}
.drop {
border: 1px solid #ccc;
background-color: #fff;
}
本文标签:
版权声明:本文标题:javascript - Using HTML5 Drag & Drop, is there a way to hide the element from view while keeping the drag flow - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741979050a2408290.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论