17

ページ上のいくつかのエラー/検証要素をアニメーション化しています。バウンスしてハイライト表示してほしいのですが、可能であれば同時に。これが私が現在行っていることです:

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

これにより、要素が最初にバウンスし、次に強調表示されます。これらを同時に発生させたいと思います。.animate()オプションで指定できることは知っていますがqueue:false、事前に作成されたエフェクトの「バウンス」と「ハイライト」がまさに私が望むものであるため、アニメーションは使用したくありません。

のように呼び出しを単純にチェーンしようとels.effect().effect()しましたが、うまくいきません。渡したオプションオブジェクトも入れようとしましたが、うまくqueue:falseいきません。

4

4 に答える 4

12

jQuery UIは、デフォルトでエフェクトをキューに入れます。dequeue()を使用して、同時に実行します。

    var opt = {duration: 7000};

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

JsFiddleでのデモ

于 2013-04-29T11:36:31.103 に答える
8

さて、これはバウンス効果とハイライト効果を組み合わせた非常にカスタムなソリューションです。{queue:false}を指定して、これらをより簡単に組み合わせるための何らかのjqueryサポートが必要ですが、それほど単純ではないと思います。

私がしたことは、jquery.effects.bounce.jsとjquery.effects.highlight.js(jquery-ui-1.8rc3から)を取得し、DaveSが提案したように、2つのコードを組み合わせて、「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);
于 2010-03-02T03:26:01.933 に答える
4

jQuery UIのエフェクトはアニメーションをキューに入れるので、独自のバージョンのバウンス/ハイライト関数を作成します。両方からソースコードを1つの関数にコピーし、コードをクリーンアップし、animateを呼び出すたびに、バウンスとハイライトのロジックが一緒に含まれていることを確認します。

于 2010-03-01T16:38:17.987 に答える
1

あなたはこれを試すことができます:

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

これにより、両方の効果がほぼ同時に、非同期的に呼び出されるはずです。

于 2010-02-27T00:27:24.593 に答える