如何同时执行多个 jquery 效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2344804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I execute multiple, simultaneous jquery effects?
提问by Samuel Meacham
I am animating some error/validation elements on a page. I want them to bounce and be highlighted, but at the same time if possible. Here's what I'm currently doing:
我正在为页面上的一些错误/验证元素设置动画。我希望它们反弹并突出显示,但如果可能的话同时进行。这是我目前正在做的事情:
var els = $(".errorMsg");
els.effect("bounce", {times: 5}, 100);
els.effect("highlight", {color: "#ffb0aa"}, 300);
This causes the elements to first bounce, and THEN be highlighted, and I'd like them to occur simultaneously. I know that with .animate()
you can specify queue:false
in the options, but I don't want to use animate because the pre-built effects "bounce" and "highlight" are exactly what I want.
这会导致元素首先反弹,然后突出显示,我希望它们同时发生。我知道.animate()
你可以queue:false
在选项中指定,但我不想使用动画,因为预建的效果“反弹”和“突出显示”正是我想要的。
I have tried simply chaining the calls like els.effect().effect()
, and that doesn't work. I've also tried to put queue:false
in the options object I pass in, and that doesn't work.
我试过简单地链接像 那样的调用els.effect().effect()
,但这是行不通的。我还尝试放入queue:false
我传入的选项对象,但这不起作用。
采纳答案by Samuel Meacham
Ok, so this is a very custom solution that combines the bounce and highlight effects. I'd rather see some kind of jquery support for combining these more easily, specifying {queue:false}, but I don't think it's that simple.
好的,所以这是一个非常自定义的解决方案,它结合了反弹和高光效果。我宁愿看到某种 jquery 支持更容易地组合这些,指定 {queue:false},但我不认为它那么简单。
What I did was take the jquery.effects.bounce.js and jquery.effects.highlight.js (from jquery-ui-1.8rc3), and combine the code of the two as DaveS suggested, to create a new effect named "hibounce". In my testing, it supports all of the options of both, and they occur simultaneously. It looks great! I'm not a hugefan of solutions like this though, because of the maintenance factor. Anytime I upgrade jquery.ui, I'll have to update this file manually as well.
我所做的是采用 jquery.effects.bounce.js 和 jquery.effects.highlight.js(来自 jquery-ui-1.8rc3),并按照 DaveS 的建议将两者的代码结合起来,创建一个名为“hibounce”的新效果”。在我的测试中,它支持两者的所有选项,并且它们同时发生。看起来不错!我不是一个巨大的像这样虽然解决方案的粉丝,因为维护系数。每当我升级 jquery.ui 时,我都必须手动更新此文件。
Anyway, here is the combined result (jquery.effects.hibounce.js)
无论如何,这是组合结果(jquery.effects.hibounce.js)
(function($) {
$.effects.hibounce = function(o) {
return this.queue(function() {
// Highlight and bounce parts, combined
var el = $(this),
props = ['position','top','left','backgroundImage', 'backgroundColor', 'opacity'],
mode = $.effects.setMode(el, o.options.mode || 'show'),
animation = {
backgroundColor: el.css('backgroundColor')
};
// From highlight
if (mode == 'hide') {
animation.opacity = 0;
}
$.effects.save(el, props);
// From bounce
// Set options
var mode = $.effects.setMode(el, o.options.mode || 'effect'); // Set Mode
var direction = o.options.direction || 'up'; // Default direction
var distance = o.options.distance || 20; // Default distance
var times = o.options.times || 5; // Default # of times
var speed = o.duration || 250; // Default speed per bounce
if (/show|hide/.test(mode)) props.push('opacity'); // Avoid touching opacity to prevent clearType and PNG issues in IE
// Adjust
$.effects.save(el, props); el.show(); // Save & Show
$.effects.createWrapper(el); // Create Wrapper
var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left';
var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg';
var distance = o.options.distance || (ref == 'top' ? el.outerHeight({margin:true}) / 3 : el.outerWidth({margin:true}) / 3);
if (mode == 'show') el.css('opacity', 0).css(ref, motion == 'pos' ? -distance : distance); // Shift
if (mode == 'hide') distance = distance / (times * 2);
if (mode != 'hide') times--;
// from highlight
el
.show()
.css({
backgroundImage: 'none',
backgroundColor: o.options.color || '#ffff99'
})
.animate(animation, {
queue: false,
duration: o.duration * times * 1.3, // cause the hilight to finish just after the bounces (looks best)
easing: o.options.easing,
complete: function() {
(mode == 'hide' && el.hide());
$.effects.restore(el, props);
(mode == 'show' && !$.support.opacity && this.style.removeAttribute('filter'));
(o.callback && o.callback.apply(this, arguments));
el.dequeue();
}
});
// Animate bounces
if (mode == 'show') { // Show Bounce
var animation = {opacity: 1};
animation[ref] = (motion == 'pos' ? '+=' : '-=') + distance;
el.animate(animation, speed / 2, o.options.easing);
distance = distance / 2;
times--;
};
for (var i = 0; i < times; i++) { // Bounces
var animation1 = {}, animation2 = {};
animation1[ref] = (motion == 'pos' ? '-=' : '+=') + distance;
animation2[ref] = (motion == 'pos' ? '+=' : '-=') + distance;
el.animate(animation1, speed / 2, o.options.easing).animate(animation2, speed / 2, o.options.easing);
distance = (mode == 'hide') ? distance * 2 : distance / 2;
};
if (mode == 'hide') { // Last Bounce
var animation = {opacity: 0};
animation[ref] = (motion == 'pos' ? '-=' : '+=') + distance;
el.animate(animation, speed / 2, o.options.easing, function(){
el.hide(); // Hide
$.effects.restore(el, props); $.effects.removeWrapper(el); // Restore
if(o.callback) o.callback.apply(this, arguments); // Callback
});
} else {
var animation1 = {}, animation2 = {};
animation1[ref] = (motion == 'pos' ? '-=' : '+=') + distance;
animation2[ref] = (motion == 'pos' ? '+=' : '-=') + distance;
el.animate(animation1, speed / 2, o.options.easing).animate(animation2, speed / 2, o.options.easing, function(){
$.effects.restore(el, props); $.effects.removeWrapper(el); // Restore
if(o.callback) o.callback.apply(this, arguments); // Callback
});
};
el.queue('fx', function() { el.dequeue(); });
el.dequeue();
});
};
})(jQuery);
It can be used like any other effect now:
它现在可以像任何其他效果一样使用:
var el = $("#div1");
el.effect("hibounce", {color: "#F00", times: 5}, 100);
回答by HoffZ
jQuery UI will queue the effects by default. Use dequeue() to run simultaneously:
默认情况下,jQuery UI 会将效果排入队列。使用 dequeue() 同时运行:
var opt = {duration: 7000};
$('#lbl').effect('highlight', opt).dequeue().effect('bounce', opt);
回答by DaveS
jQuery UI's effects queue animations, so write your own version of a bounce/highlight function. Just copy the source code from both into a single function, clean up the code, and each time it calls animate, make sure to have the bounce and highlight logic in there together.
jQuery UI 的效果队列动画,因此请编写您自己的反弹/突出显示功能版本。只需将源代码从两者复制到一个函数中,清理代码,每次调用 animate 时,确保将反弹和高亮逻辑放在一起。
回答by kevingessner
You could try this:
你可以试试这个:
var els = $(".errorMsg");
setTimeout(function() {
els.effect("bounce", {times: 5}, 100);
}, 1);
setTimeout(function() {
els.effect("highlight", {color: "#ffb0aa"}, 300);
}, 1);
That should call both the effects at roughly the same time, asynchronously.
这应该大致同时异步调用这两种效果。