Basic jQuery animation: Elipsis (three dots sequentially appearing)

后端 未结 4 2049
余生分开走
余生分开走 2021-02-04 17:24

What I need:

I need an animated elipisis (...), one dot appearing after the other. The animation needs to loop. I\'d like to achieve this via jQuery

Animation

4条回答
  •  清酒与你
    2021-02-04 18:10

    The following code is essentially what I ended up with.

    JavaScript:

    var animatedDot;
    animatedDot = animatedDot || (function () {
        var dots = 0;
        var animatedDotInterval;
        var selectorAnimatedDot = ".animatedDot";
    
        return {
            start: function(interval) {
                if (!interval)
                    interval = 400;
    
                animatedDotInterval = setInterval(this.nextFrame, interval);
            },
            stop: function() {
                if (animatedDotInterval)
                    clearInterval(animatedDotInterval);
            },
            nextFrame: function() {
                if ($(selectorAnimatedDot).length) {
                    if (dots < 3) {
                        $(selectorAnimatedDot).append('.');
                        dots++;
                    } else {
                        $(selectorAnimatedDot).html('');
                        dots = 0;
                    }
                } else {
                    if (animatedDotInterval)
                        clearInterval(animatedDotInterval);
                }
            }
        };
    })();
    
    function animatedDotTimeout(timeout) {
        if (!timeout)
            timeout = 10000;
    
        animatedDot.start();
    
        setTimeout(animatedDot.stop, timeout);
    }
    

    Html:

    Loading
    
    
    

提交回复
热议问题