Simultaneous jQuery Animations

喜欢而已 提交于 2019-12-10 17:13:57

问题


I'm trying to get both a fadeIn (opacity toggle) and border fade (using jquery-animate-colors) to fire simultaneously but I'm having some trouble. Can someone help review the following code?

$.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

Would really appreciate it. Thanks!


回答1:


By default, JQuery places animations in the effects queue so they will happen one after another. If you want an animation to happen immediately set the queue:false flag in your animate options map.

For example, in your case,

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

would become

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

You'd likely want to use the options map and set the queue for the border animation as well.

http://api.jquery.com/animate/




回答2:


$.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"
  });
};


来源:https://stackoverflow.com/questions/6803491/simultaneous-jquery-animations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!