Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Looping Graph Animation

I saw this codepen: https://codepen.io/alex_rodrigues/pen/ogYZdr You can see the javascipt code here:

setTimeout(function start (){
  
  $('.bar').each(function(i){  
    var $bar = $(this);
    $(this).append('<span class="count"></span>')
    setTimeout(function(){
      $bar.css('width', $bar.attr('data-percent'));      
    }, i*100);
  });

  $('.count').each(function () {
    $(this).prop('Counter',0).animate({
        Counter: $(this).parent('.bar').attr('data-percent')
    }, {
        duration: 2000,
        easing: 'swing',
        step: function (now) {
            $(this).text(Math.ceil(now) +'%');
        }
    });
  });

}, 500)

I want the graph to complete the animation, stall for 3-5 seconds then repeat the animation. Currently, it completes the animation, but there is no loop. Any help is much appreciated. Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create a function that:

  1. Resets the bar's width and content
  2. Restarts the animation.

This function can be set to be called after:

($('.bar').length - 1) * 100 + 2000 + your_chosen_delay_in_ms

For this to work, we have to pull out all the things that don't need repeating, like $(this).append('<span class="count"></span>').

Combined, it would look like this:

var animation_offset = 100;
var animation_duration = 2000;
var reanimate_delay = 3000;

function animateBars() {
    $('.bar').each(function(i){  
        var $bar = $(this);
        setTimeout(function(){
            $bar.css('width', $bar.attr('data-percent'));      
        }, i*animation_offset);
    });

    $('.count').each(function () {
        $(this).prop('Counter',0).animate({
            Counter: $(this).parent('.bar').attr('data-percent')
        }, {
            duration: animation_duration,
            easing: 'swing',
            step: function (now) {
                $(this).text(Math.ceil(now) +'%');
            }
        });
    });

    setTimeout(
        reAnimateBars,
        (
            ($('.bar').length - 1) * animation_offset // Moment the last bar starts animating
            + animation_duration // Animation duration
            + reanimate_delay // Your chosen delay
        )
    );
}

function reAnimateBars() {
    // Reset the bars
    $('.bar .count').text('');
    $('.bar').css('width', '0%');
    // This will take some time (as per CSS transition property) to reset.
    // So we have to wait for that too.

    // Start the animation (again)
    // after CSS transition time for resetting is over
    setTimeout(animateBars, animation_duration);
}

setTimeout(function() {
    // Put non-repeating things here:
    $('.bar').append('<span class="count"></span>');

    // Start animation (first time)
    animateBars();
}, 500);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda