Jquery Pulsate Times

Jor*_*rdy 10 jquery effects

我使用Pulsate函数(http://docs.jquery.com/UI/Effects/Pulsate).使用参数'times',我可以设置元素脉动的时间.默认值为5,但如何设置元素将无限脉动.

Ali*_*guy 32

我建议你根本不要使用JQueryUI - 它是一个非常简单的动画,可以在不加载UI的情况下轻松完成:

// DOM ready
$(function(){
    // Self-executing recursive animation
    (function pulse(){
        $('#my_div').delay(200).fadeOut('slow').delay(50).fadeIn('slow',pulse);
    })();
});
Run Code Online (Sandbox Code Playgroud)

工作演示:http://jsfiddle.net/AlienWebguy/bSWMC/

正如您所看到的,可以通过更改淡入淡出的速度和延迟的持续时间来轻松调整它.

原始的JQueryUI pulsate()函数使用for循环,times因此您无法在不更改插件逻辑的情况下使用此插件实现结果:

$.effects.pulsate = function(o) {
    return this.queue(function() {
        var elem = $(this),
            mode = $.effects.setMode(elem, o.options.mode || 'show');
            times = ((o.options.times || 5) * 2) - 1;
            duration = o.duration ? o.duration / 2 : $.fx.speeds._default / 2,
            isVisible = elem.is(':visible'),
            animateTo = 0;

        if (!isVisible) {
            elem.css('opacity', 0).show();
            animateTo = 1;
        }

        if ((mode == 'hide' && isVisible) || (mode == 'show' && !isVisible)) {
            times--;
        }

        for (var i = 0; i < times; i++) {
            elem.animate({ opacity: animateTo }, duration, o.options.easing);
            animateTo = (animateTo + 1) % 2;
        }

        elem.animate({ opacity: animateTo }, duration, o.options.easing, function() {
            if (animateTo == 0) {
                elem.hide();
            }
            (o.callback && o.callback.apply(this, arguments));
        });

        elem
            .queue('fx', function() { elem.dequeue(); })
            .dequeue();
    });
};
Run Code Online (Sandbox Code Playgroud)


Kev*_*Pei 12

Jordy,您可以使用脉动器内部功能来完成上述操作.

//the function
function pulsateforever(){
$("element").effect("pulsate", { times:1 }, pulsatetime,function(){
//repeat after pulsating
pulsateforever();
});
}
//call function when DOM is ready
$(function(){
pulsateforever();
});
Run Code Online (Sandbox Code Playgroud)

确保使用要脉动的元素替换元素,并以您希望脉动的速度替换脉冲时间.