简体   繁体   English

如何使用箭头键移动特定的 div(由单击的按钮指定)

[英]How to make a specific div move with arrow keys (specified by button clicked)

I have 2 divs (#1 and #2) and two buttons.我有 2 个 div(#1 和 #2)和两个按钮。 When a button is clicked, it should select a div to move and when the arrow keys are pressed, this div should move.当一个按钮被点击时,它应该选择一个 div 来移动,当箭头键被按下时,这个 div 应该移动。 The other should remain in its place.另一个应该留在它的位置。 If the other button is clicked, the div that had previously moved should remain in its new position and the newly selected div should move along with the arrow keys.如果单击另一个按钮,则先前移动的 div 应保留在其新位置,而新选择的 div 应随箭头键移动。

.counter {
                border-radius:50%;
                width:20px;
                height:20px;
                position:absolute;
                transition:top linear 0.6s, left linear 0.6s;
                font-size:20px;
                font-weight:bold;
                text-align:center;
                padding:20px;
                top: 525px;
                left: 60px;
                background-color:red;
            }
<button onclick="one">Move One</button>
           <button onclick="two">Move Two</button>
           <div class="counter" id="1" >1</div>
           <div class="counter" id="2">2</div>
 var go = "1"
               function one() {
                   go = "1"
               }
               function two() {
                   go = "2"
               }
document.onkeydown = detectKey;
        function detectKey(e) {
    
        var posLeft = document.getElementById('').offsetLeft
        var posTop = document.getElementById('').offsetTop
        
            if (e.keyCode == '39') {
                if (go === "1") {
        document.getElementById('1').style.left  = (posLeft+150)+"px"
        }  
            if (e.keyCode == '38') {
        document.getElementById('1').style.top  = (posTop-150)+"px"
        }
            }
            
            if (e.keyCode == '39') {
                if (go === "2") {
        document.getElementById('2').style.left  = (posLeft+150)+"px"
        }  
            if (e.keyCode == '38') {
        document.getElementById('2').style.top  = (posTop-150)+"px"
        }
            }
    }

Here is a solution.这是一个解决方案。 I have edited the values a bit, but you can easily change them back.我对这些值进行了一些编辑,但您可以轻松地将它们改回来。

 const buttonOne = document.getElementById('button-one'); const buttonTwo = document.getElementById('button-two'); const elementOne = document.getElementById('one'); const elementTwo = document.getElementById('two'); buttonOne.addEventListener('click', clickOnButtonOne); buttonTwo.addEventListener('click', clickOnButtonTwo); let selectedElement = null; function clickOnButtonOne() { selectedElement = elementOne; } function clickOnButtonTwo() { selectedElement = elementTwo; } document.onkeydown = detectKey; function detectKey(e) { if (selectedElement) { if (e.keyCode == '39') { var posLeft = selectedElement.offsetLeft selectedElement.style.left = (posLeft + 50) + "px" } if (e.keyCode == '38') { var posTop = selectedElement.offsetTop selectedElement.style.top = (posTop - 50) + "px" } } }
 .counter { border-radius: 50%; width: 20px; height: 20px; position: absolute; transition: top linear 0.6s, left linear 0.6s; font-size: 20px; font-weight: bold; text-align: center; padding: 20px; top: 500px; left: 60px; background-color: red; }
 <button id="button-one" onclick="one">Move One</button> <button id="button-two" onclick="two">Move Two</button> <div class="counter" id="one">1</div> <div class="counter" id="two">2</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM