所以我有一个 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
在你的情况下。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句