开发者

With HTML5 drag and drop, how can I change the drag image when it's over the drop zone?

开发者 https://www.devze.com 2023-04-01 17:19 出处:网络
So here\'s what I want to do. I would like to开发者_如何转开发 be able to drag an element, and have it dynamically change it\'s default drag image to a different image when it\'s over the drop zone. Y

So here's what I want to do. I would like to开发者_如何转开发 be able to drag an element, and have it dynamically change it's default drag image to a different image when it's over the drop zone.


You are able to set the drag image using dataTransfer.setDragImage method. It exists in the event objects of all of the drag events, including the dragover event.

Some basic code would be:

dropElem.ondragover = function(e) {
    if (e.stopPropagation) {
        e.stopPropagation(); //Stops some browsers from redirecting.
    }
    var dragIcon = document.createElement('img');
    dragIcon.src = 'image.png';
    dragIcon.width = 100;
    e.dataTransfer.setDragImage(dragIcon, 0, 0);
};
0

精彩评论

暂无评论...
验证码 换一张
取 消