On 08/06/2005 14:40, ASM wrote:
cjl wrote:
Just getting my feet wet with javascript. The following script works
fine in Firefox, but won't run in IE.
I cannot see anything in what you've posted that will not work in IE
5.x+. Please post a URL to an example - it's much easier to diagnose.
Some alternative code is included at the end of this post (though it is
untested).
[snip]
try :
document.getElementById('right').src = eval(images[counter]+'.src');
Randomly throwing eval about will *NEVER* solve a problem.
try also putting a var right=''; at top of JS
What do you think that will do?
or prefer :
document.getElementById(element).src = images[counter].src;
and
timeout_state = setTimeout("animate("+element+")", 50);
That seems sensible, but it won't fix the OP's code.
[snip]
Mike
function Animation(name, images, rate) {
var current = 0,
element = document.images[name],
timer;
function animate() {
element.src = images[current];
current = (current + 1) % images.length;
timer = setTimeout(animate, rate);
}
if(!element || ('function' != typeof setTimeout)
|| ('function' != typeof clearTimeout))
{
return null;
}
animate.toString = function() {
return 'Animation[' + name + ']();';
};
Animation[name] = animate;
this.preload = function() {
if('function' == typeof Image) {
for(var i = 0, n = images.length, t; i < n; ++i) {
t = new Image();
t.src = images[i];
}
}
};
this.reset = function() {
this.stop();
current = 0;
};
this.start = function() {
if(!timer) {
timer = setTimeout(animate, rate);
}
};
this.stop = function() {
if(timer) {
clearTimeout(timer);
timer = null;
}
};
}
A usage example:
var myImages = [],
i = 2,
n = 29,
myAnimation;
/* Define the array of images in the animation.
* It could be performed in any way, but this
* seems to suit your situation the best.
*/
while(i <= n) {
myImages[myImages.length] = i + '.jpg';
}
/* Create an Animation object. */
myAnimation = new Animation('right', myImages, 50);
/* If it was created properly, */
if(myAnimation) {
/* attempt to preload the frames */
myAnimation.preload();
/* and start the animation when the document
* loads.
*/
window.onload = function() {
myAnimation.start();
};
}
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.