ciao a tutti,
questo codice che permette ad una immagine di muoversi lungo il bordo dello schermo autonomamente, sapete come modificare il codice (oppure un altro codice) che permetta all'immagine di muoversi a CASO sullo schermo e NON lungo il bordo dello schermo???
grazie
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Documento senza titolo</title>
<style>
DIV.movable { position:absolute; }
</style>
<script language="javascript">
var x = 0;
var y = 0;
var velocita = 1;
var ritorno = 0;
function muoviImmagine() {
var dest_x = 0;
var dest_y = 0;
var interval_x = velocita;
var interval_y = velocita;
var img = document.getElementById('ball');
var imgHeight = img.offsetHeight;
var imgWidth = img.offsetWidth;
if (self.innerHeight)
{
dest_x = self.innerWidth;
}
else if (document.documentElement && document.documentElement.clientHeight)
{
dest_x = document.documentElement.clientWidth;
}
else if (document.body)
{
dest_x = document.body.clientWidth;
}
if (self.innerHeight)
{
dest_y = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight)
{
dest_y = document.documentElement.clientHeight;
}
else if (document.body)
{
dest_y = document.body.clientHeight;
}
if((x!=dest_x-imgWidth) && ritorno == 0){
if((x + interval_x > dest_x-imgWidth)){
x = dest_x - imgWidth;
}else {
x = x + interval_x;
}
}else if((y<dest_y-imgHeight) && ritorno == 0){
if((y + interval_y > dest_y-imgHeight)){
y = dest_y - imgHeight;
}else{
y = y + interval_y;
}
if(y==dest_y-imgHeight){
ritorno = 1;
}
}else if(x>0 && ritorno == 1){
if((x - interval_x < 0)){
x = 0;
}else {
x = x - interval_x;
}
}else if(y>0){
if(y - interval_y < 0){
y = 0;
}else{
y = y - interval_y;
}
if(y==0){
ritorno = 0;
}
}
document.getElementById("ball").style.top = y+'px';
document.getElementById("ball").style.left = x+'px';
window.setTimeout("muoviImmagine()",1);
}
</script>
</head>
<body onload="muoviImmagine()">
<div id="ball" class="movable"> [img]ball.gif[/img]
</div>
</body>
</html>