当前位置:网站首页>.102键盘移动div
.102键盘移动div
2022-04-22 20:56:00 【sanda_nd】
通过键盘移动红色方块
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>键盘移动div</title>
<script>
window.onload = function () {
var box = document.getElementById('box');
document.onkeydown = function (event) {
event = event || window.event;
/*
左 37
右 39
上 38
下 40
*/
var speed=10;
// ctrl 和方向键按住才有效果
if(event.ctrlKey){
speed=50;
}
switch (event.keyCode) {
case 37:
box.style.left = box.offsetLeft - speed + 'px';
break;
case 38:
box.style.top = box.offsetTop - speed+ 'px';
break;
case 39:
box.style.left = box.offsetLeft + speed+ 'px';
break;
case 40:
box.style.top = box.offsetTop + speed + 'px';
break;
}
};
};
</script>
<style>
#box {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
}
</style>
</head>
<body>
<div id="box"></div>
</body>
</html>
版权声明
本文为[sanda_nd]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sanda_nd/article/details/124343621
边栏推荐
- 【youcans 的 OpenCV 例程200篇】160. 图像处理之OTSU 方法
- L1-046 整除光棍
- SDF accelerate trace
- Redis's key and value best practices
- LeeCode 130. Surrounded area
- Consul 的使用
- MySQL主从复制之GTID复制
- MySQL开发技巧
- Huawei machine test question -- hj72 100 money to buy 100 chickens
- Ziguang Guowei: it is actively expanding the category of analog chips, and some products have been sold in batches
猜你喜欢

88 R 用户画像之线性回归逻辑回归综合实战 1

2020 team design ladder competition (part)

Detailed explanation of sorting methods (8 kinds) - bucket sorting

【数据库学习01】

Design and analysis of MySQL high availability architecture

On "waiting for awakening mechanism"

Building a new generation of computing platform, stepvr will open the "door" of metauniverse in 2022

LeeCode 130. Surrounded area

Who is important about products and services? Changan Ford tells you "all"
Ali Interviewer: you'd better not write glide on your resume. It's not as simple as asking for the source code
随机推荐
紫光国微:正积极拓展模拟芯片品类,部分产品已实现批量销售
2、 Linear regression
Leetcode daily question 396 Rotation function
Botu PLC user defined data type
Jingdong new product Intelligence Bureau spoilers oppo K10 series or linkage Guoman IP surprise?
LeeCode 130. Surrounded area
(l2-026) small generation (with weight and search set)
[dasctf OCT x] lost magic girl
产品和服务谁重要,长安福特告诉你“全都要”
MySQL高可用架构设计分析
MySQL 进阶 触发器 -- 触发器介绍、触发器语法、触发器案例
MySQL 第8章 数据库约束
中美程序员对比:你认同吗
华中师范大学少年儿童组织与思想意识教育考研上岸前辈备考经验
buuctf-[Flask]SSTI
Sword finger offer summary
MySQL advanced trigger -- trigger introduction, trigger syntax and trigger case
Leetcode question brushing Diary - Sword finger offer II 070 Sort numbers that appear only once in the array
Who is important about products and services? Changan Ford tells you "all"
CDH6. 3.2 enable Kerberos authentication