简体   繁体   English

Firefox 防止图片拖拽

[英]Prevent Image dragging in Firefox

I have a link with background image which is in a carousel.我有一个与轮播中的背景图片的链接。 The problem is that whenever the user drags the carousel, the image get dragged instead with a ghost image visible when dragging.问题在于,每当用户拖动轮播时,图像都会被拖动,而拖动时会出现一个可见的重影图像。 This causes inconsistent dragging behaviour for the carousel.这会导致轮播不一致的拖动行为。 I tried using user-select: none , but it only works in Chrome and Edge.我尝试使用user-select: none ,但它只适用于 Chrome 和 Edge。 How do I disable image dragging in Firefox?如何禁用 Firefox 中的图像拖动? Adding draggable="false" on anchor tag also doesn't work在锚标记上添加draggable="false"也不起作用

Run below code in Firefox to test.运行以下代码在 Firefox 进行测试。 Try to drag the image and observe the behaviour.尝试拖动图像并观察行为。

 .disable-dragging { display: block; width: 200px; height: 200px; background-image: url("https://homepages.cae.wisc.edu/~ece533/images/fruits.png"); background-size: cover; background-color: #cccccc; user-select: none; }
 <a href="#" class="disable-dragging" draggable="false"></a>

You have missed the = in the HTML attribute您错过了 HTML 属性中的=

<a href="#" class="disable-dragging" draggable="false"></a>

And remove user-select: none;并删除user-select: none;

Full working code完整的工作代码

 .disable-dragging { display: block; width: 200px; height: 200px; background-image: url('https://homepages.cae.wisc.edu/~ece533/images/fruits.png'); background-size: cover; background-color: #cccccc; /* user-select: none; */ }
 <a href="https://google.com" class="disable-dragging" draggable="false"></a>

Update:更新:

As @kaiido said in the comment user-select has an influence here.正如@kaiido 在评论中所说, user-select在这里有影响。

And is an open issue in firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1376369并且是 firefox 中的未解决问题 - https://bugzilla.mozilla.org/show_bug.cgi?id=1376369

You can prevent default action on mousedown event for the image您可以阻止对图像的 mousedown 事件的默认操作

<img src="test.png" onmousedown="if (event.preventDefault) event.preventDefault()">

You can disable this for all image using您可以使用禁用所有图像

window.onload = function (e) {
    var evt = e || window.event, imgs, i;
  
    if (evt.preventDefault) {
        imgs = document.getElementsByTagName('img');
        for (i = 0; i < imgs.length; i++) {
            imgs[i].onmousedown = disableDragging;
        }
    }
};
 
function disableDragging(e) {
    e.preventDefault();
}

In your case you can replace the tag在您的情况下,您可以更换标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM