我的应用程序有许多拖放功能。在拖动时,我希望光标改变为某种抓取光标。Internet Explorer和Firefox可以正常工作,但Chrome始终将光标更改为文本光标。
我的应用程序有许多拖放功能。在拖动时,我希望光标改变为某种抓取光标。Internet Explorer和Firefox可以正常工作,但Chrome始终将光标更改为文本光标。
这些解决方案对我都没用,因为它们的代码太多了。
我使用了一个自定义的jQuery实现来进行拖动,在mousedown
处理程序中添加这行代码可以让Chrome浏览器正常工作。
e.originalEvent.preventDefault();
mousedown
事件才能使其正常工作。 - Lorenzo Polidori尝试关闭文本选择事件。
document.onselectstart = function(){ return false; }
这将禁用页面上的所有文本选择,而且浏览器似乎开始显示自定义光标。
但请记住,文本选择将完全无法使用,因此最好只在拖动时这样做,并在此之后立即重新启用它。只需附加不返回false的函数即可:
document.onselectstart = function(){ return true; }
mousedown
事件的默认操作。请参见此处链接:https://dev59.com/P3E85IYBdhLWcg3wgDvd#12957678 - Lorenzo Polidorie.originalEvent.preventDefault();
。 - Chris Nolet如果你想防止浏览器选中元素内的文本并显示选择光标,你可以采取以下措施:
element.addEventListener("mousedown", function(e) { e.preventDefault(); }, false);
您不能放置
document.onselectstart = function(){ return false; };
因为onselectstart事件已经被触发,所以需要在"mousedown"处理程序中添加此代码,才能使它起作用。
因此,为了使它正常工作,您需要在mousedown事件之前执行此操作。我在mouseover事件中执行此操作,因为一旦鼠标进入我的元素,我希望它可以被拖动,而不是可选择的。然后你可以将
document.onselectstart = null;
在调用mouseout事件时需要注意一个问题。当拖动时,可能会触发mouseover/mouseout事件。为了解决这个问题,在拖动/mousedown事件中,设置一个名为flag_dragging的标志,并在拖动停止(mouseup)时将其设置为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;
}
});
mouseenter
和mouseleave
正是我想要的,因为在拖动元素时会不断触发mouseover
。 - jberryman我通过使元素不可选,并在拖动的元素上添加一个活动伪类来解决了同样的问题:
* {
-webkit-user-select: none;
}
.your-class:active {
cursor: crosshair;
}
input { -webkit-user-select: auto; }
的代码,让用户在输入框中选择文本。 - Laurence我面临了几乎相同的问题。我希望光标在我的DIV元素及其所有子元素内时都是默认的,这里的CSS技巧对IE、FF和Opera有帮助,但对Google Chrome无效。这是我在父DIV中所做的:
<div ... onselectstart="return false;" ... > ... </div>
现在它正常工作了。希望这能帮到你。
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代码中的修复程序,因此基本上可能会破坏某些内容。
在你的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;
}
arguments[0].preventDefault();
已经足够了。 - A1rPun