Another way to disable dragging an image is to set the pointer-event CSS property to none .
We can disable drag and drop on HTML elements by setting the draggable attribute to false . We set draggable to false so we can't drag it. We add event listeners for the dragstart and drop events with addEventListener .
To make an object draggable set draggable=true on that element. Just about anything can be drag-enabled: images, files, links, files, or any markup on your page.
Btw, you can make a div "draggable" without JS, but you cannot "drag" it.
You can set the draggable
attribute to false
in either the markup or JavaScript code.
// As a jQuery method: $('#myImage').attr('draggable', false);
document.getElementById('myImage').setAttribute('draggable', false);
<img id="myImage" src="http://placehold.it/150x150">
I think you can change your
img {
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
}
into a
img {
-webkit-user-drag: none;
-khtml-user-drag: none;
-moz-user-drag: none;
-o-user-drag: none;
user-drag: none;
}
Try it:
img {
pointer-events: none;
}
and try to avoid
* {
pointer-events: none;
}
This will disable dragging for an image in all browsers, while preserving other events such as click and hover. Works as long as any of HTML5, JS, or CSS are available.
<img draggable="false" onmousedown="return false" style="user-drag: none" />
If you're confident the user will have JS, you only need to use the JS attribute, etc. For more flexibility, look into ondragstart, onselectstart, and some WebKit tap/touch CSS.
You can use a CSS property to disable images in webkit browsers.
img{-webkit-user-drag: none;}
Very simple don't make it complicated with lots of logic use simple attribute draggable and make it false
<img draggable="false" src="img/magician.jpg" alt="" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With