[英]Changing the cursor while it's not moving
因此,我有一个Web应用程序(在html,css和javascript,jquery中),可在其中拖动一个元素(这意味着,光标不会在该元素上移动,因为该元素随光标一起移动)。 我希望将光标更改为“移动”光标,但是遇到这种奇怪的行为。 我写了这段代码来演示:
<html>
<head></head>
<body oncontextmenu="return false;">
<script>
var b=document.getElementsByTagName('body')[0];
b.onmousedown=function(event){
if(event.button){
b.style.cursor='move';
}
}
b.onmouseup=function(event){
if(event.button){
b.style.cursor='initial';
}
}
</script>
</body>
</html>
所以基本上,我希望光标更改为“ cursor:move;”。 每当用户按住鼠标右键时; 但是,发生以下情况:
所以现在我的问题是:为什么会发生这种情况,什么是解决它的好方法?
PS:经过chrome测试,这是我需要使用的主要浏览器
http://jsbin.com/vepihik/edit?html,css,js,output
document.addEventListener('mousedown', onMouseAction) document.addEventListener('mouseup', onMouseAction) function onMouseAction(e){ document.body.style.cursor = e.buttons && e.button == 2 ? 'move' : 'default'; }
html, body{ height:100%; }
Try to hold the RIGHT mouse button and move it, then release
将mousedown
和mouseup
事件附加到文档本身,并使它们都调用相同的函数,只需检查单击的按钮即可。 在您的情况下为right = 2
。
您可以附加mousedown
和mouseup
事件来启动将更改和还原光标的功能。
在每个功能中,您可以确认刚刚按下(或释放)的按钮是鼠标右键。
工作示例:
var div = document.getElementsByTagName('div')[0]; function changeCursorToMove(event) { if ((event.which === 3) || (event.button === 2)) { div.classList.add('move-cursor'); } } function changeCursorToDefault(event) { if ((event.which === 3) || (event.button === 2)) { div.classList.remove('move-cursor'); } } div.addEventListener('mousedown', changeCursorToMove, false); div.addEventListener('mouseup', changeCursorToDefault, false); document.oncontextmenu = function(){return false;}
div { width: 100px; height: 100px; background-color: red; } .move-cursor { cursor: move; }
<div></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.