This code runs perfectly in Chrome, but Firefox says the function tile1
is not defined. What could be the problem?
Also, is there any way to shorten this function? I have tried using a for-loop inside tile1
and also an if-else
statement but I didn't succeed.
$('div.tile').each(function(index, element) {
for(var i=0;i<=index;i++){
var tile1=function(){
var one ="div.tile div.one";
var two =" div.tile div.two";
var three = "div.tile div.three";
if(index==0){
one="div.tile div.one";
two="div.tile div.two";
three="div.tile div.three";
} else {
one ="div.tile div.one"+index;
two ="div.tile div.two"+index;
three ="div.tile div.three"+index;
}
var $one=$(one);
var $two = $(two);
var $three=$(three);
var oneTop = $one.top;
var twoTop = $two.top;
var threeTop = $three.top;
delayRate += 3000; // delayRate 5 sec (5000) by default
$one
.delay(delayRate)
.animate({top: "-100.5%"},300,easing);
$two
.delay(delayRate)
.animate({top:"0%"},300,easing);
$three
.delay(delayRate)
.animate({top:"100.5%"},300,easing);
$one
.delay(12000)
.animate({top: "-200.5%"},300,easing);
$two
.delay(12000)
.animate({top:"-100.5%"},300,easing);
$three
.delay(12000)
.animate({top:"0"},300,easing);
$one
.delay(12000)
.animate({top: "-100.5%"},300,easing);
$two
.delay(12000)
.animate({top:"0"},300,easing);
$three
.delay(12000)
.animate({top:"100.5%"},300,easing);
$one
.delay(15000-delayRate)
.animate({top: "0"},300,easing);
$two
.delay(15000-delayRate)
.animate({top:"100.5%"},300,easing);
$three
.delay(15000-delayRate)
.animate({top:"200.5%"},300,easing);
if(i==3){
delayRate=0;
}
}
}
window.setInterval(tile1, 3000);
});
As I called the function the index
comes randomly like 0,3,1,2, and there are 4 divs that the index corresponds to.