jquery - 我可以将 jQuery 事件存储在变量中吗?

标签 jquery

我想将两个或多个事件存储在一个变量中,例如元素选择器。
或者 - 还有其他方法可以做到这一点吗?让我知道。

我知道的方式:

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

del.fadeIn().delay(3000).fadeOut(function() {
  auto.fadeIn().delay(3000).fadeOut(function() {
    show.fadeIn().delay(3000).fadeOut();
  });
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

但我想要这样的东西:

let envent = fadeIn().delay(3000).fadeOut(); 

del.envent {
  auto.envent {
    show.envent;
  };
};

最佳答案

您可以通过编写可重用的函数来做到这一点:

function mySequence(el, callback) {
    return el.fadeIn().delay(3000).fadeOut(callback);
}

然后要使用它,请将 del 传递给它:

mySequence(del, function() {
    // Do the next thing
});

实例(延迟较短):

function mySequence(el, callback) {
    return el.fadeIn().delay(300).fadeOut(callback);
}

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

mySequence(del, function() {
    mySequence(auto, function() {
        mySequence(show);
    });
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果你愿意,你还可以做一个小的 jQuery 插件:

$.fn.mySequence = function(callback) {
    return this.fadeIn().delay(3000).fadeOut(callback);
};

然后要使用它,您可以像使用任何其他 jQuery 函数一样使用它:

del.mySequence(function() {
    // Do the next thing
});

实例(延迟较短):

$.fn.mySequence = function(callback) {
    return this.fadeIn().delay(300).fadeOut(callback);
};

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

del.mySequence(function() {
    auto.mySequence(function() {
        show.mySequence();
    });
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<小时/>

如果您想避免直接传递回调,您可以使用 native Promises 或 jQuery 的 Deferred 对象。这是一个返回 Deferred promise 的插件:

$.fn.mySequence = function() {
    var d = $.Deferred();
    this.fadeIn().delay(3000).fadeOut(d.resolve.bind(d));
    return d.promise();
};

然后要使用它,您可以像使用任何其他 jQuery 函数一样使用它:

del.mySequence()
.then(function() {
    // Do the next thing
});

实例(延迟较短):

$.fn.mySequence = function() {
    var d = $.Deferred();
    this.fadeIn().delay(300).fadeOut(d.resolve.bind(d));
    return d.promise();
};

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

del.mySequence()
.then(function() {
    return auto.mySequence();
})
.then(function() {
    return show.mySequence();
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

关于jquery - 我可以将 jQuery 事件存储在变量中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214324/

相关文章:

javascript - 通过ajax无法填充HTML下拉列表

javascript - 在调整大小之前不会出现元素(Safari 浏览器)

javascript - AEM 6.1 触摸 UI 对话框 - 手动调用对话框验证事件

javascript - Summernote 下拉菜单不适用于 Bootstrap 导航栏

javascript while循环正确迭代但具有相同逻辑的for循环不是,在具有整数值和其中一些空值的数组上

javascript - Ajax 在另一个中运行,通过点击事件

javascript - 返回单击时选中的所有复选框的数组

jQuery/CSS 偏移+鼠标移动问题

jQuery slideToggle(从中间点切换)

javascript - 查看变量是否存在