JS代码练习之键盘事件
来源:互联网 发布:行爱交流软件 编辑:程序博客网 时间:2024/06/13 21:57
今天看了网上的一个教学视频,感觉还不错,又把之前学的js基础复习了一点(已经半学期没碰了)。
下面附上代码吧
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>键盘事件联系</title> <style> *{ padding: 0; margin: 0; } html{ height: 100%; } body{ height: 100%; background: url('images/bg.jpg'); background-size: 100% 100%; } #site{ position: relative; height: 100%; } img{ position: absolute; width: 200px; height: 300px; bottom: 50px; } </style></head><body> <div id="site"> <img src="images/YAGAMI/stand.gif" id="MC"> </div> </body> <script type="text/javascript"> var imgObj=document.getElementById("MC"); var left=0; var Game=new game(imgObj); document.onkeydown=key; function key(e){ var e=e||window.event; Game.keydown(e.keyCode); this.onkeyup=function(){ Game.keyup(); this.onkeydown=key; } document.onkeydown=null; } function game(obj){ this.left=0; this.timer=null; this.active={ "39":function(){ obj.src="images/YAGAMI/advance.gif"; this.timer=setInterval(function(){ this.left+=5; obj.style.left=this.left + "px"; }.bind(this),1000/60); //bind()方法低版本浏览器不支持。 }, "37":function(){ obj.src="images/YAGAMI/retreat.gif"; this.timer=setInterval(function(){ this.left-=5; obj.style.left=this.left+"px"; }.bind(this),1000/60); }, "40":function(){ obj.src="images/YAGAMI/7490881c38cfba5ef724e4ba.gif"; }, "stop":function(){ obj.src="images/YAGAMI/stand.gif"; } }; } game.prototype.keydown=function(keyCode){ for(var key in this.active){ if(key==keyCode){ this.active[key].call(this); } } } game.prototype.keyup=function(){ clearInterval(this.timer); this.active.stop(); } </script></html>
代码中所用到的图片有
阅读全文
0 0
- JS代码练习之键盘事件
- js键盘事件代码
- js监听键盘事件代码
- js监听键盘事件示例代码
- 键盘事件onkeyup练习
- [js点滴]JavaScript之键盘事件05
- js和jquery实现监听键盘事件示例代码
- js监听键盘事件
- js 键盘事件
- JS键盘事件
- JS键盘事件
- js处理键盘事件
- 判断JS键盘事件
- js 控制键盘事件
- js获取键盘事件
- js键盘触发事件
- js 键盘事件
- JS处理键盘事件
- Python相关文章索引(10)
- 2017-07-14:Tarjan(受欢迎的牛)
- log4j配置使用教程
- Runtime 创建动态一个类 添加成员变量 属性
- Spring配置文件<context:property-placeholder>标签使用漫谈
- JS代码练习之键盘事件
- 什么叫做「数据驱动方法」
- Computing Euler angles from a rotation matrix (翻译)
- execjs RSA
- oracle 删除外键 删除所有表
- AI圣经!《深度学习》中文版隆重上市!美国亚马逊AI领域排名第一畅销书!
- MySQL insert技巧
- Android上Java程序和Opengl通信方式和opengl es着色器
- rails链接js/css