<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用键盘控制DIV</title>
<style type="text/css">
html,body{overflow:hidden;}
body{margin:0;padding:0;}
pre{
color:green;
padding:10px 15px;
background:#f0f0f0;
border:1px dotted #333;
font:12px/1.5 Courier New;
margin:12px;
}
span{color:#999;}
#box{
position:absolute;
top:50px;
left:300px;
width:100px;
height:100px;
background:red;
}
</style>
<script type="text/javascript">
window.onload = function () {
var oBox = document.getElementById("box");
var bLeft = bTop = bRight = bBottom = bCtrlKey = false; setInterval(function (){
if (bLeft){
oBox.style.left = oBox.offsetLeft - 10 + "px"
}else if (bRight) {
oBox.style.left = oBox.offsetLeft + 10 + "px"
}
if (bTop){
oBox.style.top = oBox.offsetTop - 10 + "px"
}else if(bBottom){
oBox.style.top = oBox.offsetTop + 10 + "px"
}
//防止溢出
limit();
},30); document.onkeydown = function (event)
{
var event = event || window.event;
bCtrlKey = event.ctrlKey; switch (event.keyCode) {
case 37:
bLeft = true;
break;
case 38:
//放大
if(bCtrlKey){
var oldWidth = oBox.offsetWidth;
var oldHeight = oBox.offsetHeight;
oBox.style.width = oBox.offsetWidth * 1.5 + "px";
oBox.style.height = oBox.offsetHeight * 1.5 + "px";
oBox.style.left = oBox.offsetLeft - (oBox.offsetWidth - oldWidth) / 2 + "px";
oBox.style.top = oBox.offsetTop - (oBox.offsetHeight - oldHeight) / 2 + "px";
break;
}
bTop = true;
break;
case 39:
bRight = true;
break;
case 40:
if(bCtrlKey) {
var oldWidth = oBox.offsetWidth;
var oldHeight = oBox.offsetHeight;
oBox.style.width = oBox.offsetWidth * 0.75 + "px";
oBox.style.height = oBox.offsetHeight * 0.75 + "px";
oBox.style.left = oBox.offsetLeft - (oBox.offsetWidth - oldWidth) / 2 + "px";
oBox.style.top = oBox.offsetTop - (oBox.offsetHeight - oldHeight) / 2 + "px";
break;
}
bBottom = true;
break;
case 49:
bCtrlKey && (oBox.style.background = "green");
break;
case 50:
bCtrlKey && (oBox.style.background = "yellow");
break;
case 51:
bCtrlKey && (oBox.style.background = "blue");
break;
}
return false
};
document.onkeyup = function (event) {
switch ((event || window.event).keyCode){
case 37:
bLeft = false;
break;
case 38:
bTop = false;
break;
case 39:
bRight = false;
break;
case 40:
bBottom = false;
break;
}
};
//防止溢出
function limit() {
var doc = [document.documentElement.clientWidth, document.documentElement.clientHeight]
//防止左侧溢出
oBox.offsetLeft <=0 && (oBox.style.left = 0);
//防止顶部溢出
oBox.offsetTop <=0 && (oBox.style.top = 0);
//防止右侧溢出
doc[0] - oBox.offsetLeft - oBox.offsetWidth <= 0 && (oBox.style.left = doc[0] - oBox.offsetWidth + "px");
//防止底部溢出
doc[1] - oBox.offsetTop - oBox.offsetHeight <= 0 && (oBox.style.top = doc[1] - oBox.offsetHeight + "px")
}
};
</script>
</head>
<body>
<pre>
红色方块为键盘操作区域,您可以进行如下操作:
上:↑ 下:↓ 左:← 右:→
Ctrl + 1 : 背景变为绿色
Ctrl + 2 : 背景变为黄色
Ctrl + 3 : 背景变为蓝色
Ctrl + ↑ : 放大
Ctrl + ↓ : 缩小
</pre>
<div id="box"></div>
</body>
</html>

摘自:http://www.jb51.net/article/32823.htm

JS实现用键盘控制DIV上下左右+放大缩小与变色的更多相关文章

  1. 键盘控制div上下左右移动 (转)

    <html> <head> <title></title> <link rel="stylesheet" type=" ...

  2. js键盘控制div移动,解决停顿问题

    问题版本代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <html> &l ...

  3. 键盘控制div移动并且解决停顿问题(原生js)

    <html> <head> <title>键盘控制div移动,解决停顿问题</title> <meta charset="utf-8&q ...

  4. 键盘控制div移动

    <!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...

  5. 用键盘控制DIV && Div闪烁

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 原生JS的移入溢出控制div的显示与隐藏

    原生JS的移入溢出控制div的显示与隐藏的写法 上面的写法火狐存在兼容性

  7. JS控制图片拖动 放大 缩小 旋转 支持滚轮放大缩小 IE有效

    <html> <head>     <title>图片拖动,放大,缩小,转向</title> <script type="text/ja ...

  8. html 图片在一个div中放大缩小效果

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  9. js键盘控制DIV移动

    <style type="text/css"> html,body{overflow:hidden;}body{margin:0;padding:0;}pre{colo ...

随机推荐

  1. C# 移动端与PC端的数据交互

    小记:针对目前功能越来越强大的智能手机来说,在PC端支持对手机中的用户数据作同步.备份以及恢复等保护措施的应用已经急需完善.不仅要对数据作保护,而且用户更希望自己的手机跟PC能够一体化,以及和远程服务 ...

  2. ISO9126 质量模型

    功能性 适合性:当软件在指定条件下使用,其满足明确和隐含要求功能的能力. 准确性:软件提供给用户功能的精确度是否符合目标. 互操作性:软件与其它系统进行交互的能力. 安全性:软件保护信息和数据的安全能 ...

  3. Android默认启动程序问题

    参考地址:http://www.cnblogs.com/Lewis/p/3316946.html 怎么让我们自己开发的Android程序设为默认启动呢?其实很简单,只要在AndroidManifest ...

  4. 面试前的准备---C#知识点回顾----01

    过完年来,准备找份新工作,虽然手里的工作不错,但树挪死,人挪活.咱不能一直在一个坑里生活一辈子,外面的世界毕竟是很美好的. 为了能正常的找到自己中意的工作,最近是将所有的基础知识拿出来复习了一次.仅作 ...

  5. vs2012 设计器 视图异常

    WPF 开发/(其他xaml app 的开发) 的时候 设计视图功能非常方便  不过有时候经常遇到 设计器加载失败的情况,原因很多  很多时候是代码问题导致,设计器无法展现视图,也有sliverlig ...

  6. BeeswaxException 以及其他问题

    Could not read table BeeswaxException(handle=QueryHandle(log_context='ae18ae74-518f-400b-b4b0-d399ed ...

  7. BAK文件怎么恢复到数据库中

    1.右击SQLServer2000实例下的“数据库”文件夹.就是master等数据库上一级的那个图标.选择“所有任务”,“还原数据库” 2.在“还原为数据库”中填上你希望恢复的数据库名字.这个名字应该 ...

  8. OC——NSDictionary和NSMutableDictionary

    //初始化 NSString *key1 = @"key1"; NSString *key2 = @"key2"; NSString *key3 = @&quo ...

  9. c# 使用oledb 写入导出excel设置单元格为成数字格式 设置了不起作用

    使用oledb 导出过程中,如果excel安装版本低于2010,无论怎么设置.导出的都是文本格式. 用代码-使用数据-分列,解决

  10. U - stl 的 优先队列 Ⅰ

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...