Preventing an image from being draggable or select

2019-01-12 16:01发布

Does anyone know of a way to make an image not draggable and not selectable -- at the same time -- in Firefox, without resorting to Javascript? Seems trivial, but here's the issue:

1) Can be dragged and highlighted in Firefox:

<img src="...">

2) So we add this, but image can still be highlighted while dragging:

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

3) So we add this, to fix the highlighting issue, but then counterintuitively, the image become draggable again. Weird, I know! Using FF 16.0.1

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

So, does anyone know why adding "-moz-user-select: none", would somehow trump and disable "draggable=false"? Of course, webkit works as expected. Nothing is out there on the Interwebs about this...It would be great if we could shine some light on this together.

Thanks!!

Edit: This is about keeping UI elements from being inadvertently dragged and improving usability - not some lame attempt at a copy protection scheme :-)

7条回答
走好不送
2楼-- · 2019-01-12 16:41

You can use the pointer-events property in your CSS, and set it equal to 'none'

img {
    pointer-events: none;
}

Edited

this will block (click) event. So better solution would be

<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;
}
查看更多
登录 后发表回答