当前位置:网站首页>[move pictures up, down, left and right through the keyboard in JS]
[move pictures up, down, left and right through the keyboard in JS]
2022-07-05 03:33:00 【renrenrenrenqq】
js Move pictures up, down, left and right through the keyboard
The first method
<!-- <!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<style>
</style>
<body>
<img style="left:0px" src="./html-01/images/ house .png" alt="">
<script>
let img=document.querySelector('img')
document.onkeyup = function(event){
if (event.keyCode==37) {
img.style.left = img.offsetLeft-20+"px";
}else if (event.keyCode==38){
img.style.top = img.offsetTop-20+"px";
}else if (event.keyCode==39){
img.style.left = img.offsetLeft+20+"px";
}else if (event.keyCode==40){
img.style.top = img.offsetTop+20+"px";
}
} -->
<!-- </script>
</body> -->
<!-- </html>
The second method ,
<!DOCTYPE html>
<html lang="en">
<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>Document</title>
</head>
<style>
img{
position: absolute;
display: none;
transition: display 2s;
}
</style>
<body>
<img style="left:0px;top: 0px;" src="./html-01/images/ house .png" alt=""> -->
<!-- <script>
document.onkeyup=function(event){
console.dir(event.keyCode)
if(event.keyCode==39){
let img=document.querySelector('img')
let currentLeft=img.style.left;
let index=currentLeft.lastIndexOf('px')
currentLeft=currentLeft.substring(0,index)
img.style.left=(parseInt(currentLeft)+30)+'px'}
else if(event.keyCode==40){
let img=document.querySelector('img')
let currenttop=img.style.top;
let index=currenttop.lastIndexOf('px')
currenttop=currenttop.substring(0,index)
img.style.top=(parseInt(currenttop)+30)+'px'
}
else if(event.keyCode==37){
let img=document.querySelector('img')
let currentLeft=img.style.left;
let index=currentLeft.lastIndexOf('px')
currentLeft=currentLeft.substring(0,index)
img.style.left=(parseInt(currentLeft)-30)+'px'
} else if(event.keyCode==38){
let img=document.querySelector('img')
let currenttop=img.style.top;
let index=currenttop.lastIndexOf('px')
currenttop=currenttop.substring(0,index)
img.style.top=(parseInt(currenttop)-30)+'px'
}
} -->
<!-- </script> -->
边栏推荐
- Redis6-01nosql database
- Daily question 2 12
- Leetcode92. reverse linked list II
- PHP cli getting input from user and then dumping into variable possible?
- Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
- Smart pointer shared_ PTR and weak_ Difference of PTR
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Pat class a 1160 forever (class B 1104 forever)
- How to define a unified response object gracefully
- SQL performance optimization skills
猜你喜欢
this+闭包+作用域 面试题
Huawei MPLS experiment
SQL performance optimization skills
1. Five layer network model
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Pdf things
[105] Baidu brain map - Online mind mapping tool
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
Azkaban安装部署
Mongodb common commands
随机推荐
Talk about the SQL server version of DTM sub transaction barrier function
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
Solve the problem that sqlyog does not have a schema Designer
[105] Baidu brain map - Online mind mapping tool
Kuboard
Technology sharing swift defense programming
Daily question 2 12
Basic authorization command for Curl
Nmap使用手册学习记录
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Necessary fonts for designers
SQL performance optimization skills
Comparison of advantages and disadvantages between platform entry and independent deployment
Redis6-01nosql database
SQL injection exercise -- sqli Labs
SPI and IIC communication protocol
[groovy] string (string type variable definition | character type variable definition)
Zero foundation uses paddlepaddle to build lenet-5 network
How to define a unified response object gracefully
Smart pointer shared_ PTR and weak_ Difference of PTR