Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable dragging of a file system image into a browser.

I am experimenting with the HTML5 file API. I notice however that browsers have a default behaviour where they display an image if you drag the image into the browser. This can however be annoying if your aim is to upload the image rather than to view it.

I am wondering if there is a way of preventing this behaviour? I have tried stopPropagation / preventDefault on an ondrop event which works somewhat, however leaves the "drop" cursor in place giving the impression that the image can be dropped anywhere on the page.

Ideally you would only see the "drop" cursor on the designated area where images are meant to be dropped.

like image 440
Kenneth Spencer Avatar asked Sep 13 '11 00:09

Kenneth Spencer


People also ask

How do I stop images dragging on my website?

Setting the draggable HTML Attribute to false We can also set the draggable HTML attribute of the img element we want to disable dragging for to false . to disable dragging. img. setAttribute("draggable", false);

How do I stop images dragging in react?

To disable dragging while controlled, send the prop disabled={true} - at this point the <Draggable> will operate like a completely static component.

How do I stop dragging JavaScript?

Prevent Image Dragging using JavaScript “ondragstart” event The JavaScript ondragstart event occurs when a user drags an HTML element (in our case it's an Image) on a web page. An image on a web page is draggable by default. We can use the ondragstart event to prevent dragging of the image.


1 Answers

The dataTransfer object has dropEffect and effectAllowed properties. Not exactly sure what the difference between them, but setting both to 'none' should help.

$(document).bind({
   dragenter: function (e) {
      e.stopPropagation();
      e.preventDefault();
      var dt = e.originalEvent.dataTransfer;
     dt.effectAllowed = dt.dropEffect = 'none';
   },
   dragover: function (e) {
      e.stopPropagation();
      e.preventDefault();
      var dt = e.originalEvent.dataTransfer;
      dt.effectAllowed = dt.dropEffect = 'none';
   }
});

See the http://jsfiddle.net/andreymir/H3RR7/embedded/result/ - drop allowed to rectangle only.

like image 167
Andrey M. Avatar answered Sep 17 '22 17:09

Andrey M.