当前位置:网站首页>[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> -->
原网站

版权声明
本文为[renrenrenrenqq]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140734192703.html