cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <!-- Main container -->
<div id="container">
		<!-- Div to be moved -->
    <div id="square">
		</div>
	</div>
            
          
!
            
              /* Box Model */
* {
  box-sizing: border-box;
}

/* Main container, with some border and centered */
#container {
  width: 100%;
  height: 150px;
  margin: 30px auto;
  border: 1px solid black;
  position: relative;
}

/* Div to be moved, placed with position absolute
inside the main container */
#square {
  width: 25px;
  height: 25px;
  background-color: red;
  position: absolute;
  left: 50%;
  top: 45%;
}

            
          
!
            
              $(function() {

 //Capturing any key event in the body 
$("body").keypress(function(event) {
					
          //Get the code of the pressed key
          var tecla = event	.which;
					console.log(tecla);
          //Get the current position
					var left =parseInt($("#square").css("left"));
					var top =parseInt($("#square").css("top"));
           
          //Get the size of the container
         var h = parseInt($("#container").css("height"));
        var w = parseInt($("#container").css("widht"));
  
          //LEFT key z code 122
					//If it can be moved within the container we animate the css properties to moved it.
           if (tecla == 122 && left > 15) {
						$("#square").animate({
							left : "-=10px"
						},200);
					}
  
          //UP key s code 115
          //If it can be moved within the container we animate the css properties to moved it.
					if (tecla == 115 && top > 10) {
						$("#square").animate({
							top : "-=10px"
						},200);
					}
  
          //DOWN key  x code 120
          //If it can be moved within the container we animate the css properties to moved it.
					if (tecla == 120 && top < (h-35)) {
						$("#square").animate({
							top : "+=10px"
						},200);
					}
  
					//RIGHT key c code 99
          //If it can be moved within the container we animate the css properties to moved it.
					if (tecla == 99 && left < (w-30)) {
						$("#square").animate({
							left : "+=10px"
						},200);
					}
				});
	  });
            
          
!
999px
Loading ..................

Console