创建一个div,在div上按键盘中的上下左右,这个div会根据你所按的键盘来进行移动,每次移动10px,上哈左右,div会有四个颜色变化,div中的蚊子随着上下左右而变化, 不能穿出图片 超出弹出提示框
3条回答 默认 最新
- CSDN专家-赖老师(软件之家) 2021-11-12 20:27关注
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>方向键移动div</title> <style> #box1 { width: 200px; height: 200px; background-color: aqua; position: absolute; } </style> <script> window.onload = function () { var b1 = document.getElementById("box1"); var distance = 10; alert("按下ctrl键加速,按下alt键减速"); document.onkeydown = function (event) { event = event || window.event; if (event.ctrlKey) { distance = 40; } if (event.shiftKey) { distance = 5; } if (event.keyCode === 37) { b1.style.left = b1.offsetLeft - distance + "px"; } else if (event.keyCode === 38) { b1.style.top = b1.offsetTop - distance + "px"; } else if (event.keyCode === 39) { b1.style.left = b1.offsetLeft + distance + "px"; } else if (event.keyCode === 40) { b1.style.top = b1.offsetTop + distance + "px"; } }; }; </script> </head> <body> <div id="box1"></div> </body>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
- ¥15 Centos / PETSc / PETGEM
- ¥15 centos7.9 IPv6端口telnet和端口监控问题
- ¥120 计算机网络的新校区组网设计
- ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
- ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
- ¥20 海浪数据 南海地区海况数据,波浪数据
- ¥20 软件测试决策法疑问求解答
- ¥15 win11 23H2删除推荐的项目,支持注册表等
- ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法