简体   繁体   English

使用JQuery淡出动画

[英]Fade out animation with JQuery

I have the following code that attempts to demonstrate and animate a dequeuing procedure of an aircraft takeoff queue. 我有以下代码,试图演示和动画化飞机起飞队列的出队过程。 For every takeoff (or dequeue) which happens after 5 seconds, a box is supposed to fade out until all have faded out after the takeoff queue is empty. 对于5秒钟后发生的每一次起飞(或出队),都应该淡出一个盒子,直到起飞队列为空后所有盒子都消失了。 My problem is how do i link each plane dequeue to a box such that for every dequeue a box fades out? 我的问题是我如何将每个出队列的飞机链接到一个盒子,以便每个出队列的盒子都淡出?

Here is the code snipet 这是代码片段

 function airport() { this.takeoff_queue = ["KQA", "ERJ", "TOM", "DHL", "ETH"]; this.landing_queue = ["RWA", "KLM", "PAN", "FLY540", "JAMBO"]; console.log('DEPARTING', this.landing_queue, 'ARRIVING', this.takeoff_queue); } var departure = new airport(); var takeoff_interval = setInterval(function depart() { $("#box1").fadeOut(); if (departure.takeoff_queue.length != 0) { departure.takeoff_queue.shift() $("#box1").fadeOut(); console.log('DEPARTING', departure.takeoff_queue); } else { clearInterval(takeoff_interval); console.log('TAKEOFFS COMPLETE'); } }, 5000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h3>SIMPLE SIMULATED TAKEOFF</h3> <div id="box1" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box2" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box3" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box4" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box5" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> 

If this is a wrong approach kindly explain it to me. 如果这是错误的方法,请向我解释。

How about associating each element with some data such as its destination in this case, and look for the element based on that 在这种情况下,如何将每个元素与一些数据(例如其目的地)相关联,并基于该元素寻找元素

 function airport() { this.takeoff_queue = ["KQA", "ERJ", "TOM", "DHL", "ETH"]; this.landing_queue = ["RWA", "KLM", "PAN", "FLY540", "JAMBO"]; console.log('DEPARTING', this.landing_queue, 'ARRIVING', this.takeoff_queue); } var departure = new airport(); var takeoff_interval = setInterval(function depart() { if (departure.takeoff_queue.length != 0) { var dest = departure.takeoff_queue.shift() $("[data-dest='" + dest + "']").fadeOut(); console.log('DEPARTING', departure.takeoff_queue); } else { clearInterval(takeoff_interval); console.log('TAKEOFFS COMPLETE'); } }, 5000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h3>SIMPLE SIMULATED TAKEOFF</h3> <div id="box1" style="width: 20px; height: 20px; background:black;float:left;margin:10px" data-dest="KQA"></div> <div id="box2" style="width: 20px; height: 20px; background:black;float:left;margin:10px" data-dest="ERJ"></div> <div id="box3" style="width: 20px; height: 20px; background:black;float:left;margin:10px" data-dest="TOM"></div> <div id="box4" style="width: 20px; height: 20px; background:black;float:left;margin:10px" data-dest="DHL"></div> <div id="box5" style="width: 20px; height: 20px; background:black;float:left;margin:10px" data-dest="ETH"></div> 

Within your setInterval call, you're continually fading #div1 which only should be faded once. 在您的setInterval调用中,您不断使#div1淡入淡出,该淡入仅应淡入一次。 I believe what you're after is $("div:visible:first").fadeOut(); 我相信您需要的是$("div:visible:first").fadeOut(); :

var takeoff_interval = setInterval(function depart() {
    if (departure.takeoff_queue.length != 0) {
        departure.takeoff_queue.shift()
        $("div:visible:first").fadeOut();
        console.log('DEPARTING', departure.takeoff_queue);
    } else {
        clearInterval(takeoff_interval);
        console.log('TAKEOFFS COMPLETE');
    }
}, 5000);

jsFiddle example jsFiddle示例

Try utilizing .queue() 尝试利用.queue()

 function airport() { this.takeoff_queue = ["KQA", "ERJ", "TOM", "DHL", "ETH"]; this.landing_queue = ["RWA", "KLM", "PAN", "FLY540", "JAMBO"]; console.log('DEPARTING', this.landing_queue, 'ARRIVING', this.takeoff_queue); } var departure = new airport(); $(document).queue("q", $.map(departure.takeoff_queue, function(val, key) { // add `class` `departure.takeoff_queue` value to `div` $("div[id^=box]").eq(key).addClass(val); return function(next) { // select `div` by `departure.takeoff_queue` value `class` $("div[class="+val+"]").delay(5000).fadeOut(0, function() { console.log("DEPARTING:" + this.className); next() }) } })).dequeue("q").promise("q").done(function() { console.log("TAKEOFFS COMPLETE") }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h3>SIMPLE SIMULATED TAKEOFF</h3> <div id="box1" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box2" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box3" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box4" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> <div id="box5" style="width: 20px; height: 20px; background:black;float:left;margin:10px"></div> 

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

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