简体   繁体   English

jQuery动画和效果使用$ .when()和then()应用于多个元素

[英]JQuery animation and effects apply for several elements with $.when() and then()

I have html element like this, 我有这样的html元素,

<div class="row h-100 p-3 justify-content-center align-items-center m-0">
        <h1 class="col-12 text-center position-absolute welcome-line-1 d-none-edited" id="welcome-line-1">TEXT 01</h1>
        <h3 class="col-12 text-center position-absolute welcome-line-2 d-none-edited" id="welcome-line-2">TEXT 02</h3>
        <h2 class="col-12 text-center position-absolute welcome-line-3 d-none-edited" id="welcome-line-3">TEXT 03</h2>
    </div>

and I want to animate these text elements using jQuery.This is my jquery code for one element. 我想使用jQuery为这些文本元素设置动画。这是我对一个元素的jquery代码。

var line_1_anim = function(){
            return $('#welcome-line-1')
                .css('opacity', 0)
                .slideDown('slow')
                .animate(
                    { opacity: 1 },
                    { queue: false, duration: 'slow' }
                )
                .delay(1000)
                .slideUp('slow');
        }

and lets assume I have three elements and i use this kind of approach to animate each element one by one using $.when() and then() 并假设我有三个元素,并且我使用这种方法使用$ .when()和then()分别为每个元素设置动画

$.when(line_1_anim())
   .then(line_2_anim)

I am trying to reduce the code complexity and achieve this. 我正在尝试减少代码复杂性并实现这一目标。 Already my functions work, but I want to do more. 我的功能已经可以使用,但是我想做更多。 Because if I want to add 10 more elements I have to repeat same code 10 times. 因为如果我想再添加10个元素,则必须将相同的代码重复10次。 So I write something like this. 所以我写这样的东西。

var line_animation = function(selector,delay){
            return selector
                .css('opacity', 0)
                .slideDown('slow')
                .animate(
                    { opacity: 1 },
                    { queue: false, duration: 'slow' }
                )
                .delay(delay)
                .slideUp('slow');
        }

        $.when(line_animation(line_1,1000))
        .then(line_animation(line_2,2000))
        .then(line_animation(line_3,3000));

I simply planned to change selector and delay and run the same method several time. 我只是计划更改选择器和延迟并多次运行相同的方法。 But this doesn't work like I want. 但这并不能按照我的要求进行。 All the functions work at once and not, one after the other. 所有功能一次都起作用,而不是一次又一次地起作用。

Any idea what is the wrong with my approach and how can I achieve this. 任何想法我的方法有什么问题以及如何实现这一目标。

Hope I have explained my question and everything is clear. 希望我已经解释了我的问题,一切都清楚了。

From jQuery promise example you can rewrite all like (ie: line_animation should return a promise and not a jQuery object): jQuery Promise 示例中,您可以将所有内容重写( 例如 :line_animation应该返回一个Promise,而不是jQuery对象):

 var line_animation = function (selector, delay) { var dfd = jQuery.Deferred(); selector.css('opacity', 0) .slideDown('slow') .animate({opacity: 1}, {queue: false, duration: 'slow'}) .delay(delay) .slideUp('slow', function () { dfd.resolve("hurray"); }); return dfd.promise(); // return a promise.... } $.when(line_animation($('#welcome-line-1'), 1000)).then(function () { line_animation($('#welcome-line-2'), 2000).then(function () { line_animation($('#welcome-line-3'), 3000); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row h-100 p-3 justify-content-center align-items-center m-0"> <h1 class="col-12 text-center position-absolute welcome-line-1 d-none-edited" id="welcome-line-1">TEXT 01</h1> <h3 class="col-12 text-center position-absolute welcome-line-2 d-none-edited" id="welcome-line-2">TEXT 02</h3> <h2 class="col-12 text-center position-absolute welcome-line-3 d-none-edited" id="welcome-line-3">TEXT 03</h2> </div> 

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

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