Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4045772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:28:37+00:00 2026-05-20T13:28:37+00:00

Ok so I made a page that can move an image with the arrow

  • 0

Ok so I made a page that can move an image with the arrow keys but only the down and right functions work. I made the functions from a hide show script because i’m very new to javascript.

Here is the code.

left

function left(id) { 
    var y = '5';
    var z =document.getElementById(id).style.left;
    var x = parseInt(y)+parseInt(z);
    var state = document.getElementById(id).style.left;
        if (state == '1') {
            document.getElementById(id).style.left = x;
        } else {
            document.getElementById(id).style.left = x;
        }
    }

right

function right(id) { 
    var y = '5';
    var z =document.getElementById(id).style.right;
    var x = parseInt(y)+parseInt(z);
    var state = document.getElementById(id).style.right;
        if (state == '1') {
            document.getElementById(id).style.right = x;
        } else {
            document.getElementById(id).style.right = x;
        }
    }

up

function up(id) { 
    var y = '5';
    var z =document.getElementById(id).style.bottom;
    var x = parseInt(y)+parseInt(z);
    var state = document.getElementById(id).style.bottom;
        if (state == '1') {
            document.getElementById(id).style.bottom = x;
        } else {
            document.getElementById(id).style.bottom = x;
        }
    }

down

function down(id) { 
    var y = '5';
    var z =document.getElementById(id).style.top;
    var x = parseInt(y)+parseInt(z);
    var state = document.getElementById(id).style.top;
        if (state == '1') {
            document.getElementById(id).style.top = x;
        } else {
            document.getElementById(id).style.top = x;
        }
    }

then the arrow key script

document.onkeyup = KeyCheck;       
function KeyCheck() {

 var KeyID = event.keyCode;

  switch(KeyID)
  {

  case 37:

  right('img');

  break;

  case 38:

  up('img')

  break

  case 39:

  left('img');

  break;

  case 40:

  down('img');

  break;
  }
  }

and then the html

<img id="img" src="http://trevorrudolph.com/logo.png" style="position:absolute; left:1; bottom:1; right:1; top:1;">

you can download the html at arrow.zip

the actual page is here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:28:37+00:00Added an answer on May 20, 2026 at 1:28 pm

    You should only be positioning it with top and left, because if you try to do it the way you’re doing it, you are going to end up with very messed up properties, such as a top of say 5 but a bottom of say 100.

    Also, you should use a unit, preferable pixels, for the top and left properties.

    So, all you really need to do is change your functions to look like:

    function left(id) {
        document.getElementById(id).style.left.match(/^([0-9]+)/);
        var current = RegExp.$1; // get just the number and not the units
        document.getElementById(id).style.left = current - 1 + 'px'; // taking advantage of JavaScript's strange but sometimes useful type conversion. The subtraction converts it to an int and the addition converts it back to a string. 
    }
    
    function right(id) {
        document.getElementById(id).style.left.match(/^([0-9]+)/);
        var current = RegExp.$1;
        document.getElementById(id).style.left = parseInt(current) + 1 + 'px'; // here we can't use that trick
    }
    
    function up(id) {
        document.getElementById(id).style.top.match(/^([0-9]+)/);
        var current = RegExp.$1;
        document.getElementById(id).style.top = current - 1 + 'px';
    }
    
    function down(id) {
        document.getElementById(id).style.top.match(/^([0-9]+)/);
        var current = RegExp.$1;
        document.getElementById(id).style.top = parseInt(current) + 1 + 'px';
    }
    

    Also, you should be using <script type="text/javascript"> instead of <script language="JavaScript">. The latter form is deprecated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`v made my webpart that works well (I can add it to page and
Had a page that was working fine. Only change I made was to add
ReSharper likes to point out multiple functions per ASP.NET page that could be made
I made a new page in our website that needs to access some functions
I'm debugging someone else's code for a web page that is made with ASP.NET
I'm kind of lost. I made a sort of 'master page' that I want
i made an HTML page with all widths and heights as percentage that's primary
I have made a web application that uses master page for Login & Logout
I have a Web Application that I'm trying to move from Sun Application Server
I have a -link element that has href link to page, but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.