Javascript can't acces to HTML(moving object left-right)

user6101277

my file:

index.html
client
   css
     style.css
   js
     javascript.js

index.html:

<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>blablabla</title>
<link rel="stylesheet" type="text/css" href="client/css/style.css">
<script type="text/javascript" src="client/js/javascript.js"></script>

</head>
<body>
<div id="container">
     <div id="guy"></div>
</div>

</body>
</html>

javascript.js

var guy=document.getElementById('guy');
var container=document.getElementById('container');

var guyLeft=0;

function anim(e){
if(e.keyCode==39){
    guyLeft +=2;
    guy.style.left = guyLeft + 'px';

}
if(e.keyCode==37){
    guyLeft -=2;
    guy.style.left = guyLeft + 'px';
}   
}
document.onkeydown =anim;

style.css

#container{
height:400px;
width:600px;
outline:1px solid black;
position: relative;
}
#guy{
position: absolute;
height:50px;
width:50px;
outline:1px solid black;
background-color:red;
left:0;
}

1)So guys pls help.. I just want to move object left and right .. when i put that code between in index.html it works but in seperate javascript.js class doesn't work. Thx for help.

Francesco

1) You have to load the JavaScript at the bottom of the page.

<!doctype html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>blablabla</title>
        <link rel="stylesheet" type="text/css" href="client/css/style.css">
    </head>
    <body>
        <div id="container">
            <div id="guy"></div>
        </div>
        <script type="text/javascript" src="client/js/javascript.js"></script>
    </body>
</html>

2) Move up and down:

var guy = document.getElementById('guy');
var container = document.getElementById('container');

var guyLeft = 0;
var guyTop = 0;

function anim(e){
    if(e.keyCode==39){
        guyLeft +=2;
        guy.style.left = guyLeft + 'px';

    }
    if(e.keyCode==37){
        guyLeft -=2;
        guy.style.left = guyLeft + 'px';
    }

    // UP
    if(e.keyCode==38) {
        guyTop -=2;
        guy.style.top = guyTop + 'px';
    }

    // DOWN
    if(e.keyCode==40) {
        guyTop +=2;
        guy.style.top = guyTop + 'px';
    }
}

document.onkeydown = anim;

You should use the console if you want to catch bugs and errors in JavaScript.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript can't acces to HTML(moving object left-right)

From Dev

Can't toggle from right to left Jquery

From Dev

Cannot acces object from javascript

From Dev

C# After passing an object from window window I can´t acces object properties

From Dev

Java-Chars can rotate left but can't rotate to the right?

From Dev

Java-Chars can rotate left but can't rotate to the right?

From Dev

why the right button can't on the right of parent like left button on the left of parent

From Dev

Can javascript tell the difference between left and right shift key?

From Dev

Can't close NavigationDrawer with right-to-left swipe

From Dev

Why can't VLOOKUP function right-to-left?

From Dev

Jquery UI can't drag from left to right side

From Dev

Why I can't left div height to be same right div

From Dev

Can't move/snap libreoffice window to occupy left or right half

From Dev

Can't acces Sort property in a telerik:RadComboBox

From Dev

I can`t get acces to myArray

From Dev

Can't insert into acces db with .net

From Dev

why i can´t acces to makeRange method

From Dev

nodeJs - can't acces required properties

From Dev

How can I acces the N'th object property name?

From Dev

How can I acces the N'th object property name?

From Dev

SoftException in Application.cpp:249: can't acces file

From Dev

I can't acces the user's jEditorPane from another class

From Dev

SoftException in Application.cpp:249: can't acces file

From Dev

Why can't my phone acces my router on WDS mode?

From Dev

Lazarus Pascal -- Class methods can't acces to private members

From Dev

compare-object left or right side only

From Dev

Can't scroll up/down and scroll left/right at the same time on UITableView within a UIScrollView

From Dev

Can't set elements properly in 100% mid and static size left and right fashion

From Dev

My JavaScript image gallery slider's right slider isn't working and my left one is

Related Related

  1. 1

    Javascript can't acces to HTML(moving object left-right)

  2. 2

    Can't toggle from right to left Jquery

  3. 3

    Cannot acces object from javascript

  4. 4

    C# After passing an object from window window I can´t acces object properties

  5. 5

    Java-Chars can rotate left but can't rotate to the right?

  6. 6

    Java-Chars can rotate left but can't rotate to the right?

  7. 7

    why the right button can't on the right of parent like left button on the left of parent

  8. 8

    Can javascript tell the difference between left and right shift key?

  9. 9

    Can't close NavigationDrawer with right-to-left swipe

  10. 10

    Why can't VLOOKUP function right-to-left?

  11. 11

    Jquery UI can't drag from left to right side

  12. 12

    Why I can't left div height to be same right div

  13. 13

    Can't move/snap libreoffice window to occupy left or right half

  14. 14

    Can't acces Sort property in a telerik:RadComboBox

  15. 15

    I can`t get acces to myArray

  16. 16

    Can't insert into acces db with .net

  17. 17

    why i can´t acces to makeRange method

  18. 18

    nodeJs - can't acces required properties

  19. 19

    How can I acces the N'th object property name?

  20. 20

    How can I acces the N'th object property name?

  21. 21

    SoftException in Application.cpp:249: can't acces file

  22. 22

    I can't acces the user's jEditorPane from another class

  23. 23

    SoftException in Application.cpp:249: can't acces file

  24. 24

    Why can't my phone acces my router on WDS mode?

  25. 25

    Lazarus Pascal -- Class methods can't acces to private members

  26. 26

    compare-object left or right side only

  27. 27

    Can't scroll up/down and scroll left/right at the same time on UITableView within a UIScrollView

  28. 28

    Can't set elements properly in 100% mid and static size left and right fashion

  29. 29

    My JavaScript image gallery slider's right slider isn't working and my left one is

HotTag

Archive