admin管理员组

文章数量:1355542

Currently the editor that I am using is Adobe Dreamweaver. In my editor the following statements work during trial, however in my browsers such as Safari, Mozilla, and Chrome my fade in and fade out is not working at all. I'd like for them to constantly fade out then fade back in, but the images still remain visible. Any incites as to why this is happening to me?

$(document).ready(function(){
    var x=0;
    while(x==0 ){
        $("#Imag1").fadeOut(3000);
    $("#Imag2").fadeOut(8000);
        $("#Imag3").fadeOut(6000); 
        x++;

    if(x==1){
        $("#Imag1").fadeIn(2000);
            $("#Imag2").fadeIn(5000);
            $("#Imag3").fadeIn(9000);
        x--;
        } 
    }
});

Currently the editor that I am using is Adobe Dreamweaver. In my editor the following statements work during trial, however in my browsers such as Safari, Mozilla, and Chrome my fade in and fade out is not working at all. I'd like for them to constantly fade out then fade back in, but the images still remain visible. Any incites as to why this is happening to me?

$(document).ready(function(){
    var x=0;
    while(x==0 ){
        $("#Imag1").fadeOut(3000);
    $("#Imag2").fadeOut(8000);
        $("#Imag3").fadeOut(6000); 
        x++;

    if(x==1){
        $("#Imag1").fadeIn(2000);
            $("#Imag2").fadeIn(5000);
            $("#Imag3").fadeIn(9000);
        x--;
        } 
    }
});
Share Improve this question edited Aug 13, 2013 at 3:02 Nunser 4,5228 gold badges27 silver badges37 bronze badges asked Aug 13, 2013 at 1:54 HRoHRo 1152 silver badges9 bronze badges 10
  • 1 You are mission a ) closing paran and ; semicolon in your code example..just a copy/paste error or is that your actual code? If so, you need to add those in order for your javascript to run correctly. – Alex Commented Aug 13, 2013 at 2:02
  • 1 With the change @Alex mentioned, this code looks like it just goes into a hard loop, x swaps between 0 and 1, both the while loop condition and the fadein condition will always be true. – dc5 Commented Aug 13, 2013 at 2:07
  • @Alex, that was a copy and past issue. – HRo Commented Aug 13, 2013 at 2:19
  • @dc5, I have tried to make the conditions true or false and it yet has worked out. – HRo Commented Aug 13, 2013 at 2:20
  • You call fadeOut then fadeIn... This means that it will immediately be faded out then back in on document.ready... Is that the effect you want? – Zach Saucier Commented Aug 13, 2013 at 2:27
 |  Show 5 more ments

3 Answers 3

Reset to default 7

Here is my fiddle http://jsfiddle/m7HcT/1/ It creates a re-useable method for you to call on any element you want to flash.

HTML:

<div class='block' id='Imag1'></div>
<div class='block' id='Imag2'></div>
<div class='block' id='Imag3'></div>

jQuery:

fadeloop('#Imag1',1500,1200,true);
fadeloop('#Imag2',100,200,true);
fadeloop('#Imag3',3000,7000,true);

//The element is cached inside the function so we don't make 
//Multiple DOM calls on elements we have already found.
//Each Interval is pletely independant

function fadeloop(el,timeout,timein,loop){
    var $el = $(el),intId,fn = function(){
         $el.fadeOut(timeout).fadeIn(timein);
    };
    fn();
    if(loop){
        intId = setInterval(fn,timeout+timein+100);
        return intId;
    }
    return false;
}

Simply remove the while loop and it works perfectly

$(document).ready(function () {
    $("#Imag1").fadeOut(3000);
    $("#Imag2").fadeOut(8000);
    $("#Imag3").fadeOut(6000);
    $("#Imag1").fadeIn(2000);
    $("#Imag2").fadeIn(5000);
    $("#Imag3").fadeIn(9000);
});

Working jsFiddle

EDIT

Since you want it endless, you can put the effects into another function and run it on the callback. Updated fiddle here

$(document).ready(function () {
    fadeThem();
});
function fadeThem() {
    $("#Imag1").fadeOut(3000, function() {
        $(this).fadeIn(2000, fadeThem());
    // Apply the callback to the one with the shortest bined animation time
    });
    $("#Imag2").fadeOut(8000, function() {
        $(this).fadeIn(5000);
    });
    $("#Imag3").fadeOut(6000, function() {
        $(this).fadeIn(9000);
    });
}

I have fixed the problem. All this time the link element was missing its less than sign(>). So frustrated after 4 hours, I found my mistake.

本文标签: javascriptJQuery fading in fading out continuouslyStack Overflow