javascript - jQuery:元素列表上的交错动画

标签 javascript jquery html css

我有一个导航容器,里面有垂直排序的链接。我想要的是每个链接淡入淡出并从左向右飞。但是我不确定如何按顺序执行此操作?我有代码可以一次完成所有这些,但我想一次完成一个。或者至少让它们交错排列,以便在动画触发之间有任意延迟

代码:

$(document).ready(function(){
    $("#navigation a")
        .css({opacity:0,"margin-right":"10px"})
        .animate({opacity:1,"margin-right":"0"});
});

最佳答案

这是 $.queue 的完美用法。感谢您提出问题:您让我学会了如何更好地使用它来研究这个问题。

这是一个活生生的例子:http://jsfiddle.net/EJgEC/

// Cache our jQuery-wrapped objects
var $navigation = $('#navigation'),
    $navigationLinks = $navigation.find('a');

// Set the initial state on navigation links for future animation
$navigationLinks.css({
  opacity: 0,
  "margin-left": "20px"
});

$navigationLinks.each(function (i, item) {
  var $item = $(item);

  // Add animations on each item to the fx queue on the navigation DOM element
  $.queue($navigation[0], 'fx', function () {
    var that = this;
    $item.animate({
      opacity: 1,
      "margin-left": "0"
    }, {
      complete: function () {
        // Fire the next item in the queue as the callback
        $.dequeue(that);
      }
    });
  });
});

// Start the navigation queue
$navigation.dequeue();

我还强烈建议阅读 $.queue:它值得了解。 http://api.jquery.com/jQuery.queue/

关于javascript - jQuery:元素列表上的交错动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14183899/

相关文章:

html - 伪元素上的 Font Awesome 5 显示正方形而不是图标

javascript - 将数字数组转换为对象数组

javascript - 添加 javascript 和 jquery 的 Django 模板

javascript - 通过 jquery 或 javascript 隐藏内容会影响 SEO 吗?

html - 使用 CSS 的相对大小标题

javascript - 将 HTML5 Canvas 详细信息保存到 MySql

javascript - 将焦点设置到谷歌地图上的对象

javascript - 为什么 (!+[]+[]) 在 Javascript 中是 'true' 而 (false + []) 是 'false'?

javascript - Three.js - 在 JSON 对象上显示图像

JQuery - 在下拉菜单更改时查找最接近的文本框