javascript - jQuery Animate and Loops -


i made simple animation website jquery inside joomla!, seems fine have strange issue during loop. cells fixed dims. inside loop i. check here http://db.tt/uud00tkc issue happens firefox.

the js code is:

jquery(document).ready(function () { function loop() {     jquery("#txt1").animate({         backgroundcolor: "#0089bc"         }, 3000, function() {         loop();     });             jquery("#txt1").animate({         backgroundcolor: "#fff"         }, 3000);                 jquery("#txt2").animate({         backgroundcolor: "#0089bc"         }, 9000, function() {         loop();     });     jquery("#txt2").animate({         backgroundcolor: "#fff"         }, 9000);        jquery("#txt3").animate({         backgroundcolor: "#0089bc"         }, 6000, function() {         loop();     });             jquery("#txt3").animate({         backgroundcolor: "#fff"         }, 6000);                }     loop(); });  jquery(document).ready(function () {  jquery("#img1").hide().fadein(3000); setinterval(function () {             jquery("#img1").fadeout(3000).fadein(3000); }, 5000);      jquery("#img2").hide().fadein(9000); setinterval(function () {             jquery("#img2").fadeout(9000).fadein(9000); }, 5000);    jquery("#img3").hide().fadein(6000); setinterval(function () {             jquery("#img3").fadeout(6000).fadein(6000); }, 5000);        }); 

html code

<div id="home-mosaico"> <div class="row"> <span class="cell-img" id="img1"><img src="http://www.quatrotd.co.uk/images/home/substation-02.jpg" alt="substation" title="substation" /></span> <span class="cell" id="txt1">engineering<br />and construction</span> <span class="cell-img" id="img2"><img src="http://www.quatrotd.co.uk/images/home/substation-01.jpg" alt="substation" title="substation" /></span> </div> <div class="row"> <span class="cell" id="txt2">transmission lines</span> <span class="cell-img" id="img3"><img src="http://www.quatrotd.co.uk/images/home/transmision_lines-01.jpg" alt="transmision lines" title="transmision lines" /></span> <span class="cell" id="txt3">substations</span> </div> </div> 

css code

/*** home mosaico ***/ #home-mosaico { display: table; width: 940px; height: 500px; } #home-mosaico .row { display: table-row; height: 250px;  } #home-mosaico .cell-img { display: table-cell; width: 313px; background-color: #0089bc; vertical-align:middle; text-align:center; font-size:20px; line-height: 24px; font-weight:normal; color:#ffffff; } #home-mosaico .cell { display: table-cell; width: 313px; background-color: #fff; vertical-align:middle; text-align:center; font-size:20px; line-height: 24px; font-weight:normal; color:#ffffff; } 

your loop function looks run problems. loop function starts, keep recursively calling instantly putting large strain on browser there isn't seconds break between calls.

on top of that, of animations finished, additionally call loop function again compounding first issue. while animations may stack correctly (one after other on elements told animate), there huge amount of recursion occurring in loop function.

an alternative loop function still getting same effect (animation stacking) might like:

jquery(document).ready(function () {     var txt1animate = function()     {         jquery("#txt1").animate({             backgroundcolor: "#0089bc"             }, 3000, function(){             jquery(this).animate({                 backgroundcolor: "#fff"             }, 3000, txt1animate);         });     }     var txt2animate = function()     {         jquery("#txt2").animate({             backgroundcolor: "#0089bc"             }, 9000, function(){             jquery(this).animate({                 backgroundcolor: "#fff"             }, 9000, txt2animate);         });     }     var txt3animate = function()     {         jquery("#txt3").animate({             backgroundcolor: "#0089bc"             }, 6000, function(){             jquery(this).animate({                 backgroundcolor: "#fff"             }, 6000, txt3animate);         });     }      txt1animate();     txt2animate();     txt3animate(); }); 

this correctly wait first animation finish on element, second , once second complete start again. stops animation queue spiraling out of control 100s 1000s of animations being queued second.

addressing second part of question, calling fadeout , straight away calling fadein. while should queue animation, goes 12 seconds , have being called again looped setinterval. create issues in long term queue growing every 10 or seconds without chance of finishing.

jquery(document).ready(function () {      jquery("#img1").hide().fadein(3000);     var img1fade = function()     {         jquery("#img1").fadeout(3000).fadein(3000,function()         {             settimeout(img1fade,5000);         });     }      jquery("#img2").hide().fadein(9000);     var img2fade = function()     {         jquery("#img2").fadeout(9000).fadein(9000,function()         {             settimeout(img2fade,5000);         });     }      jquery("#img3").hide().fadein(6000);     var img3fade = function()     {         jquery("#img3").fadeout(6000).fadein(6000,function()         {             settimeout(img3fade,5000);         });     }      img1fade();     img2fade();     img3fade(); }); 

similar other code provided, loops on in way won't have huge queue of animations @ once. nicely waits fade in finish before running settimeout loop function again.

edit

picked 1 bug in code supplied originally, using $ jquery in code on site linked, required jquery instead.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -