Chrome 在拖动时将光标设置为文本,为什么?

发布于 2024-08-30 21:38:10 字数 98 浏览 3 评论 0原文

我的应用程序有许多拖放功能。拖动时我希望光标更改为某种抓取光标。 Internet Explorer 和 Firefox 对此工作得很好,但 Chrome 总是将光标更改为文本光标。

My application has many drag and drop features. While dragging I want the cursor to change to some grab cursor. Internet Explorer and Firefox work fine for this, but Chrome always changes the cursor to the text cursor.

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

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

发布评论

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

评论(8

烟沫凡尘 2024-09-06 21:38:10

这些解决方案都不适合我,因为代码太多。

我使用自定义 jQuery 实现来进行拖动,并在 mousedown 处理程序中添加这一行,在 Chrome 中为我解决了这个问题。

e.originalEvent.preventDefault();

None of these solutions worked for me because it's too much code.

I use a custom jQuery implementation to do the drag, and adding this line in the mousedown handler did the trick for me in Chrome.

e.originalEvent.preventDefault();
静水深流 2024-09-06 21:38:10

尝试关闭文本选择事件。

document.onselectstart = function(){ return false; }

这将禁用页面上的任何文本选择,并且浏览器似乎开始显示自定义光标。

但请记住,文本选择根本不起作用,因此最好仅在拖动时执行此操作,然后立即再次打开它。只需附加不返回 false 的函数:

document.onselectstart = function(){ return true; }

Try turning off text selection event.

document.onselectstart = function(){ return false; }

This will disable any text selection on the page and it seems that browser starts to show custom cursors.

But remember that text selection will not work at all, so it's the best to do it only while dragging, and turn it on again just after that. Just attach function that doesn't return false:

document.onselectstart = function(){ return true; }
剩一世无双 2024-09-06 21:38:10

如果您想阻止浏览器选择元素内的文本并显示选择光标,您可以执行以下操作:

element.addEventListener("mousedown", function(e) { e.preventDefault(); }, false);

If you want to prevent the browser from selecting text within an element and showing the select cursor, you can do the following:

element.addEventListener("mousedown", function(e) { e.preventDefault(); }, false);
心病无药医 2024-09-06 21:38:10

陷阱

您不能将 放入

document.onselectstart = function(){ return false; };

“mousedown”处理程序中,因为 onselectstart 已经被触发。

解决方案

因此,要使其正常工作,您需要在 mousedown 事件之前执行此操作。我在鼠标悬停事件中做到了这一点,因为一旦鼠标进入我的元素,我希望它是可拖动的,而不是可选择的。然后您可以将

document.onselectstart = null;

调用放入 mouseout 事件中。然而,有一个问题。拖动时,可能会调用 mouseover/mouseout 事件。为了解决这个问题,在拖动/鼠标按下事件中,将 flag_dragging 设置为 true,并在拖动停止(鼠标松开)时将其设置为 false。 mouseout 函数可以在设置之前检查该标志

document.onselectstart = null;

示例

我知道您没有使用任何库,但这里有一个可能对其他人有帮助的 jQuery 代码示例。

var flag_dragging = false;//counter Chrome dragging/text selection issue
$(".dragme").mouseover(function(){
    document.onselectstart = function(){ return false; };
}).mouseout(function(){
    if(!flag_dragging){
        document.onselectstart = null;
    }
});

//make them draggable
$(".dragme").draggable({
    start: function(event, ui){
        flag_dragging = true;
    }, stop: function(event, ui){
        flag_dragging = false;
    }
});

Pitfall

You cannot put the

document.onselectstart = function(){ return false; };

into your "mousedown" handler because onselectstart has already been triggered.

Solution

Thus, to have it working, you need to do it before the mousedown event. I did it in the mouseover event, since as soon as the mouse enters my element, I want it to be draggable, not selectable. Then you can put the

document.onselectstart = null;

call into the mouseout event. However, there's a catch. While dragging, the mouseover/mouseout event might be called. To counter that, in your dragging/mousedown event, set a flag_dragging to true and set it to false when dragging stops (mouseup). The mouseout function can check that flag before setting

document.onselectstart = null;

Example

I know you are not using any library, but here's a jQuery code sample that might help others.

var flag_dragging = false;//counter Chrome dragging/text selection issue
$(".dragme").mouseover(function(){
    document.onselectstart = function(){ return false; };
}).mouseout(function(){
    if(!flag_dragging){
        document.onselectstart = null;
    }
});

//make them draggable
$(".dragme").draggable({
    start: function(event, ui){
        flag_dragging = true;
    }, stop: function(event, ui){
        flag_dragging = false;
    }
});
森林散布 2024-09-06 21:38:10

我通过使元素不可选择并在拖动的元素上添加活动伪类解决了同样的问题:

* {
    -webkit-user-select: none; 
}

.your-class:active {
    cursor: crosshair;
}

I solved a same issue by making the Elements not selectable, and adding an active pseudo class on the draged elements:

* {
    -webkit-user-select: none; 
}

.your-class:active {
    cursor: crosshair;
}
盗琴音 2024-09-06 21:38:10

我面临着几乎同样的问题。我希望我的 DIV 元素及其所有子元素中的光标成为默认值,此处的 CSS 提示在 IE、FF 和 Opera 中有所帮助,但不适用于 Google Chrome。这是我在父 DIV 中所做的操作:

> ...

现在它工作正常。希望这有帮助。

I was facing almost same problem. I want cursor inside my DIV element and all its child to be the default, the CSS tips here helped in IE, FF and Opera, but not for Google Chrome. Here is what I have done in parent DIV:

<div ... onselectstart="return false;" ... > ... </div>

Now it is working fine. Hope this help.

无人接听 2024-09-06 21:38:10

我在使用 jQuery UI 可拖动和可排序(版本 1.8.1)时遇到了类似的问题,而且它非常具体,所以我假设您使用的是相同的库。

问题是由 jQuery UI 中的错误引起的(实际上是对其他 Safari 错误的修复)。
我刚刚在 jQuery UI http://dev.jqueryui.com/ticket/5678 所以我想你需要等到它修复为止。

我已经找到了解决此问题的方法,但它非常核心,因此只有在您真正知道发生了什么时才使用它;)

if ($.browser.safari) {
    $.ui.mouse.prototype.__mouseDown = $.ui.mouse.prototype._mouseDown;
    $.ui.mouse.prototype._mouseDown = function(event){
        event.preventDefault();
        return $.ui.mouse.prototype.__mouseDown.apply(this, arguments);
    }
}

它只是关闭 jQuery UI 代码中的修复,所以基本上它可能打破一些东西。

I have a similar issue using jQuery UI draggable and sortable (ver. 1.8.1), and it's quite specific, so I assume that you are using same library.

Problem is caused by a bug in jQuery UI (actually a fix for other Safari bug).
I just raised the issue in jQuery UI http://dev.jqueryui.com/ticket/5678 so I guess you will need to wait till it's fixed.

I've found a workaround for this, but it's quite hard-core, so you only use it if you really know what is going on ;)

if ($.browser.safari) {
    $.ui.mouse.prototype.__mouseDown = $.ui.mouse.prototype._mouseDown;
    $.ui.mouse.prototype._mouseDown = function(event){
        event.preventDefault();
        return $.ui.mouse.prototype.__mouseDown.apply(this, arguments);
    }
}

It simply switches off the fix that's in jQuery UI code, so basically it may break something.

迷雾森÷林ヴ 2024-09-06 21:38:10

只需在您的 mousedown 事件中使用此行

arguments[0].preventDefault();

您还可以通过 CSS 将此类添加到可拖动元素来禁用文本选择

.nonselectable {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

Just use this line inside your mousedown event

arguments[0].preventDefault();

You can also disable text selection by CSS adding this class to your draggable element

.nonselectable {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文