//用鍵盤控制DIV,紅色方塊為鍵盤操作區域,您可以進行如下操作:左右控制;背景變為綠色;背景變為黃色;背景變為藍色放大或縮小
用鍵盤控制DIV,紅色方塊為鍵盤操作區域,您可以進行如下操作:
上:↑ 下:↓ 左:← 右:→
Ctrl + 1 : 背景變為綠色
Ctrl + 2 : 背景變為黃色
Ctrl + 3 : 背景變為藍色
Ctrl + ↑ : 放大
Ctrl + ↓ : 縮小
<!--
Author: XiaoWen
Create a file: 2017-01-10 19:00:30
Last modified: 2017-01-12 13:19:28
Start to work:
Finish the work:
Other information:
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box{
width:80px;
height:80px;
background:#f00;
position:absolute;
}
</style>
</head>
<body>
<pre>
移動:上↑ 下↓ 左← 右→
顏色:
ctrl + 1 綠色
ctrl + 2 黃色
ctrl + 3 藍色
大小:
ctrl + ↑ 放大
ctrl + ↓ 縮小
<!-- 重置:Ctrl + Enter -->
</pre>
<div id="box"></div>
</body>
<script>
var box=document.getElementById('box');
onkeydown=function(){
if(ev(event).ctrlKey){
var code=ev(event).keyCode
code==49 ? box.style.background='#0f0' : ''
code==50 ? box.style.background='#ff0' : ''
code==51 ? box.style.background='#00f' : ''
code==38 ? resize('big') : ''
code==40 ? resize('small') : ''
code==13 ? console.log('重置') : ''
function resize(re){
switch(re){
case 'big':
box.style.width=box.offsetWidth+10+'px';
box.style.height=box.offsetHeight+10+'px';
break
case 'small':
box.style.width=box.offsetWidth-10+'px';
box.style.height=box.offsetHeight-10+'px';
break
}
}
}else{
var code=ev(event).keyCode
code==37 ? box.style.left=box.offsetLeft-10+'px' : ''
code==38 ? box.style.top=box.offsetTop-10+'px' : ''
code==39 ? box.style.left=box.offsetLeft+10+'px' : ''
code==40 ? box.style.top=box.offsetTop+10+'px' : ''
}
}
function ev(ev){
var e=ev||event;
return e
}
</script>
</html>
