如何使用JQuery链接或排队自定义函数? [英] How do I chain or queue custom functions using JQuery?

查看:117
本文介绍了如何使用JQuery链接或排队自定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有多种功能,可以对HTML的不同部分进行不同的动画处理.我想将这些功能链接或排队,以便它们可以依次而不是同时运行动画.

I have multiple functions the do different animations to different parts of the HTML. I would like to chain or queue these functions so they will run the animations sequentially and not at the same time.

我试图按顺序自动执行多个事件,以使用户看起来像是在单击不同的按钮或链接.

I am trying to automate multiple events in sequence to look like a user has been clicking on different buttons or links.

我可能可以使用回调函数来做到这一点,但随后我将不得不从不同的函数中提取所有动画,并以正确的模式重新组合.

I could probably do this using callback functions but then I would have to pull all of the animations from the different functions and regroup in the right pattern.

jquery的队列"有帮助吗?我无法理解队列的文档.

Does the jquery "queue" help? I couldn't understand the documentation for the queue.

示例,jQuery:

 function One() {
        $('div#animateTest1').animate({ left: '+=200' }, 2000);
    }
    function Two() {
        $('div#animateTest2').animate({ width: '+=200' }, 2000);
    }

// Call these functions sequentially so that the animations
// in One() run b/f the animations in Two()
    One();
    Two();

HTML:

    <div id="animatetest" style="width:50px;height:50px;background-color:Aqua;position:absolute;"></div>
    <div id="animatetest2" style="width:50px;height:50px;background-color:red;position:absolute;top:100px;"></div>

谢谢.

我尝试使用计时器,但我认为有更好的方法.

I tried it with timers but I thought there is a better way to do it.

编辑#2:

让我更具体一些.我具有绑定到单击&的多个功能.将事件悬停在页面的不同元素上.通常,这些函数彼此无关(它们彼此不引用).我想模拟一个用户在不更改现有功能代码的情况下经历这些事件.

Let me be more specific. I have multiple functions bound to click & hover events on different elements of the page. Normally these functions have nothing to do with each other ( they don't reference each other). I would like to simulate a user going through these events without changing the code of my existing functions.

推荐答案

jQuery Queue代码虽然没有得到很好的记录,但是基本思想是:

The jQuery Queue code is not as well documented as it could be, but the basic idea is this:

$("#some_element").queue("namedQueue", function() {
  console.log("First");
  var self = this;
  setTimeout(function() {
    $(self).dequeue("namedQueue");
  }, 1000);
});

$("#some_element").queue("namedQueue", function() {
  console.log("Second");
  var self = this;
  setTimeout(function() {
    $(self).dequeue("namedQueue");
  }, 1000);
});

$("#some_element").queue("namedQueue", function() {
  console.log("Third");
});

$("#some_element").dequeue("namedQueue");

如果运行此代码,您将在控制台中看到"First",暂停为1s,在控制台中看到"Second",再次暂停为1s,最后在控制台中看到"Third".

If you run this code, you will see "First" in the console, a pause of 1s, see "Second" in the console, another pause of 1s, and finally see "Third" in the console.

基本思想是,可以将任意数量的命名队列绑定到一个元素.您可以通过调用dequeue从队列中删除一个元素.您可以根据需要手动进行多次操作,但是如果希望队列自动运行,只需在排队的函数中调用dequeue.

The basic idea is that you can have any number of named queues bound to an element. You remove an element from the queue by calling dequeue. You can do this yourself manually as many times as you want, but if you want the queue to run automatically, you can simply call dequeue inside the queued up function.

jQuery中的动画都在称为fx的队列中运行.为元素设置动画时,它会自动将新动画添加到队列中,如果当前没有动画在运行,则调用出队.您可以根据需要将自己的回调插入fx队列中;您只需要在结尾处手动调用dequeue(与其他队列使用方式一样)即可.

Animations in jQuery all run inside a queue called fx. When you animate an element, it automatically adds the new animation on the queue and calls dequeue if no animations are currently running. You can insert your own callbacks onto the fx queue if you wish; you will just need to call dequeue manually at the end (as with any other queue use).

这篇关于如何使用JQuery链接或排队自定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆