在 Javascript 中,FireFox 不允许通过拖动来调整 div 大小

发布于 2024-12-31 23:56:36 字数 1054 浏览 0 评论 0原文

我有一个带边框的 div,在其右下角有用于调整大小的图像:

在此处输入图像描述

因此,当用户在图像上按下鼠标,他(或她)可以拖动鼠标并调整 div 的大小。

除了 FireFox 之外,这在所有浏览器中都可以正常工作。

在 FireFox 中,发生了一些奇怪的事情:用户按下鼠标并开始拖动后,光标变为:

在此处输入图像描述

因此当拖动鼠标时,光标更改为这一状态,并且鼠标移动事件不会到来。

我想知道是什么导致了这种行为。我想也许 FireFox 认为用户试图通过按下并拖动鼠标来选择文本。但我使用以下代码取消了文本选择:(

resizeImageImg.onselectstart          = "return false;";
resizeImageImg.ondragstart            = "return false;";

resizeImageImg.style.WebkitUserSelect = 'none';
resizeImageImg.style.KhtmlUserSelect  = 'none';
resizeImageImg.style.MozUserSelect    = 'none';
resizeImageImg.style.MsUserSelect     = 'none';
resizeImageImg.style.OUserSelect      = 'none';
resizeImageImg.style.UserSelect       = 'none';

resizeImageImg.setAttribute ("unselectable", "on");
resizeImageImg.setAttribute ("draggable",    "false");

对于 div 和调整大小图像)

但这并没有解决问题。 FireFox 仍然不允许调整大小并将光标更改为“不允许”。

有人可以帮忙吗?

I have a div with border and in its right-bottom corner I have image for resizing:

enter image description here

So when user presses mouse on the image, he (or she) can drag mouse and resize the div.

This works fine in all browsers but FireFox.

In FireFox something strange happens: after the user presses mouse and starts dragging, the cursor changes to:

enter image description here

So the cursor changes to this one and mouse move events are not coming, when the mouse is dragged.

I am wondering, what causes this behaviour. I thought maybe FireFox thinks that the user is trying to select text by pressing and dragging the mouse. But I cancelled text selection using this code:

resizeImageImg.onselectstart          = "return false;";
resizeImageImg.ondragstart            = "return false;";

resizeImageImg.style.WebkitUserSelect = 'none';
resizeImageImg.style.KhtmlUserSelect  = 'none';
resizeImageImg.style.MozUserSelect    = 'none';
resizeImageImg.style.MsUserSelect     = 'none';
resizeImageImg.style.OUserSelect      = 'none';
resizeImageImg.style.UserSelect       = 'none';

resizeImageImg.setAttribute ("unselectable", "on");
resizeImageImg.setAttribute ("draggable",    "false");

(for both: the div and the resize image)

But this did not solve the problem. FireFox still does not let resizing and changes cursor to "not-allowed".

Can anybody please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒一口袋星星 2025-01-07 23:56:36

谢谢大家,我找到了解决方案。

我将:替换

resizeImageImg.ondragstar = "return false;"; 

resizeImageImg.ondragstart = function () { return false; };

,它也开始在 FireFox 中工作。

这里发生的情况是,如果您想在鼠标按下事件来自图像时处理鼠标移动事件,那么您必须使图像不可拖动。但这还不足以使用

resizeImageImg.setAttribute ("draggable", false);

(至少在 FireFox 中),因为 ondragstart 事件仍在到来。当我设置时我明白了这一点:

resizeImageImg.ondragstart = function () { alert ("ondragstart"); return false; };

所以我意识到 FireFox 不遵守 setAttribute ("draggable", false) - 而其他浏览器则这样做。

Thank you all, I found the solution.

I replaced:

resizeImageImg.ondragstar = "return false;"; 

by

resizeImageImg.ondragstart = function () { return false; };

and it started working in FireFox as well.

What happens here is that if you want to process mouse-move events when your mouse-down event came from an image, then you have to make you image not-draggable. But this is not enough to use

resizeImageImg.setAttribute ("draggable", false);

(at least in FireFox) becasuse events ondragstart are still coming. I understood this when I set:

resizeImageImg.ondragstart = function () { alert ("ondragstart"); return false; };

So I realized that FireFox does not obbey setAttribute ("draggable", false) - whilst other browsers do.

遮了一弯 2025-01-07 23:56:36

安迪,这是我想出的解决方案。我付出了巨大的努力来使其快速且易于使用。

您可以在此处查看该文件:
http://files.social-library.org/stackoverflow/imageResizer.html

使用起来很简单。创建图像并指定宽度和高度。然后,页面加载后,调用函数 imageResizer.init(imageObject) 发送图像对象作为参数。然后它将使用拖动器设置图像。

这适用于 Firefox、Chrome 和 Internet Explorer 8+。

Andy, here is the solution I have come up with. I have gone to great effort to make it quick and easy to use.

You can view the file here:
http://files.social-library.org/stackoverflow/imageResizer.html

It is simple to use. Create your image and specify a width and height. Then, once the page loads call the function imageResizer.init(imageObject) sending the image object as a parameter. It will then set the image up with the dragger.

This works in firefox, chrome and internet explorer 8+.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文