在 Javascript 中,FireFox 不允许通过拖动来调整 div 大小
我有一个带边框的 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:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢大家,我找到了解决方案。
我将:替换
为
,它也开始在 FireFox 中工作。
这里发生的情况是,如果您想在鼠标按下事件来自图像时处理鼠标移动事件,那么您必须使图像不可拖动。但这还不足以使用
(至少在 FireFox 中),因为 ondragstart 事件仍在到来。当我设置时我明白了这一点:
所以我意识到 FireFox 不遵守 setAttribute ("draggable", false) - 而其他浏览器则这样做。
Thank you all, I found the solution.
I replaced:
by
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
(at least in FireFox) becasuse events ondragstart are still coming. I understood this when I set:
So I realized that FireFox does not obbey setAttribute ("draggable", false) - whilst other browsers do.
安迪,这是我想出的解决方案。我付出了巨大的努力来使其快速且易于使用。
您可以在此处查看该文件:
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+.