Evitar que una imagen sea arrastrable o seleccionable sin usar JS


¿Alguien sabe de una manera de hacer una imagen no arrastrable y no seleccionable in al mismo tiempo in en Firefox, sin recurrir a Javascript? Parece trivial, pero aquí está el problema:

1) Se puede arrastrar y resaltar en Firefox:

<img src="...">

2) Así que agregamos esto, pero la imagen aún se puede resaltar mientras arrastra:

<img src="..." draggable="false">

3) Así que agregamos esto, para solucionar el problema de resaltado, pero luego contraintuitivamente, la imagen se vuelve arrastrable de nuevo. ¡Raro, lo sé! Utilizar FF 16.0.1

<img src="..." draggable="false" style="-moz-user-select: none;">

Entonces, ¿alguien sabe por qué agregar "-moz-user-select: none", de alguna manera triunfaría y deshabilitaría "draggable=false"? Por supuesto, webkit funciona como se esperaba. No hay nada en las Interwebs sobre this...It sería genial si pudiéramos arrojar algo de luz sobre esto juntos.

Gracias!!

Editar: Se trata de evitar que los elementos de la interfaz de usuario se arrastren inadvertidamente y mejorar la usabilidad , no un intento poco convincente de un esquema de protección anticopia :-)

Author: tmkly3, 2012-10-16

7 answers

Establezca las siguientes propiedades CSS en la imagen:

user-drag: none; 
user-select: none;
-moz-user-select: none;
-webkit-user-drag: none;
-webkit-user-select: none;
-ms-user-select: none;
 149
Author: Jeff Wooden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-30 16:34:28

He estado olvidando compartir mi solución, no pude encontrar una manera de hacer esto sin usar JS. Hay algunos casos de esquina donde @ Jeffery CSS sugerido de madera simplemente no cubrirá.

Esto es lo que aplico a todos mis contenedores de IU, no es necesario aplicarlo a cada elemento ya que se vuelve a aplicar a todos los elementos secundarios.

.unselectable {
    /* For Opera and <= IE9, we need to add unselectable="on" attribute onto each element */
    /* Check this site for more details: http://help.dottoro.com/lhwdpnva.php */
    -moz-user-select: none; /* These user-select properties are inheritable, used to prevent text selection */
    -webkit-user-select: none;
    -ms-user-select: none; /* From IE10 only */
    user-select: none; /* Not valid CSS yet, as of July 2012 */

    -webkit-user-drag: none; /* Prevents dragging of images/divs etc */
    user-drag: none;
}

var makeUnselectable = function( $target ) {
    $target
        .addClass( 'unselectable' ) // All these attributes are inheritable
        .attr( 'unselectable', 'on' ) // For IE9 - This property is not inherited, needs to be placed onto everything
        .attr( 'draggable', 'false' ) // For moz and webkit, although Firefox 16 ignores this when -moz-user-select: none; is set, it's like these properties are mutually exclusive, seems to be a bug.
        .on( 'dragstart', function() { return false; } );  // Needed since Firefox 16 seems to ingore the 'draggable' attribute we just applied above when '-moz-user-select: none' is applied to the CSS 

    $target // Apply non-inheritable properties to the child elements
        .find( '*' )
        .attr( 'draggable', 'false' )
        .attr( 'unselectable', 'on' ); 
};

Esto era mucho más complicado de lo que tenía que ser.

 53
Author: tmkly3,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-15 23:12:34

Puedes usar la propiedad pointer-events en tu CSS, y establecerla igual a 'none'

img {
    pointer-events: none;
}

Editado

Esto bloqueará el evento (clic). Así que mejor solución sería

<img draggable="false" (dragstart)="false;" class="unselectable">

.unselectable {
  user-drag: none; 
  user-select: none;
  -moz-user-select: none;
  -webkit-user-drag: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}
 27
Author: Linh Dam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-31 10:22:55

Dependiendo de la situación, a menudo es útil hacer que la imagen sea una imagen de fondo de un div con CSS.

<div id='my-image'></div>

Luego en CSS:

#my-image {
    background-image: url('/img/foo.png');
    width: ???px;
    height: ???px;
}

Ver esto JSFiddle para un ejemplo en vivo con un botón y una opción de tamaño diferente.

 13
Author: Nick Merrill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-20 16:45:56

Probablemente podrías recurrir a

<img src="..." style="pointer-events: none;">
 5
Author: Thomas Bachem,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-07 00:46:53

Puede establecer la imagen como imagen de fondo. Dado que reside en un div, y el div es indragable, la imagen será indragable:

<div style="background-image: url("image.jpg");">
</div>
 0
Author: joshua pogi 28,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-22 06:35:57

Una solución genérica especialmente para el navegador Edge de Windows (como la regla-ms-user-select: none; CSS no funciona):

Ventana.ondragstart = function () {return false}

Nota: Esto puede ahorrarle tener que agregar draggable=false a cada etiqueta img cuando aún necesita el evento de clic (es decir, no puede usar pointer-events=none), pero no desea que aparezca la imagen del icono de arrastre.

 0
Author: Philip Murphy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-14 10:18:45