用键盘控制DIV && Div闪烁
<!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>
Div闪烁
<!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">
#box{position:absolute;top:50%;left:50%;color:#fff;width:200px;height:200px;background:red;cursor:pointer;letter-spacing:5px;text-align:center;font:12px/200px Arial;margin:-100px 0 0 -100px;}
</style>
<script type="text/javascript">
window.onload = function ()
{
var oBox = document.getElementById("box");
var timer = null; oBox.onclick = function ()
{
var i = 0;
clearInterval(timer);
timer = setInterval(function () {
oBox.style.display = i++ % 2 ? "none" : "block";
i > 6 && (clearInterval(timer))
}, 80)
}
};
</script>
</head>
<body>
<div id="box">点击我就闪</div>
</body>
</html>
用键盘控制DIV && Div闪烁的更多相关文章
- 键盘控制div移动
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- 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 ...
- JS实现用键盘控制DIV上下左右+放大缩小与变色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 键盘控制div移动并且解决停顿问题(原生js)
<html> <head> <title>键盘控制div移动,解决停顿问题</title> <meta charset="utf-8&q ...
- 键盘控制div上下左右移动 (转)
<html> <head> <title></title> <link rel="stylesheet" type=" ...
- js控制固定div和随屏滚动div兼容多浏览器和纯css控制(来自网络)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js实现键盘操作对div的移动或改变-------Day43
<爸爸去哪儿>的第二季据说要开播了额,有点小期待,不知道这一季的小宝贝们会有多萌,还会甜到心底吧, 哈哈,还记得那个风一样的女子呢,不知道她如今怎样了. 言归正传,继续今天的记录,实际上在 ...
- 鼠标滚轮控制侧边div上下翻动效果
css部分: <style> * { margin: 0; padding: 0;} .wrap { width: 1000px; margin: 0 auto; overflow: hi ...
- 用css控制一个DIV画图标。
在实际开发中,我们会用到一些小图形,图标.大多数情况下都是用图片来实现的,同时对图片进行处理使图片大小尽可能的缩小.但是图片在怎么处理也是按KB来算的.但是要是用CSS画,只要用很少的空间就能完成同样 ...
随机推荐
- Nodejs 基础知识 浅析
1. 模块化 ①常用模块化规范 CommonJS + nodejs AMD(Asynchronous Module Definition) + RequireJS CMD(Common Module ...
- Spring Cloud配置文件加载优先级简述
Spring Cloud中配置文件的加载机制与其它的Spring Boot应用存在不一样的地方:如它引入了bootstrap.properties的配置文件,同时也支持从配置中心中加载配置文件等:本文 ...
- 【ZOJ4070】Function and Function(签到)
题意:求 k 层嵌套的 f(x) 0<=x,k<=1e9 思路:迭代不会很多次后函数里就会=0或者1,再看层数奇偶直接返回答案 #include<cstdio> #includ ...
- Scaffold your ASP.NET MVC 3 project with the MvcScaffolding package
原文发布时间为:2011-05-21 -- 来源于本人的百度文章 [由搬家工具导入] http://blog.stevensanderson.com/2011/01/13/scaffold-your- ...
- [ CodeVS冲杯之路 ] P1063
不充钱,你怎么AC? 题目:http://codevs.cn/problem/1063/ 本来是想写石子合并的,结果把题目看错了,写成了合并果子…… 凑合交了上去,直接A了…… 题目将可以将任意两堆合 ...
- F28379D烧写双核程序(在线&离线)
烧写双核程序前需知在分别对F28379D的CPU1和CPU2两个核进行烧写程序时,需要在CCS中建立两个工程,独立编写两个核的程序.如controlSUITE中提供的双核程序例程: 1. 在线1.1 ...
- redis加入到Windows系统服务
1.cmd命令,到redis的安装目录输入以下命令 安装命令: redis-server.exe --service-install redis.windows.conf --loglevel ve ...
- scandir函数详解
scandir函数详解2009-10-30 10:51scandir函数:读取特定的目录数据表头文件:#include <dirent.h>定义函数:int scandir(const c ...
- 移动web开发问题和优化小结
之前在微信公众号上看到的一篇文章,直接给拷过来了....原文链接http://mp.weixin.qq.com/s/0LwTz-Mw2WumSztIrHucdQ 2.Meta标签 页面在手机上显示时, ...
- ACL权限引发的403 Forbidden
403 Forbidden查看nginx用户 getfacl 查看url实际对应的目录地址.检查权限是否授权有问题,特别是mask mask是最大权限控制查看权限是否有nginx用户以及mask权限, ...