简体   繁体   English

对 jQuery 中列表项的顺序影响

[英]sequential effects on list-items in jQuery

here's the snippet of my code:这是我的代码片段:

<ul>
    <li><a href="home">Home</a></li>
    <li><a href="links">Links</a></li>
    <li><a href="contact">Contact</a></li>
</ul>

I use css to style them horizontally (menu-like) and what I would like to do is to animate all the list-items of the <ul> element.我使用 css 水平设置样式(类似菜单),我想做的是为<ul>元素的所有列表项设置动画。 I top them when the dom is ready and animate them to the bottom to attract users' eyes when the entire page is loaded.当 dom 准备好时,我将它们置顶,并在整个页面加载时将它们动画到底部以吸引用户的眼球。

here's the jquery code:这是 jquery 代码:

$(function(){
    $("ul li").css('top', '-40px'); //items are in relative position

    $(window).bind("load", items_animate, false);
});

function items_animate(){
       ... //I'd like to animate each <li> of the <ul> changing 'top' to '0px' but not simultaneously, I want to declare a DELAY between each animation (<li>'s get down in a row)
}

I know how to sequence effects with queue() or calling functions one by one but on only one element, I'm lost in this case..我知道如何使用 queue() 对效果进行排序或一一调用函数,但仅在一个元素上,我在这种情况下迷失了..

EDIT: for those who are interested, here's the code to accomplish this sequence, thanks to Joseph编辑:对于那些感兴趣的人,这里是完成这个序列的代码,感谢 Joseph

var animationDelay = 600;
var offset = 200;

function blah(meh) {
    setTimeout(function(){
        $(meh).animate({
            opacity: "0"
        }, animationDelay);
    },$(meh).index() * offset)
}

$("li").each(function(){
    blah(this);
})

Demo演示


Here is another way (using opacity for clarity) that animates the list items in series with a delay in between.这是另一种方式(为了清楚起见使用不透明度),它可以使列表项串联动画,中间有延迟。


<ul>
    <li><a href="home">Home</a></li>
    <li><a href="links">Links</a></li>
    <li><a href="contact">Contact</a></li>
</ul>


var animationDelay = 600;
var offset = 200;

function blah(meh) {
    setTimeout(function(){
        $(meh).animate({
            opacity: "0"
        }, animationDelay);
    },$(meh).index() * offset)
}

$("li").each(function(){
    blah(this);
})

* pardon the less than original names... it's late:P *请原谅那些比原来名字少的名字……太晚了:P

Try something like this:尝试这样的事情:

$(function() {
    function animateSequentially(element, properties, duration) {
        element.animate(properties, duration, function() {
            animateSequentially(element.next(), properties, duration);
        });
    }
    animateSequentially($("ul > li:first-child"), {top: '0'}, 1000);
});

Edit: If you'd like them to animate sequentially but not wait for the previous one, you can try this:编辑:如果您希望它们按顺序制作动画但不等待前一个动画,您可以试试这个:

$(function() {
    $("ul > li").each(function(index, item) {
        setTimeout(function() {
            $(item).animate({top: '0'}, 500);
        }, index*175);
    });
});

Try the one that waits here , or the one that doesn't wait here .试试在这里等的,或者不在这里等的。

function slide_down_recursive(e,duration,callback)
{
    $(e).animate(
    {
        top: '0px'
    }, duration, 'linear',function()
    {
        if($(e).next().length == 0)
        {
            if(typeof(callback) == 'function')
            {
                callback();
            }
            return;
        }
        else
        {
            // Apply recursion for every sibling.
            slide_down_recursive($(e).next(),duration,callback);
        }
    });

} // End slide_down_recursive
slide_down_recursive($('li:first-child'),500);

Here is a demo: http://jsfiddle.net/rpvyZ/这是一个演示: http://jsfiddle.net/rpvyZ/

as of this request, I wrote a jQuery plugin to walk sequencially through a list of (any) elements and applying css changes.根据这个请求,我编写了一个 jQuery 插件来顺序遍历(任何)元素列表并应用 css 更改。

You can checkout the Plugin here:您可以在此处查看插件:

https://github.com/ieservices/frontend-components/tree/master/jQuery/Plugins/jquery.list-effects https://github.com/ieservices/frontend-components/tree/master/jQuery/Plugins/jquery.list-effects

There I made it quite easy to apply those effects by defining the list and the effect options as a JavaScript object.在那里,我通过将列表和效果选项定义为 JavaScript object 使应用这些效果变得非常容易。 For the first version I created the possiblity to define the delay of the changes between the elements as well as the options to define a starting index to define on which element the changes should be applied.对于第一个版本,我创建了定义元素之间更改的延迟的可能性,以及定义起始索引以定义应应用更改的元素的选项。

With the plugin you can do something like this:使用插件,您可以执行以下操作:

<div id="myList">
<h4>This is my list</h4>
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
</ul>

By applying css stylesheet changes by rotating through the list elements:通过旋转列表元素来应用 css 样式表更改:

jQuery('#myList ul li').listEffect(
  {delay: '2000', attribute: 'color', value: '#ccc'}
);

Also I created and a demo in the repo, which is available here:我还在 repo 中创建了一个演示,可在此处获得:

https://github.com/ieservices/frontend-components/blob/master/jQuery/Plugins/jquery.list-effects/demo/list-effects-demo-simple.html https://github.com/ieservices/frontend-components/blob/master/jQuery/Plugins/jquery.list-effects/demo/list-effects-demo-simple.html

So, far it can't do much, but what do you guys think of that Plugin?所以,到目前为止它不能做太多,但是你们觉得那个插件怎么样?

Use .animate s callback function to animate the next element.使用.animate的回调 function 为下一个元素设置动画。

  $('li:eq(0)').animate({
    top: "0px"
  }, 5000, function() {
    $('li:eq(1)').animate({
      top: "0px"
    }, 5000, function() {
      ...
    });
  });

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

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