简体   繁体   English

使用jquery / js将HTML元素显示为带有暂停的序列

[英]Turn html elements visible as a sequence with pauses using jquery / js

I've been searching for quite a while now, and before everyone hits me the setTimeout() function, hear me out. 我已经搜索了很长一段时间,在每个人都打我setTimeout()函数之前,请听我说。

I have ~20 HTML elements which are invisible. 我有20个看不见的HTML元素。 I want them to turn visible one at a time, with a pause inbetween. 我希望他们一次可以看到一个,中间有一个停顿。 I also want to use some of jQueries awesome animation features when turning them visible. 当将它们变为可见时,我还想使用一些jQueries很棒的动画功能。

So basically I want to "play a sequence of frames/events/states" using JavaScript/jQuery. 因此,基本上我想使用JavaScript / jQuery“播放一系列帧/事件/状态”。

What have I looked in to? 我在看什么?

SetTimeout(): SetTimeout():

I've been looking at the JS setTimeout function, and from what I can tell, it works great when you have 1-3 setTimeout() functions. 我一直在研究JS setTimeout函数,据我所知,当您具有1-3个setTimeout()函数时,它的效果很好。

If you want more, it starts to get extremely messy. 如果您想要更多,它会变得非常混乱。 They are embedded, you have to time everything by adding numbers together and it's really hard to get the overall picture. 它们是嵌入式的,您必须通过将数字加在一起来计时所有内容,而要获得整体图像确实很困难。

jQuery.delay() jQuery.delay()

Also, the jQuery.delay() function lacks to actually pause the code. 另外,jQuery.delay()函数实际上并没有暂停代码。 I have 20+ elements, not one I want to animate. 我有20多个元素,没有一个要设置动画。

Updatepanel spam Updatepanel垃圾邮件

Another option, which I kind of considers right now (please, kill me), is using an updatepanel. 我现在考虑的另一种选择(请杀死我)是使用updatepanel。 Every second it makes a postback using a timer, and using a session, it knows exactly at what "frame" it is. 它每秒使用计时器进行回发,并使用会话进行回发,它确切地知道它是什么“帧”。 Then it can set things visible/invisible depending on frame number, and also fire jQuery animations. 然后,它可以根据帧号设置可见/不可见的内容,并触发jQuery动画。

Now, when I woke up this morning, I didn't dream of spamming my webserver with an updatepanel like this.... So: 现在,当我今天早上醒来时,我没有梦想用这样的updatepanel向我的网络服务器发送垃圾邮件。

Is there any good ways to do this? 有什么好的方法吗? I am very open to suggestions. 我很乐意提出建议。

Thanks a lot! 非常感谢! :-) :-)

If you apply the same class to all the elements ( sequential_expose in my example), you can select those elements and call each() on the selection. 如果你申请的同一类的所有元素( sequential_expose在我的例子),您可以选择这些元素并调用each()上的选择。 To stagger the fade in of the elements, you can multiple the index of the item as provided from each() and multiply it by the time you want for for the delay between fadeIn's and pass that value as parameter to delay() . 要错开元素的淡入,可以将each()提供的项的索引倍增,然后将其乘以所需的时间以淡化两次之间的延迟,然后将该值作为参数传递给delay() Given an interval of 500 milliseconds (you can make this whatever you want), the first item in selection would have 0 as delay, the second 500 , the third 1000 , etc. You then just chain your preferred animation ( fadeIn(200) in my example) after the call to delay() . 给定一个500毫秒的间隔(您可以根据需要进行设置),选择的第一个项目的延迟为0 ,第二个为500 ,第三个为1000fadeIn(200) 。然后,只需链接您喜欢的动画( fadeIn(200)我的示例)在调用delay()

$(function () {
    $('.sequential_expose').each(function(i) {
        $(this).delay(i*500).fadeIn(200);
    });
});

Note the example shows this as a document onready function, you can obviously change this as needed. 注意,该示例将其显示为文档就绪功能,您显然可以根据需要进行更改。

Here is how I would use setTimeout for this: 这是我将使用setTimeout的方式:

// Assume your target elements are all divs 
var elements = $('div');

function showOne() {
    // Fade-in the first element
    elements.first().fadeIn(200);

    // Remove first element from our jQuery object
    elements.splice(0,1);

    // If there are further elements, start a new timer
    if(elements.length) {
        setTimeout(showOne, 500);
    }
}

// Start first timeout
setTimeout(showOne, 500);

http://jsfiddle.net/uRwFK/ http://jsfiddle.net/uRwFK/

jQuery animate() includes a complete callback that allows you to chain animations. jQuery animate()包括一个完整的回调,允许您链接动画。

Let's say you have an array of 20 DOM elements, here is a rough snippet: 假设您有一个由20个DOM元素组成的数组,下面是一个简单的代码段:

var elArray=[el1,el2,...el20];

function animateFunction(i) {
    $(elArray[i]).animate(
       ...
       // on complete move to the next element
       complete: function(){if (i<elArray.length-1) animateFunction(i+1);}
    });
}

animateFunction(0);

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

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