当前位置:网站首页>[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> -->
边栏推荐
- Delphi read / write JSON format
- How to define a unified response object gracefully
- Breaking the information cocoon - my method of actively obtaining information - 3
- Ubantu disk expansion (VMware)
- Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
- Kubernetes -- cluster expansion principle
- 040. (2.9) relieved
- How to learn to get the embedding matrix e # yyds dry goods inventory #
- PHP cli getting input from user and then dumping into variable possible?
- Subversive cognition: what does SRE do?
猜你喜欢
SQL performance optimization skills
Port, domain name, protocol.
[2022 repair version] community scanning code into group activity code to drain the complete operation source code / connect the contract free payment interface / promote the normal binding of subordi
Huawei MPLS experiment
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
Tiny series rendering tutorial
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
[安洵杯 2019]不是文件上传
IPv6 experiment
Devtools的簡單使用
随机推荐
Accuracy problem and solution of BigDecimal
qrcode:将文本生成二维码
001 chip test
Use of kubesphere configuration set (configmap)
Ubantu disk expansion (VMware)
Cette ADB MySQL prend - elle en charge SQL Server?
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
SQL injection exercise -- sqli Labs
Sqoop installation
Kubernetes -- cluster expansion principle
Kubernetes - identity and authority authentication
Comparison of advantages and disadvantages between platform entry and independent deployment
How can we truncate the float64 type to a specific precision- How can we truncate float64 type to a particular precision?
ICSI213/IECE213 Data Structures
Usage scenarios and solutions of ledger sharing
线程基础知识
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Pdf things
2021 Li Hongyi machine learning (3): what if neural network training fails
SFTP cannot connect to the server # yyds dry goods inventory #