Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

67
Views
Element not moving to the right location after setting position

I am using this code to move an element around based on keydown event:

$("body").on("keydown", function(event) {
    var arrow_position = $(".down-arrow").position();

    if(event.key == 's') {
        $(".down-arrow").css({"top": arrow_position.top - 10});
    }
    if(event.key == 'w') {
        $(".down-arrow").css({"top": arrow_position.top + 10});
    }
    if(event.key == 'a') {
        $(".down-arrow").css({"left": arrow_position.left - 10});
    }
    if(event.key == 'd') {
        $(".down-arrow").css({"left": arrow_position.left + 10});
    }
});
body {
  font-family: var(--font-family);
  font-size: 4rem;
  overflow: hidden;
  position: relative;
  /* width: 1920px; */
  /* height: 1080px; */
}

.down-arrow {
  left: 58%;
  position: absolute;
  bottom: 5%;
  border: 1px solid red;
  height: 50px;
  width: 50px;
  transform: rotate(170deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="down-arrow">

</div>

The element is directly inside the body.

However, the element only moves left when I press either a or d. Similarly, it only moves up when I press s or w. It also moves in increments of 20 instead of 10. What am I doing wrong?

Thanks.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!