javascript - Moving Item from arrows -
the following code should make controller html element arrows.
- right arrow should make move right;
- left arrow should stop car.
movedown()
function should check if car moving , stop move , make move down , moveup()
should same process move up; problem movedown()
, moveup()
not working ,could me ? thanks!
<!doctype html> <html> <body > <p id="demo">a function triggered when user pressing key in input field.</p> <input type="text" id="car" style="position:relative;"> <script> document.addeventlistener("keydown", myfunction); var left=0; var interval=0; var down=0; var up=0; function myfunction(e) { var car=document.getelementbyid("car"); //move right if(e.keycode == 39){ moveright(car);} //stop move if(e.keycode == 37){stopmove(car);} //move down if(e.keycode ==40){movedown(car)}; //move if(e.keycode==38){moveup(car)}; } //move car function moveright(car){ //check if car not moving if(interval==0){ interval=window.setinterval(function(){ left+=2; car.style.left=left+"px";}, 30);} } //stop move function stopmove(car){ clearinterval(interval); //reset interval 0 interval=0;} //move down function moveup(car){ interval=window.setinterval(function(){ down+=2; car.style.bottom=down+"px"; },30); } //move function movedown(car){ interval=window.setinterval(function(){ down+=2; car.style.bottom=-down+"px"; },30); } </script> </body> </html>
Comments
Post a Comment