Sei sulla pagina 1di 3

Efecto mover imagen con el teclado

mediante javascript
10/08/2014 Autor: Martin

Codigo javascript que permite desplazar una imagen cualquiera,


mediante el teclado.

1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3

<html>
<title>efecto mover imagen con el teclado</title>
<HEAD>
<script type="text/javascript">
<!-- Original: Massimo Giari (motore@iol.it) -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
n = (document.layers) ? 1 : 0;
ie = (document.all) ? 1 : 0;
function moveImage() {
if (n) {
block = document.blockDiv;
}
if (ie) {
block = blockDiv.style;
}
block.xpos = parseInt(block.left);
block.active = 0;
document.onkeydown = keyDown;
document.onkeyup = keyUp;
if (n) {
document.captureEvents(Event.keydown | Event.keyup);
}
}
function keyDown(e) {
if (n) {
var nKey = e.which;
var ieKey = 0;
}
if (ie) {
var ieKey = event.keyCode;
var nKey = 0;
}
if ((nKey == 97 || ieKey == 65) && !block.active) {
block.active = 1;
slideLeft();
}
if ((nKey == 100 || ieKey == 68) && !block.active) {
block.active = 1;
slideRight();
}
}
function keyUp(e) {
if (n) {
var nKey = e.which;
var ieKey = 0;
}
if (ie) {
var ieKey = event.keyCode;

0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6

var nKey = 0;
}
if ((nKey == 97 || ieKey == 65 || nKey == 100 || ieKey == 68))
block.active = 0;
}
function slideRight() {
if (block.active) {
block.xpos += 5;
block.left = block.xpos;
status = block.xpos;
setTimeout("slideRight()", 25);
}
}
function slideLeft() {
if (block.active) {
block.xpos -= 5;
block.left = block.xpos;
status = block.xpos;
setTimeout("slideLeft()", 25);
}
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
<BODY OnLoad="moveImage()">
<!-- STEP THREE: Copy this code into the BODY of your HTML document -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<div id="blockDiv" STYLE="position: absolute; left: 317; top: 127; width: 137; height:
121">
<img src="ejemplos/bisco.gif"></div>
<center>
Usa la tecla <b>A</b> para mover la imagen hacia la izquierda, <b>D</b> para
moverla hacia la derecha.
<center>
</body>
</html>

3
6
4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
7
3
7
4
7
5
7
6
7
7
7
8
7
9
8
0
8
1
8
2
8
3
8
4
8
5
8
6
8
7
Puntua este post

Potrebbero piacerti anche