Tecnología

Inicio

Cómo rebotar una pelota en JavaScript

Hay un par de métodos que se pueden emplear para insertar una pelota que rebota en su página Web. Un método requiere que usted tenga acceso al software Adobe Flash. El otro método - que utiliza JavaScript - no requiere que comprar ningún software especial. Sin embargo, usted debe tener un conocimiento general de codificación HTML y una comprensión sólida de JavaScript para hacer rebotar una pelota con el código JavaScript.

Instrucciones

1 Conectarse a su servidor Web y haga clic en la página HTML en la que desea insertar el código JavaScript que creará una pelota que rebota.

2 Desplazarse a la sección <head> del documento HTML. Inserte el siguiente código dentro de esta sección de la página:

<Script language = "JavaScript1.2">

<! - Begin

BrowserCheck funcionar () {

var b = navigator.appName;

si (b == "Netscape") this.b = "NS";

else if (b == "Microsoft Internet Explorer") this.b = "IE";

else this.b = b;

this.v = parseInt (navigator.appVersion);

this.NS = (this.b == "NS" && this.v> = 4);

this.NS4 = (this.b == "NS" && this.v == 4);

this.NS5 = (this.b == "NS" && this.v == 5);

this.IE = (this.b == "IE" && this.v> = 4);

this.IE4 = (navigator.userAgent.indexOf ( 'MSIE 4')> 0);

this.IE5 = (navigator.userAgent.indexOf ( 'MSIE 5')> 0);

si (this.IE5 || this.NS5) this.VER5 = true;

si (this.IE4 || this.NS4) this.VER4 = true;

this.OLD = (! this.VER5 &&! this.VER4)? verdadero Falso;

this.min = (this.NS || this.IE);

}

es = new BrowserCheck ();

// End ->

</ Script>

3 Desplazarse hasta la sección <body> de su página. Inserte el código entre las etiquetas <body> y </ body>:

<Center>

Haga clic en cualquier lugar para iniciar la escritura

<Div id = "staticBall" style = "position: relative; visibilidad Visible">

<Img height = 30 width = 30 alt = "bola estática">

</ Div>

</ Center>

<Div id = estilo "bola" = "visibility: hidden; position: absolute; left: 100; la parte superior: 10; altura: 34; anchura: 34">

<Img height = 30 width = 30 alt = "Rebotando pelota">

</ Div>

4 Reemplazar "http://www.rewritables.net/ball.gif" con la URL de la imagen de otra bola si desea utilizar otro tipo de pelota. La pelota está en el código es azul. Mantenga el código intacto si desea utilizar la bola azul.

5 Inserte el siguiente código justo debajo del código que ha insertado en el paso 3:

<Script language = "JavaScript1.2">

<! - Begin

iter = 0;

setId = 0;

abajo = true;

arriba = false;

bouncingBall = (is.VER5)? document.getElementById ( "bola"). estilo

: (is.NS)? document.layers [ "pelota"]

: Document.all [ "pelota"] estilo;.

stillBall = (is.VER5)? document.getElementById ( "staticBall"). estilo

: (is.NS)? document.layers [ "staticBall"]:. document.all [ "staticBall"] estilo;

winH = (is.NS)? window.innerHeight - 55: document.body.offsetHeight - 55;

document.onmouseup = ButtonUp;

si (is.NS4)

document.captureEvents (Event.MOUSEUP);

función ButtonUp (e) {

if (((is.NS) e.which: event.button) = 1?!) return true;

si (setId = 0) clearInterval (setId);

bouncingBall.visibility = "visible";

stillBall.visibility = "oculto";

bouncingBall.left = (is.NS)? e.pageX - 15: event.offsetX - 15;

bouncingBall.top = (is.NS)? e.pageY - 15: event.offsetY - 15;

iter = 0;

setId = setInterval ( "generateGravity ()", 20);

return true;

}

generateGravity función () {

if ((parseInt (bouncingBall.top) + iter <winH) && abajo) {

bouncingBall.top = parseInt (bouncingBall.top) + iter;

iter ++;

regreso;

}

else {

if ((parseInt (bouncingBall.top) <winH) && abajo) {

bouncingBall.top = winH + 5;

regreso;

}

abajo = false;

a = true;

si (iter <0 && parseInt (bouncingBall.top)> winH) {

clearInterval (setId);

bouncingBall.visibility = "oculto";

stillBall.visibility = "visible";

setId = 0;

}

si (parseInt (bouncingBall.top)> 0 && && hasta iter> = 0) {

bouncingBall.top = parseInt (bouncingBall.top) - iter;

iter--;

si (iter% 3 == 0) iter--;

regreso;

}

abajo = true;

arriba = false;

}

}

// End ->

</ Script>

6 Publicar la página.