代码之家  ›  专栏  ›  技术社区  ›  Samuel Meacham

如何执行多个同时jquery效果?

  •  15
  • Samuel Meacham  · 技术社区  · 15 年前

    我正在页面上设置一些错误/验证元素的动画。我希望它们能反弹并突出显示,但如果可能的话,同时也要突出显示。以下是我目前正在做的工作:

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

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

    我试过像这样把电话连起来 els.effect().effect() 那不管用。我也试过 队列:假 在选项对象中,我传入,但不起作用。

    4 回复  |  直到 12 年前
        1
  •  11
  •   HoffZ    12 年前

    jquery用户界面默认将效果排队。使用dequeue()同时运行:

        var opt = {duration: 7000};
    
        $('#lbl').effect('highlight', opt).dequeue().effect('bounce', opt);   
    

    Demo in JsFiddle

        2
  •  8
  •   Samuel Meacham    15 年前

    好的,这是一个非常定制的解决方案,结合了反弹和高光效果。我宁愿看到某种jquery支持,可以更容易地将它们组合起来,指定queue:false,但我认为这并不简单。

    我所做的是使用jquery.effects.bounce.js和jquery.effects.highlight.js(来自jquery-ui-1.8rc3),并按照Daves的建议组合这两个代码,以创建一个名为“hibounce”的新效果。在我的测试中,它支持两者的所有选项,并且它们同时发生。看起来很棒!我不是 巨大的 像这样的解决方案,因为维护因素。每当我升级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
  •  4
  •   DaveS    15 年前

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

        4
  •  1
  •   kevingessner    15 年前

    你可以试试这个:

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

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