admin管理员组文章数量:1404773
How to change DnD's (drag and drop) icon during dragover or dragenter? Is it even possible?
I am able to change the icon during dragstart if the source of the drop is inside the html page (for example drag a div in an other div). Here is my code, I am working with angular, I have set a plunker.
app.directive('drag', function() {
return {
link: function(scope, element, attr) {
element.attr('draggable', true);
element.css('cursor', 'move');
element.bind('dragstart', function(event) {
console.log('dragstart');
console.log(event.dataTransfer);
event.dataTransfer.effectAllowed = 'move';
var img = document.createElement("img");
img.src = ".jpg";
event.dataTransfer.setDragImage(img, 0, 0);
});
}
};
});
But it doesn't work to change the icon if the source is a file for example or an url, here is the code. I am trying to change the icon during dragover or dragenter.
element.bind('dragenter', function(event) {
console.log('dragenter');
event.dataTransfer.effectAllowed = 'move';
var img = document.createElement("img");
img.src = ".jpg";
event.dataTransfer.setDragImage(img, 0, 0);
});
element.bind('dragover', function(event) {
if (event.preventDefault) {
event.preventDefault();
}
if (event.stopPropagation) {
event.stopPropagation();
}
console.log('dragover');
element.addClass('dragged');
var img = document.createElement("img");
img.src = ".jpg";
event.dataTransfer.setDragImage(img, 0, 0);
return false;
});
How to change DnD's (drag and drop) icon during dragover or dragenter? Is it even possible?
I am able to change the icon during dragstart if the source of the drop is inside the html page (for example drag a div in an other div). Here is my code, I am working with angular, I have set a plunker.
app.directive('drag', function() {
return {
link: function(scope, element, attr) {
element.attr('draggable', true);
element.css('cursor', 'move');
element.bind('dragstart', function(event) {
console.log('dragstart');
console.log(event.dataTransfer);
event.dataTransfer.effectAllowed = 'move';
var img = document.createElement("img");
img.src = "https://image.flaticon./teams/slug/google.jpg";
event.dataTransfer.setDragImage(img, 0, 0);
});
}
};
});
But it doesn't work to change the icon if the source is a file for example or an url, here is the code. I am trying to change the icon during dragover or dragenter.
element.bind('dragenter', function(event) {
console.log('dragenter');
event.dataTransfer.effectAllowed = 'move';
var img = document.createElement("img");
img.src = "https://image.flaticon./teams/slug/google.jpg";
event.dataTransfer.setDragImage(img, 0, 0);
});
element.bind('dragover', function(event) {
if (event.preventDefault) {
event.preventDefault();
}
if (event.stopPropagation) {
event.stopPropagation();
}
console.log('dragover');
element.addClass('dragged');
var img = document.createElement("img");
img.src = "https://image.flaticon./teams/slug/google.jpg";
event.dataTransfer.setDragImage(img, 0, 0);
return false;
});
Share
Improve this question
asked Jan 11, 2018 at 17:23
MoussaMoussa
4,1547 gold badges36 silver badges53 bronze badges
1 Answer
Reset to default 6As per the specification, you cannot use setDragImage on events other than drag start. See here: https://html.spec.whatwg/multipage/dnd.html#dom-datatransfer-setdragimage
The setDragImage(element, x, y) method must run the following steps:
If the DataTransfer object is no longer associated with a drag data store, return. Nothing happens.
If the drag data store's mode is not the read/write mode, return. Nothing happens.
If element is an img element, then set the drag data store bitmap to the element's image (at its intrinsic size); otherwise, set the drag data store bitmap to an image generated from the given element (the exact mechanism for doing so is not currently specified).
Set the drag data store hot spot coordinate to the given x, y coordinate.
And here, you can see that the drag data store is in read/write mode only on dragstart: https://html.spec.whatwg/multipage/dnd.html#concept-dnd-rw
Read/write mode For the dragstart event. New data can be added to the drag data store.
These are modes are there for security reasons, there are things you can do on the different stages of a drag and drop.
本文标签: javascriptHow to change icon during dragoverdragenter HTML 5 Drag and DropStack Overflow
版权声明:本文标题:javascript - How to change icon during dragoverdragenter HTML 5 Drag and Drop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744825291a2627023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论