4

I've made an image fader using 2 <div>s (one is an image and the other is a ul containing several images) the script is working perfectly on firefox and Safari but not working properly on Chrome, it just does the first fade and then it stops the script is this

$("#second").css({
    opacity: 0.0
});

$(function () {
    setInterval("rotateImages()", 4000);
});

function rotateImages() {

    if ($("#first").css("opacity") == 1) {
        $("#first").animate({
            opacity: 0.0
        }, 1500);
        $("#second").animate({
            opacity: 1.0
        }, 1500);
    } else {
        $("#second").animate({
            opacity: 0.0
        }, 1500);
        $("#first").animate({
            opacity: 1.0
        }, 1500);
    };

};

I don't know where exactly the problem is and how to make the script work on all browsers. any help would be much appreciated

Thanks

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Yamen
  • 41
  • 1
  • 4
  • 1
    Can you make a jsfiddle.net ? – mplungjan Aug 12 '13 at 12:46
  • Works fine for me in Chrome http://jsfiddle.net/JLC2q/ – epascarello Aug 12 '13 at 12:51
  • NITPICK: do not keep using `$("#XXXX")` over and over again, store it into a variable and use that so you are not constantly doing DOM look ups to get the element you already found once. – epascarello Aug 12 '13 at 12:54
  • @user2674916 It is really hard without your interaction. Come up and place your comments. if resolved, place the reason or share the errors. – Praveen Aug 12 '13 at 13:00
  • I tried all suggestions and it's still the same, works fine with all browsers except Chrome. – Yamen Aug 12 '13 at 14:01
  • possible duplicate of [fade in/out between two divs on a loop](http://stackoverflow.com/questions/14589842/fade-in-out-between-two-divs-on-a-loop) – mplungjan Dec 10 '13 at 09:43

3 Answers3

2

This works for me

Live Demo

$(function() {
  $("#second").css({
    opacity: 0.0
  });
  setInterval(rotateImages, 4000);    
});
function rotateImages() {
    if ($("#first").css("opacity") == 1) {
        $("#first").animate({
            opacity: 0.0
        }, 1500);
        $("#second").animate({
            opacity: 1.0
        }, 1500);
    } else {
        $("#second").animate({
            opacity: 0.0
        }, 1500);
        $("#first").animate({
            opacity: 1.0
        }, 1500);
    };

};

However it can be MUCH simpler.

For example this almost works, but this one is better

$(function() {
  var $first = $("#first");
  var $second  = $("#second");
  $("#second").hide();

  var tId = setInterval(function() {
      $first.fadeToggle("slow",function() {
        $second.fadeToggle("slow");
      })    
  },4000);          
});
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-1

Remove () and quotes

   setInterval(rotateImage, 4000);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Praveen
  • 55,303
  • 33
  • 133
  • 164
-2
$(function(){
    setInterval("rotateImages()", 4000);
});

change to

setInterval(function(rotateImages()), 4000);
Farhad
  • 742
  • 6
  • 22