<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
#ren{width:50px;height:50px;position:absolute;background-color:blue;z-index:3;left:100px;top:0px}
#zd{width:50px;height:50px;position:absolute;background-color:red;z-index:2}
</style>
<script>
var a = new Array();
var x = 0, y = 1;
var fx = 4;
function yidong(e)
{
var ren = document.getElementById("ren");
if(e.keyCode==37)
{
ren.style.left = ren.offsetLeft - 51 + "px";
y--;
fx = 1;
}
if (e.keyCode == 38) {
ren.style.top = ren.offsetTop - 51 + "px";
x--;
fx = 2;
}
if (e.keyCode == 39) {
ren.style.left = ren.offsetLeft +51 + "px";
y++;
fx = 3;
}
if (e.keyCode == 40) {
ren.style.top = ren.offsetTop +51 + "px";
x++;
fx = 4;
}
if(e.keyCode==65)
{
var zd = document.createElement("div");
zd.id = "zd";
zd.style.left = ren.offsetLeft +"px";
zd.style.top = ren.offsetTop + "px";
document.body.appendChild(zd);
zdyd(zd,fx);
}
}
function zdyd(a,f)
{
setInterval(function () {
if (f == 1) {
a.style.left = a.offsetLeft - 51 + "px";
}
if (f == 2) {
a.style.top = a.offsetTop - 51 + "px";
}
if (f == 3) {
a.style.left = a.offsetLeft + 51 + "px";
}
if (f == 4) {
a.style.top = a.offsetTop + 51 + "px";
}
},100)
}
document.onkeydown = yidong;
</script>
</head>
<body>
<div id="ren"></div>
</body>
</html>
解决方案 »
- JSP 批量插入数据库问题,新手求助
- 页面中有二个文本框,如果在一加载页面时,焦点自动在第一个文本框内闪烁?
- getElementById失效,变成byName了.疑惑中,高手进---
- lytebox的窗口可以按照百分比定义吗?
- 如何安全防范站外表单提交?[高手进来讨论]
- 关于超链接,如何实现只针对图片有内容的区域可点,而透明部分不可点
- 求助:只能输入汉字,数字,英文大小写,符号只允许,。!
- 验证码输入框,自动变成英文是怎么弄的
- body.find('>textarea');是什么意思?
- 有没有办法获得刚刚用execCommand()创建的element的指针?
- ExtJS使用本地JSON建树,重复加载
- js json
要只有按了a键才创建红色方块