以下代码示例在单击按钮时添加了一个半透明的100%覆盖层。覆盖层会一直保持到释放鼠标按钮为止。以下是预期发生的情况:
单击按钮并按住鼠标。
当鼠标按下并释放Shift键时,按住鼠标。
按下Shift键时,div区域将显示文本“horizontal”,光标将更改为e-resize。释放Shift键后,div将显示“vertical”,光标将更改为n-resize。
在IE9/10中,文本会更改,但光标仍然相同。我尝试将键绑定更改为body级别和overlay级别,但均无效。
以下是代码:(我尝试将其放入jsfiddle和jsbin中,但由于某种原因它们完全忽略了按键操作)。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
.overlay {
background-color: rgba(0, 0, 0, 0.3);
top: 0; bottom: 0; left: 0; right: 0; position: fixed;
z-index: 999;
}
.vertical { cursor: n-resize !important; }
.horizontal { cursor: e-resize !important; }
</style>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#button1").mousedown(onButtonMouseDown);
function onButtonMouseDown(e) {
var overlay;
overlay = $("body").after("<div class=\"overlay\"></div>").next();
overlay.addClass((e.shiftKey) ? "horizontal" : "vertical");
$(document).bind("keydown.ntextbox", function (e) {
if (e.which === 16) {
overlay.removeClass("vertical").addClass("horizontal");
$("#div1").html("horizontal");
}
});
$(document).bind("keyup.ntextbox", function (e) {
if (e.which === 16) {
overlay.removeClass("horizontal").addClass("vertical");
$("#div1").html("vertical");
}
});
overlay.mouseup(function (e) {
overlay.remove();
$(document).unbind(".ntextbox");
return false;
});
return false;
}
});
</script>
</head>
<body>
<div id="div1">...</div>
<button id="button1">Click me</button>
</body>
</html>