Css /JS to prevent dragging of ghost image?
To prevent image dragging, assign draggable="false"
to the <img>
element and apply user-select: none;
CSS rule to avoid incidental selection. Use JavaScript to handle the dragstart
events and block them.
A comprehensive approach to anti-dragging
Let's break this down further for superior understanding and precise control over drag behaviors.
Defusing the drag bomb entirely
You might want to lock down dragging for all elements like in a high-security area:
This is essentially like sending a "Stay where you are!" command to every element. However, it's easier to manage if you assign these properties to specific classes rather than globally.
Taming the wild Webkit
Webkit-based browsers sometimes need a gentle nudge with the -webkit-user-drag
property:
A little JavaScript, like a scolding aunt, would ensure doubly that they conform:
Keeping the balance on the tightrope of functionality
Using pointer-events: none;
might seem tempting, think of it like a switch that turns off all interaction including clicks and hovers too, so be precise:
In JavaScript, don't make things quiet. We still want our app to talk back:
Dancing the mobile-touch tango
Touch devices groover to a different beat 🕺 and don't respond to draggable=false
. They require extra shimmy in the CSS:
This gets all the partners moving in sync, regardless of their platform. With touch-action
, you guide the dance.
The intricacies of interactivity
While disabling dragging with JavaScript, remember to dance with the click events too:
Sensible JavaScript house rules ensure that all guests have a great time.
A masterclass in cross-browser compatibility
Every browser is a guest at this party. Keep everyone happy with some extra servings of browser-specific rules:
Beatmatch these CSS tunes with a JavaScript DJ that keeps up with browser compatibility:
Was this article helpful?