开发者

Sequence of events in jquery

开发者 https://www.devze.com 2023-02-05 18:47 出处:网络
I have been trying to get this to work for months without success. I can get the layers to fade in and out manually by using a separate button \"input type=\'button\' value=\'FadeOut\' onclick=\'fade(

I have been trying to get this to work for months without success. I can get the layers to fade in and out manually by using a separate button "input type='button' value='FadeOut' onclick='fade('Layer1', this)' " for each layer, but getting this to run automatically in sequence had eluded me.

I have six layers, 0-5, that I want to fade in and out in sequence. I then want two functions to run. I want the length of time a layer is visible, approximately one second, to be adjustable. Can someone please help me?

When my page opens, I want "layer0" to be displayed. onLoad I want "layer1" to fade in and then out. Next I want "layer2" to fade in and then out. Next I want "layer3" to fade in and then out. Next I want "layer4" to fade in and then out. Next I want "layer5" to fade in and then out. Then I want "layer0 to fade out. Finally I want "function1" and "function2" to run.

Here is the code I am now using:

CSS:

.initial-splash {  
    position:absolute;  
    left:10px;  
    top:105px;  
    width:610px;  
    height:335px;  
    z-index: 10;  
 }  
.splash {  
    font-family: Georgia, Times New Roman, Times, serif;  
    font-size: 36px;  
    font-weight: bolder;  
    col开发者_JS百科or: #FCC836;  
    height: 49px;  
    line-height: normal;  
    z-index: 12;  
 }  
.splash {  
    display: none;  
}  

HTML:

//<![CDATA[ 
$(window).load(function(){
    var delay = 500, speed = 600;
    var layers = $('.splash').length;

    $('.splash').each(function (i) {
        var me = $(this);
        setTimeout(function () {
            me.fadeIn(speed).delay(delay).fadeOut(speed, function () {
                if (i == layers - 1) {
                    $('.initial-splash').fadeOut(speed, animationComplete);
                }
            });
        }, i * (delay + speed * 2));
    });

    function animationComplete() {  
        fade(),hideLayerNext();  
    }  
});  
//]]>  


<div class="initial-splash"><img src="images/Site/625x340sunrise.jpg" alt="sunrise" width="625" height="340" border="1"></div>  

<div class="splash" style="position: absolute; top: 390px; left: 32px; width: 266px; ">Friendship /div>  

<div class="splash" style="position: absolute; top: 324px; left: 147px; width: 236px; ">Fellowship /div>  

<div class="splash" style="position: absolute; top: 256px; left: 265px; width: 189px; ">Recovery /div>  

<div class="splash" style="position: absolute; top: 188px; left: 361px; width: 136px; ">Sanity /div>  

<div class="splash" style="position: absolute; top: 130px; left: 500px; width: 133px; ">Hope /div>   


jQuery's .delay() will be helpful here, but it only works on a single element. However, we can combine it with some basic calculations and a setTimeout to make it work for any number of layers:

// These are adjustable
var delay = 1000, speed = 600;
var layers = $('.layer').length;

$('.layer').each(function (i) {
    var me = $(this);
    setTimeout(function () {
        me.fadeIn(speed).delay(delay).fadeOut(speed, function () {
            if (i == layers - 1) {
                $('.initial-layer').fadeOut(speed, animationComplete);
            }
        });
    }, i * (delay + speed * 2));
});

function animationComplete() {
    // Do whatever here
}

See the demo: http://jsfiddle.net/pMnwM/1/

Update: I realised that you actually want layer0 to always be underneath the other fade-ins and -outs until the end. Updated my code and demo to work that way.


you should use a callback after each fade:

$("#layer1").fadeOut(1000, function() {
    $("layer2").fadeOut(1000, function() {
       // deeper layers, you get the idea
    })
});


This is what I came up with, using callbacks. You can specify the fade delay and the time each layer is displayed.

function showHideLayer(id, speed, showFor, callback)
{
    $('#' + id).fadeIn(speed, function() {
        setTimeout(function() {
            $('#' + id).fadeOut(speed, callback);
        }, showFor);
    });
}

function yourFunction1()
{
    alert('Called Function 1');
}

function yourFunction2()
{
    alert('Called Function 2');
}

$(function() {

    $('#yourlayer1id, #yourlayer2id, #yourlayer3id').hide();

    showHideLayer('yourlayer1id', 2000, 2000, function() {
        showHideLayer('yourlayer2id', 2000, 2000, function() {
            showHideLayer('yourlayer3id', 2000, 2000, function() {
                yourFunction1();
                yourFunction2();
            });
        });
    });

});

You can see it working here.

0

精彩评论

暂无评论...
验证码 换一张
取 消