[英]Change cursor while moving mouse
在移動/拖動鼠標時嘗試在元素上放置“移動”類時遇到一個奇怪的錯誤。 我在Chrome 59.0.3071.115上使用jQuery 3.1.1。
我將問題簡化為以下示例:
<html>
<head>
<style>
.thing {
display: block;
width: 10em;
height: 10em;
background: green;
}
.moving {
cursor: move;
}
</style>
<script src="jquery-3.1.1.min.js"></script>
</head>
<body>
<div class="thing"></div>
<script>
$(document).ready(function(){
var $thing = $('.thing');
$thing.on('mousedown', function(e){
$thing.addClass("moving");
console.log("mousedown");
}).on('mouseup', function(e){
$thing.removeClass("moving");
console.log("mouseup");
});
});
</script>
</body>
</html>
這將在頁面中顯示一個綠色框,並在您的鼠標上下移動時觸發事件。
發生什么事了
div
(可在Chrome開發者工具:元素標簽中看到),但光標停留在通常的箭頭上。 我希望光標更改為move
光標。 div
上釋放單擊-光標會暫時切換到move
光標,但是如果完全移動鼠標,則會切換回默認箭頭。 我已經嘗試過https://stackoverflow.com/a/16172027/1766230之類的解決方案,還有其他運氣不佳的解決方案。 我在CSS,各種元素等中嘗試了選擇器的各種組合。奇怪的是,在jsfiddle中嘗試此操作時,一切正常,但是使用作為獨立HTML文件的內容,我看到了錯誤。
原來這一定是瀏覽器錯誤,因為當我關閉Chrome並重新打開它時,它開始按預期運行。 有人知道Chrome中的這種錯誤嗎?
只是一種選擇 :( 不帶JS)
tabindex
:active:hover
.thing { display: block; width: 10em; height: 10em; background: green; user-select: none; outline: none; } .thing:active:hover { cursor: move; background: red; }
<div class="thing" tabindex="1"></div>
drag != mousedown
它是瀏覽器的默認拖動行為。使用mousedown
drag
事件
$(document).ready(function() { var $thing = $('.thing'); $thing.on('mousedown ,drag', function(e) { $thing.addClass("moving"); console.log("mousedown"); }).on('mouseup', function(e) { $thing.removeClass("moving"); console.log("mouseup"); }); });
.thing { display: block; width: 10em; height: 10em; background: green; } .moving { cursor: move; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="thing"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.