同时执行多个jQuery动画

4

我正在尝试同时触发fadeIn(透明度切换)和border fade(使用jquery-animate-colors),但我遇到了一些问题。有人可以帮忙审核以下代码吗?

$.fn.extend({
  key_fadeIn: function() {
    return $(this).animate({
      opacity: "1"
    }, 600);
  },
  key_fadeOut: function() {
    return $(this).animate({
      opacity: "0.4"
    }, 600);
  }
});

fadeUnselected = function(row) {
  $("#bar > div").filter(function() {
    return $(this).id !== row;
  }).key_fadeOut();
  return $(row).key_fadeIn();
};

highlightRow = function(row, count) {
  return $(row).animate({
    "border-color": "#3737A2"
  }).animate({
    "border-color": "#FFFFFF"
  }).animate({
    "border-color": "#3737A2"
  }).animate({
    "border-color": "#FFFFFF"
  });
};


fadeUnselected("#foo");
highlightRow("#foo"); // doesn't fire until fadeUnselected is complete

非常感谢。谢谢!


如果您能提供一个演示,比如在jsfiddle.net上,那就太好了。 - karim79
2个回答

9

默认情况下,JQuery将动画放置在效果队列中,以便它们按顺序发生。如果您想立即发生动画,请在animate选项地图中设置queue:false标志。

例如,在您的情况下,

$(this).animate({
      opacity: "1"
    }, 600);

会变成

$(this).animate(
{
    opacity: "1"
}, 
{
    duration:600,
    queue:false
});

您可能希望同时使用选项映射和设置队列来进行边框动画。 http://api.jquery.com/animate/

0
$.fn.extend({
  key_fadeIn: function() {
    return $(this).animate({
      opacity: "1"
    }, { duration:600, queue:false });
  },
  key_fadeOut: function() {
    return $(this).animate({
      opacity: "0.4"
    }, { duration:600, queue:false });
  }
});

fadeUnselected = function(row) {
  $("#bar > div").filter(function() {
    return $(this).id !== row;
  }).key_fadeOut();
  return $(row).key_fadeIn();
};

highlightRow = function(row, count) {
  return $(row).animate({
    "border-color": "#3737A2"
  }).animate({
    "border-color": "#FFFFFF"
  }).animate({
    "border-color": "#3737A2"
  }).animate({
    "border-color": "#FFFFFF"
  });
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接