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

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

我有 2 个 div(#1 和 #2)和两个按钮。单击按钮时,它应该 select 一个 div 移动,当按下箭头键时,这个 div 应该移动。另一个应该留在原处。如果单击另一个按钮,先前移动的 div 应保留在其新位置,新 selected 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"
        }
            }
    }

这是一个解决方案。我对这些值进行了一些编辑,但您可以轻松地将它们改回来。

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>