简体   繁体   English

使用jQuery连续淡入/淡出多个图像

[英]Fade-in/Fade-out Multiple Images in Succession with jQuery

I'm new to animating with JQuery, and I'm completely lost trying to create the following animation: 我不熟悉JQuery动画,而尝试创建以下动画完全迷路了

I want ONE CUBE at a time to FADE IN for 8 seconds or so and then FADE OUT as the second cube FADES IN and this has to continue FOR ALL 8 CUBES over and over again. 我想要一个CUBE同时淡入 8秒左右,然后淡出作为第二个立方体淡入 ,这有一遍又一遍继续为所有8个方块。 Note: There should be only one cube visible at a time. 注意:一次只能看到一个立方体。

I have failed to do this in jQuery, but you can check out my fiddle and see if you can correct my code to follow the above instructions, and maybe even simplify the JQuery. 我在jQuery中未能做到这一点,但是您可以检查一下我的小提琴,看看是否可以更正我的代码以遵循上述说明,甚至可以简化JQuery。 I'm also open to CSS3 Animation if you find it a better solution. 如果您发现CSS3动画是一种更好的解决方案,那么我也可以使用它。 Thank you for your help! 谢谢您的帮助!

My Fiddle 我的小提琴

This is the JS: 这是JS:

$(function() {
function pulsate(element) {
    $(element || this).delay(3050).fadeOut(300).delay(6050).fadeIn(300, pulsate); 
}
    pulsate($('.bubble1'))
});


$(function() {
function pulsate(element) {
    $(element || this).delay(5000).fadeOut(300).delay(10000).fadeIn(300, pulsate); 
}
    pulsate($('.bubble2'))
});


$(function() {
function pulsate(element) {
    $(element || this).delay(3050).fadeOut(300).delay(6050).fadeIn(300, pulsate); 
}
    pulsate($('.bubble3'))
});


$(function() {
function pulsate(element) {
    $(element || this).delay(5000).fadeOut(300).delay(10000).fadeIn(300, pulsate); 
}
    pulsate($('.bubble4'))
});




$(function() {
function pulsate(element) {
    $(element || this).delay(3050).fadeOut(300).delay(6050).fadeIn(300, pulsate); 
}
    pulsate($('.bubble5'))
});


$(function() {
function pulsate(element) {
    $(element || this).delay(5000).fadeOut(300).delay(10000).fadeIn(300, pulsate); 
}
    pulsate($('.bubble6'))
});


$(function() {
function pulsate(element) {
    $(element || this).delay(3050).fadeOut(300).delay(6050).fadeIn(300, pulsate); 
}
    pulsate($('.bubble7'))
});


$(function() {
function pulsate(element) {
    $(element || this).delay(5000).fadeOut(300).delay(10000).fadeIn(300, pulsate); 
}
    pulsate($('.bubble8'))
});

HTML: HTML:

<div class="bubble1">&nbsp;</div>

<div class="bubble2">&nbsp;</div>

<div class="bubble3">&nbsp;</div>

<div class="bubble4">&nbsp;</div>

<div class="bubble5">&nbsp;</div>

<div class="bubble6">&nbsp;</div>

<div class="bubble7">&nbsp;</div>

<div class="bubble8">&nbsp;</div>

CSS: CSS:

.bubble1
{
    background: #ff0; position:Absolute; top:20%; left:10%;
    height: 100px;
    width: 100px;

}

.bubble2
{
    background: #333; position:Absolute; top:20%; left:30%;
    height: 100px;
    width: 100px;

}


.bubble3
{
    background: #f90; position:Absolute; top:20%; left:60%;
    height: 100px;
    width: 100px;

}


.bubble4
{
    background: #e43; position:Absolute; top:20%; left:80%;
    height: 100px;
    width: 100px;

}


.bubble5
{
    background: #e38; position:Absolute; top:70%; left:10%;
    height: 100px;
    width: 100px;

}


.bubble6
{
    background: #338; position:Absolute; top:70%; left:30%;
    height: 100px;
    width: 100px;

}




.bubble7
{
    background: #fdd; position:Absolute; top:70%; left:60%;
    height: 100px;
    width: 100px;

}


.bubble8
{
    background: #53d; position:Absolute; top:70%; left:80%;
    height: 100px;
    width: 100px;

}

Using script can make your code easy to be managed but still a pure CSS solution always has its own advantage (you can always make it more manageable by using LESS or SCSS, a kind of CSS generator source code). 使用脚本可以使您的代码易于管理,但是纯CSS解决方案仍然始终具有其自身的优势(通过使用LESS或SCSS(一种CSS生成器源代码),您始终可以使其更加易于管理)。 Here I'm introducing a solution using pure CSS. 在这里,我将介绍使用纯CSS的解决方案。 The point here is just simple, when it comes to repeating animation, you should think of CSS animation right away. 这里的意思很简单,当涉及到重复动画时,您应该立即想到CSS animation Because of the sequential fading (in/out for only one element at a time), you can think of the animation-delay . 由于连续的淡入淡出(一次只对一个元素进/出),因此您可以想到animation-delay Just set different values for each element, you will have the desired effect. 只需为每个元素设置不同的值,您将获得所需的效果。 Also note that, the animation-duration should be the total time of all cycles (of fading in and out all elements). 另请注意, animation-duration应为所有循环的总时间(淡入和淡出所有元素)。 You want a fading in of 8 seconds, I suppose the fading out takes 2 seconds. 您想要8秒钟的淡入,我想淡出需要2秒钟。 So the total time of 1 cycle is 10 seconds, the total cycles will be 80 seconds (for 8 elements). 因此1个周期的总时间为10秒,总周期将为80秒(对于8个元素)。

Here is the detailed code: 这是详细的代码:

 .bubble { height: 100px; width: 100px; position:absolute; animation: fading 80s infinite ease-in; opacity:0; } .bubble:nth-child(1) { background: #ff0; top:20%; left:10%; } .bubble:nth-child(2) { background: #333; top:20%; left:30%; animation-delay: 10s; } .bubble:nth-child(3) { background: #f90; top:20%; left:60%; animation-delay: 20s; } .bubble:nth-child(4) { background: #e43; top:20%; left:80%; animation-delay: 30s; } .bubble:nth-child(5) { background: #e38; top:70%; left:10%; animation-delay: 40s; } .bubble:nth-child(6) { background: #338;top:70%; left:30%; animation-delay: 50s; } .bubble:nth-child(7) { background: #fdd; top:70%; left:60%; animation-delay: 60s; } .bubble:nth-child(8) { background: #53d;top:70%; left:80%; animation-delay: 70s; } @keyframes fading { 0% { opacity:0; } 10% { opacity:1; } 12.5% { opacity:0; } } 
 <div class="bubble">&nbsp;</div> <div class="bubble">&nbsp;</div> <div class="bubble">&nbsp;</div> <div class="bubble">&nbsp;</div> <div class="bubble">&nbsp;</div> <div class="bubble">&nbsp;</div> <div class="bubble">&nbsp;</div> <div class="bubble">&nbsp;</div> 

You can use a recursive function, as such 您可以使用递归函数,例如

var elems = $(".bubble");
var i = 1;
fade(elems[0])

function fade(elem) {
    if (i > elems.length - 1) {
    i = 0;
 }
 $(elem).fadeIn(300).delay(8000).fadeOut(300, function() {
     fade(elems[i++])
  });
 }

Try the following code 试试下面的代码

 var el = $( "[class^=bubble]"), i = 0; (function pulsate() { el.fadeOut(300).eq(i++%el.length).fadeIn(8000, pulsate); }()); 
 .bubble1 { background: #ff0; position:Absolute; top:20%; left:10%; height: 100px; width: 100px; } .bubble2 { background: #333; position:Absolute; top:20%; left:30%; height: 100px; width: 100px; } .bubble3 { background: #f90; position:Absolute; top:20%; left:60%; height: 100px; width: 100px; } .bubble4 { background: #e43; position:Absolute; top:20%; left:80%; height: 100px; width: 100px; } .bubble5 { background: #e38; position:Absolute; top:70%; left:10%; height: 100px; width: 100px; } .bubble6 { background: #338; position:Absolute; top:70%; left:30%; height: 100px; width: 100px; } .bubble7 { background: #fdd; position:Absolute; top:70%; left:60%; height: 100px; width: 100px; } .bubble8 { background: #53d; position:Absolute; top:70%; left:80%; height: 100px; width: 100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="bubble1">&nbsp;</div> <div class="bubble2">&nbsp;</div> <div class="bubble3">&nbsp;</div> <div class="bubble4">&nbsp;</div> <div class="bubble5">&nbsp;</div> <div class="bubble6">&nbsp;</div> <div class="bubble7">&nbsp;</div> <div class="bubble8">&nbsp;</div> 

Here is the working jsfiddle: http://jsfiddle.net/f6C79/74/ 这是工作中的jsfiddle: http : //jsfiddle.net/f6C79/74/

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

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