Проблем с Javascript

adrian

Registered
Кода не е мой, от една книга е, но има проблем не действа скрипта. За анимиране на обект в права линия е?

Код:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional1//EN" "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1251"/>
<title>Пробен сайт</title>

<script language="Javascript" type="text/javascript">

var animateSpeed = 5;
var object = null;
var fX = null;
var fY = null;
var cX = null;
var cY = null;
var dX = null;
var dY = null;
var stepX = null;
var stepY = null;
var slope = null;

function initAnimate(objectID,x,y) {
	object = document.getElementById(objectID);
	fX = x;
	fY = y;

cX = object.offsetLeft;
	cY = object.offsetTop;
	dX = Math.abs(fX-cX);
	dY = Math.abs(fY-cY);
	if ((dX = = 0 ) || (dY == 0)) slope = 0;

else slope = dY/dX;
	if (dX>dY) {
		if (cX<fX) stepX = animateSpeed;
		else if (cX>fX) stepX = -animateSpeed;
		if (cY<fY) stepY = animateSpeed*slope;
		else if (cY>fY) stepY = -animateSpeed*slope;
	}
	else if (dX<dY) {
		if (cY<fY) stepY = animateSpeed;
		else if (cY>fY) stepY = -animateSpeed;
		if (cX<fX) stepX = animateSpeed/slope;
		else if (cX>cX) stepX = -animateSpeed/slope;
	}
	animateObcejt ()
}

function animateObject() {
	if ((dX >0) || (dY >0)) {
		object.style.left = Math.round(cX) + 'px';
		object.style.top = Math.round(cY) + 'px';
		cX = cX + stepX;
		cY = cY + stepY;
		dX = dX -Math.abs(stepX);
		dY = dY -Math.abs(stepY);
		setTimeout ('animateObject()',0);
	}
	else {
		object.style.left = fX + 'px';
		object.style.top = fY + 'px';
	}
	return;
}
</script>
<style type="text/css" media="screen">
#madHatter { 
	position: absolute;
	left: 10px;
	top: 10px;
}
</style>
</head>

<body onload="initAnimate('madHatter',300,250)">
<div id="madHatter">
<img src="1.jpg" height="120" width="120" border="0"/>
</div>
</body>
</html>
 

naiki999

Registered
Код:
 <script language="Javascript" type="text/javascript"> 

var animateSpeed = 5; 
var object = null; 
var fX = null; 
var fY = null; 
var cX = null; 
var cY = null; 
var dX = null; 
var dY = null; 
var stepX = null; 
var stepY = null; 
var slope = null; 

function initAnimate(objectID,x,y) { 
   object = document.getElementById(objectID); 
   fX = x; 
   fY = y; 

cX = object.offsetLeft; 
   cY = object.offsetTop; 
   dX = Math.abs(fX-cX); 
   dY = Math.abs(fY-cY); 
   if ((dX = = 0 ) || (dY == 0)) slope = 0; 

else slope = dY/dX; 
   if (dX>dY) { 
      if (cX<fX) stepX = animateSpeed; 
      else if (cX>fX) stepX = -animateSpeed; 
      if (cY<fY) stepY = animateSpeed*slope; 
      else if (cY>fY) stepY = -animateSpeed*slope; 
   } 
   else if (dX<dY) { 
      if (cY<fY) stepY = animateSpeed; 
      else if (cY>fY) stepY = -animateSpeed; 
      if (cX<fX) stepX = animateSpeed/slope; 
      else if (cX>cX) stepX = -animateSpeed/slope; 
   } 
   animateObcejt () 
} 

function animateObject() { 
   if ((dX >0) || (dY >0)) { 
      object.style.left = Math.round(cX) + 'px'; 
      object.style.top = Math.round(cY) + 'px'; 
      cX = cX + stepX; 
      cY = cY + stepY; 
      dX = dX -Math.abs(stepX); 
      dY = dY -Math.abs(stepY); 
      setTimeout ('animateObject()',0); 
   } 
   else { 
      object.style.left = fX + 'px'; 
      object.style.top = fY + 'px'; 
   } 
   return; 
} 
</script>

тая част не трябва ли да е в бодито :twisted:
 

adrian

Registered
Ами и така не става....има някъква малка грешка в кода някъде май, но 5 пъти го преглеждам и не я намирам :cry:
 

Pr0oF

Registered
Код:
if ((dX = = 0 ) || (dY == 0)) slope = 0;
да стане
Код:
f ((dX == 0 ) || (dY == 0)) slope = 0;
----

Код:
animateObcejt ()
да стане
Код:
animateObject();
---
Провери и кажи резултата.
 

Горе