当前位置:网站首页>[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> -->
边栏推荐
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
- Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
- [groovy] loop control (number injection function implements loop | times function | upto function | downto function | step function | closure can be written outside as the final parameter)
- Linux安装Redis
- Why is this an undefined behavior- Why is this an undefined behavior?
- 有个疑问 flink sql cdc 的话可以设置并行度么, 并行度大于1会有顺序问题吧?
- Pytest (4) - test case execution sequence
- [learning notes] month end operation -gr/ir reorganization
- Redis6-01nosql database
猜你喜欢
Yyds dry goods inventory intelligent fan based on CC2530 design
Leetcode42. connect rainwater
Sqoop command
腾讯云,实现图片上传
线程基础知识
Learning notes of raspberry pie 4B - IO communication (I2C)
this+闭包+作用域 面试题
IPv6 experiment
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Yyds dry goods inventory embedded matrix
随机推荐
Kubernetes - identity and authority authentication
Apache Web page security optimization
問下,這個ADB mysql支持sqlserver嗎?
Acwing game 58 [End]
040. (2.9) relieved
1. Five layer network model
Port, domain name, protocol.
[luat-air105] 4.1 file system FS
Zero foundation uses paddlepaddle to build lenet-5 network
腾讯云,实现图片上传
C file in keil cannot be compiled
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
Pat grade a 1119 pre- and post order traversals (30 points)
ICSI213/IECE213 Data Structures
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
有个疑问 flink sql cdc 的话可以设置并行度么, 并行度大于1会有顺序问题吧?
Performance of calling delegates vs methods
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?