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 7702415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:13:19+00:00 2026-05-31T23:13:19+00:00

I have an image that I want to move. I can move the element

  • 0

I have an image that I want to move. I can move the element with javascript if I have the HTML below:

<html>
<head>
    <title>Snake</title>
    <script type="text/javascript" src="snake.js"></script>
    <!-- <link type="text/css" href="snake.css"> -->
</head>
<body onKeyPress="ProcessKeypress(event);">
    <p><img id="snake" style="z-index: 0; left: 300px; position: absolute; top: 250px" 
    float=top border=0 hspace=0 src="snake.gif"></p>
</body>

However I want to stye the image element using CSS. When I do this my code to move the image does not work. HTML and CSS below are what I would like to use.

<html>
<head>
    <title>Snake</title>
    <script type="text/javascript" src="snake.js"></script>
    <link type="text/css" href="snake.css">
</head>
<body onKeyPress="ProcessKeypress(event);">
    <p><img id="snake" src="snake.gif"></p>
</body>

@CHARSET "UTF-8";
img {
z-index: 0;
left: 300px; 
position: absolute; 
top: 250px;
float: top;
border: 0;
hspace: 0;
}

JavaScript below is what I am using to move the image. Any and all help appreciated.

function moveObj(name, Xpix, Ypix) 
{    
    obj = document.getElementById(name);

    px = parseInt(obj.style.left) + Xpix;       
    py = parseInt(obj.style.top) + Ypix;
    obj.style.left = px;
    obj.style.top = py;
}

function ProcessKeypress(e)
{
    var myObj = 'snake';
    var moveBy = 10;

    if(e.keyCode === 97) {
        moveObj(myObj, -moveBy, 0);
    }
    else if(e.keyCode === 100) {
        moveObj(myObj, moveBy, 0);
    }
    else if(e.keyCode === 115) {
        moveObj(myObj, 0, moveBy);
    }
    else if(e.keyCode === 119) {
        moveObj(myObj, 0, -moveBy);
    }
}
  • 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-31T23:13:20+00:00Added an answer on May 31, 2026 at 11:13 pm

    obj.style.left accesses the style properties defined in inline only.

    UPDATE

    Here is your pure JavaScript Solution

    Function: getStyleProp() to get the current applied style [Source]

    function getStyleProp(elem, prop){
        if(window.getComputedStyle)
            return window.getComputedStyle(elem, null).getPropertyValue(prop);
        else if(elem.currentStyle) return elem.currentStyle[prop]; //IE
    }
    

    Function: moveObj()

    function moveObj(obj, Xpix, Ypix) {  
        obj.style.left = parseInt(getStyleProp(obj,'left')) + Xpix+"px";
        obj.style.top= parseInt(getStyleProp(obj,'top')) + Ypix + "px";
    }
    

    Function: ProcessKeypress()

    function ProcessKeypress(e) {
        var myObj = document.getElementById("snake");
        var moveBy = 10;
        switch(e.charCode) {
            case 97: moveObj(myObj, -moveBy, 0); break;
            case 100: moveObj(myObj, moveBy, 0); break;
            case 115: moveObj(myObj, 0, moveBy); break;
            case 119: moveObj(myObj, 0, -moveBy); break;            
        }
    }    
    

    I also solved your case using jQuery: Demo

    Function moveObj

    function moveObj(obj, Xpix, Ypix) {    
        obj.css('left', parseInt(obj.css('left')) + Xpix);       
        obj.css('top', parseInt(obj.css('top')) + Ypix);
    }
    

    Function ProcessKeypress

    function ProcessKeypress(e) {
        var myObj = $("#snake"); //It will be better if it is cached
        var moveBy = 10;
        switch(e.charCode) { //used Switch for better manageability
            case 97: moveObj(myObj, -moveBy, 0); break;
            case 100: moveObj(myObj, moveBy, 0); break;
            case 115: moveObj(myObj, 0, moveBy); break;
            case 119: moveObj(myObj, 0, -moveBy); break;            
        }
    }
    

    Event Trigger attached to document instead

    $(document).on('keypress', ProcessKeypress);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have something like a big still image that you can move
I have a canvas in which I have an image. I can move that
i have a problem with my script that use canvas. I want to move
I have an image that I want to position in the right and repeat
I have an image that I want to resize on my websites rendering using
I am using GWT 2.03 and am have an image that I want to
I have an image that is too big and I want to put it
I have emoticons in a css sprite image that I want to display within
I have a 16 bit grayscale image that I want to display using WPF
I want to be able to take an image that i have already captured

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.