Chatbox

You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Only 1 of 2 moves on 13/6/2012, 9:50 pm

grimofdoom

avatar

Member
I got a javascript code to make a div move, i made one move, but i need the second one moving,can someone help me?

Code:

<HTML>
<HEAD>
<TITLE>Cursor Mover</TITLE>
<SCRIPT>
var ie = (document.all && !window.opera)?1:0;
if(!ie)
{
   var e=document.captureEvents(Event.KEYDOWN)
}
window.onload=function()
{
   if(!ie)
   {
      document.onkeydown=function(e)
      {
         move(e);
      }
   }
   else
   {
      document.onkeydown=function()
      {
         move();
      }
   }   
}
function move(e)
{
      if(ie)
      {
         ek = window.event.keyCode;
      }
      else
      {
         var event=e;
         var ek = event.keyCode ? event.keyCode : event.which ?

event.which : event.charCode;   
      }
      if (ek==37) document.getElementById('DIV1','DIV2').style.left=

(document.getElementById('DIV1','DIV2').style.left.replace('px','')*1)-7;
      if (ek==39) document.getElementById('DIV1','DIV2').style.left=

(document.getElementById('DIV1','DIV2').style.left.replace('px','')*1)+7;
      if (ek==38) document.getElementById('DIV1','DIV2').style.top=

(document.getElementById('DIV1','DIV2').style.top.replace('px','')*1)-7;
      if (ek==40) document.getElementById('DIV1','DIV2').style.top=

(document.getElementById('DIV1','DIV2').style.top.replace('px','')*1)+7;
}

</SCRIPT>
<style type=text/css>
.char{background-color:white;border:1px solid

black;height:50px;width:50px;position:absolute;-webkit-animation:char 1s infinite;}

@-webkit-keyframes char{

0%{background-color:white;}
25%{background-color:lightgrey;box-shadow:0px 0px 8px 2px lightgrey;}
50%{background-color:grey;box-shadow:0px 0px 8px 2px grey}
75%{background-color:lightgrey;box-shadow:0px 0px 8px 2px lightgrey;}
100%{background-color:white;}

}
</style>
</HEAD>
<BODY>
<DIV ID="DIV1" class=char style="left:200px;top:100px;border-radius:45%;";></DIV>
<DIV ID="DIV2" class=char style="left:100px;top:100px;border-radius:45%;";></DIV>
</BODY>
</HTML>

View user profile

2 Re: Only 1 of 2 moves on 13/6/2012, 9:56 pm

Drogba921

avatar

Administrator
use classes, it might help. Dunno what you're really trying to do

View user profile http://www.codinghelp.org

3 Re: Only 1 of 2 moves on 14/6/2012, 5:30 am

Vanilla990

avatar

Administrator
From looking at the code, it seems you want to move the div around the screen based on which arrow key is being pushed down but can only get 1 div to move?

What you could do would be to effectively copy and paste the javascript and have 1 of the circles controlled by the normal up,down keys and have the other one controlled by the W,A,S,D keys.

View user profile

4 Re: Only 1 of 2 moves on 14/6/2012, 2:16 pm

LGforum

avatar

Moderator
Your Javascript isn't working because of these lines:
Code:
document.getElementById('DIV1','DIV2')
You can only specify one ID string in the getElementById method.

View user profile

5 Re: Only 1 of 2 moves on 15/6/2012, 4:05 pm

grimofdoom

avatar

Member
oh,well,i dont need this anymore, i figured out a much easyer way to do it, i just put them in a div and moved the div that held the other 2 and they stay in place on movement.

View user profile

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum