【问题标题】:How can I execute multiple, simultaneous jquery effects?如何执行多个同时的 jquery 效果?
【发布时间】:2011-01-21 15:15:08
【问题描述】:

我正在为页面上的一些错误/验证元素设置动画。我希望它们反弹并突出显示,但如果可能的话,同时进行。这是我目前正在做的事情:

var els = $(".errorMsg");
els.effect("bounce", {times: 5}, 100);
els.effect("highlight", {color: "#ffb0aa"}, 300);

这会导致元素首先反弹,然后突出显示,我希望它们同时发生。我知道使用.animate(),您可以在选项中指定queue:false,但我不想使用动画,因为预置效果“反弹”和“突出显示”正是我想要的。

我尝试过简单地将els.effect().effect() 之类的调用链接起来,但这是行不通的。我也尝试将queue:false 放在我传入的选项对象中,但这不起作用。

【问题讨论】:

  • 你用的是什么版本的jQuery?
  • 1.4.2,用户界面 1.7.2。因此,在撰写本文时,两者都是最新的稳定版。

标签: jquery animation effects jquery-animate effect


【解决方案1】:

默认情况下,jQuery UI 会将效果排队。使用 dequeue() 同时运行:

    var opt = {duration: 7000};

    $('#lbl').effect('highlight', opt).dequeue().effect('bounce', opt);   

Demo in JsFiddle

【讨论】:

  • 这实际上并不像看起来那样工作。尝试反转反弹和突出显示,您会发现它们并没有真正同时发生。
  • 你是对的@CharlesWood。当“hightlight”在“bounce”之前时它不起作用。我用小提琴玩了一下:其他效果,如'blind'、'puff'和'fold,在最后突出显示时效果很好。似乎是高亮前反弹的问题。不知道为什么
【解决方案2】:

好的,这是一个非常自定义的解决方案,它结合了反弹和高光效果。我宁愿看到某种 jquery 支持更容易地组合这些,指定 {queue:false},但我不认为它那么简单。

我所做的是获取 jquery.effects.bounce.js 和 jquery.effects.highlight.js(来自 jquery-ui-1.8rc3),并按照 DaveS 的建议将两者的代码结合起来,创建一个新的效果命名为“休眠”。在我的测试中,它支持两者的所有选项,并且它们同时发生。看起来不错!不过,由于维护因素,我不是这样的解决方案的巨大粉丝。每当我升级 jquery.ui 时,我也必须手动更新此文件。

不管怎样,这是合并后的结果(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);

它现在可以像任何其他效果一样使用:

var el = $("#div1");
el.effect("hibounce", {color: "#F00", times: 5}, 100);

【讨论】:

    【解决方案3】:

    jQuery UI 的效果队列动画,所以编写你自己版本的反弹/高亮函数。只需将两者的源代码复制到一个函数中,清理代码,每次调用 animate 时,确保将反弹和高亮逻辑放在一起。

    【讨论】:

    • 我已经考虑过这个选项,如果我真的需要这两种效果,我可能会选择它。现在,我满足于让代码更简单,并且只使用其中一种效果。可惜 jquery 允许您指定 {queue:false} 来调用 animate(),而不是 effect()。
    【解决方案4】:

    你可以试试这个:

    var els = $(".errorMsg");
    setTimeout(function() {
        els.effect("bounce", {times: 5}, 100);
    }, 1);
    setTimeout(function() {
        els.effect("highlight", {color: "#ffb0aa"}, 300);
    }, 1);
    

    这应该大致同时异步调用两个效果。

    【讨论】:

    • 我会在明天回到代码时试一试,如果它有效,我会投票并标记为答案。谢谢!
    • 我怀疑这会在直接方法失败时起作用。你还在一一调用effect函数。请记住 Javascript 是单线程运行的,因此两者都将线性执行。
    • LiraNuna 称之为。动画仍在排队,效果一个接一个地执行。然而,LiraNuna,虽然 javascript 可能是单线程的,但有可能以一种看似同时的方式执行 2 个效果。您当然可以使用 jquery 的 animate(),通过在选项中提供 {queue:false} 来做到这一点。在多线程/多核 CPU 之前,操作系统使用时间片来运行多个线程。 JS 并没有什么不同。
    • 好电话,LiraNuna。我很幸运使用这种技术让“后台”JS 工作——浏览器异步运行 setTimeout 函数。无论如何,jQuery 的效果必须同步运行。
    猜你喜欢
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    相关资源
    最近更新 更多