简体   繁体   English

在页面加载时,使用jQuery逐格淡入和淡出

[英]On page load, fading in and out one div after another with jQuery

I am working on a piece of my personal website and what I want to do is a simple animation. 我正在个人网站上工作,我想做的是一个简单的动画。

Here is the code: 这是代码:

<div class="wrapper">--- Main website body ---</div>
<div class="intro1">
    <div class="name1">John</div>
    <div class="name2">Doe</div>
</div>
<div class="intro2">
    <div class="prof1">Designer</div>
    <div class="prof2">Developer</div>
</div>​


<style>
    body {background: rgba(0,0,0,1); margin: 0px; padding: 0px; color: rgba(255,255,255,1); }
    .wrapper {width:100%; height:100%; max-height: 100%; background: rgba(0,0,0,1); display: none;}
    .intro1 {width: 500px; height: 150px; margin: 40% auto 0% auto; padding: 0px; display: none;}
    .intro2 {width: 500px; height: 150px; margin: 40% auto 0% auto; padding: 0px; display: none;}
</style>


$(function() {
    $('.intro1').fadeIn(1000).fadeOut(1000);
    $(function() {
        $('.intro2').fadeIn(1000).fadeOut(1000);
    });
});​

Here is the break down of what should happen: 这是应该发生的情况的分解:

  1. Page loads with simple black background 页面加载时带有简单的黑色背景
  2. Div (.intro1) slowly fades in with possible sliding in from the top and going downwards Div(.intro1)逐渐淡入,可能会从顶部向下滑动
  3. Div (.intro1) slowly fades out with possible sliding out downwards followed by removing the element completely Div(.intro1)逐渐淡出并可能向下滑动,然后完全移除该元素
  4. Div (.intro2) slowly fades in with possible sliding in from the top and going downwards Div(.intro2)逐渐淡入,可能会从顶部向下滑动
  5. Div (.intro2) slowly fades out with possible sliding out downwards followed by removing the element completely Div(.intro2)逐渐淡出并可能向下滑动,然后完全移除该元素
  6. Div (.wrapper) slowly fades in Div(.wrapper)逐渐淡入

This is what I have so far and I am nor sure what I need to do next. 到目前为止,这就是我要做的,而且我也不知道下一步需要做什么。

Here is the link: http://jsfiddle.net/gVp7m/ 这是链接: http : //jsfiddle.net/gVp7m/

Try this: 尝试这个:

$(function() {
    $('.intro1').fadeIn(1000).fadeOut(1000, function(){
       $('.intro2').fadeIn(1000).fadeOut(1000);
    });
});​

Here's how you could do it for example. 例如,您可以按照以下方法进行操作。
It looks more complex than it actually is 看起来比实际要复杂

$(function() {
    function showIntro(nr){
        $('.intro'+nr).css({
            'margin-top':'20%',
            'display':'block',
            'opacity':0})
            .animate({
                'margin-top':'40%',
                'opacity':1},2000,function(){

                $(this).delay(1000).animate({
                    'margin-top':'60%',
                    'opacity':0},2000,function(){

                        $(this).remove();
                        if(nr==1){
                            showIntro(2);
                        }else if(nr==2){
                            $('.wrapper').fadeIn(2000);
                        }
                });          
        });
    };

    showIntro(1);
});​

Demo: 演示:
http://jsfiddle.net/gVp7m/4/ http://jsfiddle.net/gVp7m/4/

Simple, use the callback function to trigger the next animation after the first one completes :) 很简单,在第一个动画完成后,使用回调函数触发下一个动画:)

Fiddle 小提琴

$(function() {

$('.intro1').fadeIn(1000).fadeOut(1000, function() {

    $('.intro2').fadeIn(1000).fadeOut(1000, function() {

        $('.wrapper').fadeIn(1000);

    });

});

}); });

How about something like this: 这样的事情怎么样:

jsFiddle - would something like this do? jsFiddle-这样可以吗?

If it needs more explination just let me know. 如果需要更多说明,请告诉我。 I broke it into spaces to make it easier to read. 我将其分成多个空格以使其更易于阅读。

CSS CSS

body {background: rgba(0,0,0,1); margin: 0px; padding: 0px; color: rgba(255,255,255,1); }
#Splash { position: fixed; top:0; left: 0; height: 100%; width: 100%; background: black; }
.wrapper {width:100%; height:100%; max-height: 100%; background: rgba(0,0,0,1); }
.intro1 {  }
.intro1 div { position: absolute; bottom: .5em; opacity: 0; }
.intro1 .name1 { left: .5em; }
.intro1 .name2 { right: .5em; }
.intro2 {  }
.intro2 div { position: absolute; opacity: 0; }
.intro2 .prof1 { left: 40%; }
.intro2 .prof2 { right: 40%; }

Script 脚本

$(".intro1 div").animate({ opacity: 1 }, 2000);

$(".intro1 .name1").animate({
    left: "40%",
    top: ".5em"
}, 3000);
$(".intro1 .name2").animate({
    left: "60%",
    top: ".5em"
}, 3000);

$(".intro1 div").animate({ opacity: 0 }, 2000, function(e) {
    $(".intro2 div").animate({ opacity: 1 }, 2000);

    $(".intro2 .prof1").animate({
        left: ".5em",
        bottom: ".5em"
    }, 3000);
    $(".intro2 .prof2").animate({
        right: ".5em",
        bottom: ".5em"
    }, 3000);

    $(".intro2 div").animate({ opacity: 0 }, 2000, function(e) {
        $("#Splash div").hide();
        $("#Splash").fadeOut(3000);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM