HTML5 draggable='false' not working in Firefox browser

后端 未结 4 1706
野的像风
野的像风 2020-12-16 12:28

I\'m simply trying to apply HTML5 draggable=\'false\' attribute to some images but it\'s not working in Firefox browser. In Chrome working fine but on Firefox, after sele

相关标签:
4条回答
  • 2020-12-16 13:06

    You can set the following CSS properties to the image:

    .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;
    }
    

    HTML code:

    <img src="something.jpg" class="unselectable">
    

    Fiddle

    0 讨论(0)
  • 2020-12-16 13:10

    Update of James Morrison's update: even the newer version doesn't seem to work anymore.

    I could sort out a similar issue with a check inside the addEventListener("dragstart",...) which you probably need to set in case you are playing with drag & drop. In my case:

    document.addEventListener("dragstart", function( event ) {
        if (event.target.parentNode.localName == 'li') {
          // console.log("In case it's inside a <li> element, prevent the drag");
          event.preventDefault();
        } else {
          // code taken from a Mozilla example, replace with what you need:
          // store a ref. on the dragged elem
          dragged = event.target;
          // make it half transparent
          event.target.style.opacity = .5;
        }
    }, false);
    

    To check for an <img> the check would look something like if (event.target.localName == 'img') {...

    As per the OP request: all of the "html only" solutions I have tried were not working.

    0 讨论(0)
  • 2020-12-16 13:11

    Update of sorts, the solution doesn't work with React, however adding the below does.

    onDragStart={(e) => { e.preventDefault() }}
    

    EDIT: returning false for ondragstart no longer works for more modern versions of Firefox (credit: Hooman Askari), in which case use the below.

    function dragStart(e) {
        e.preventDefault()
    }
    

    ...and on the element

    ondragstart="dragStart(e)"
    
    0 讨论(0)
  • 2020-12-16 13:19

    just try this

    add ondragstart="return false;" to your html element

    <div id="dnd">
        <textarea placeholder="drop here"></textarea>
        <img src="http://johnlewis.scene7.com/is/image/JohnLewis/231108668?$prod_main$" draggable='false' ondragstart="return false;"/>
    </div>

    0 讨论(0)
提交回复
热议问题