Search code examples
jqueryvisible

Run animation when element is visible on screen


I've a little progress bars script. It works fine but runs on page load. I want to run animation when bars are visible on screen (it should work too if progress bars would be in tabs). How may I get this?

Here's script:

setTimeout(function(){

    $('.skill-bar .skill-bar-content').each(function() {
        var me = $(this);
        var perc = me.attr("data-percentage");

        var current_perc = 0;

        var progress = setInterval(function() {
            if (current_perc>=perc) {
                clearInterval(progress);
            } else {
                current_perc +=1;
                me.css('width', (current_perc)+'%');
            }

            me.text((current_perc)+'%');

        }, 10);

    });

},10);

jsFiddle: http://jsfiddle.net/fUyYL/


Solution

  • Something like this? http://jsfiddle.net/fUyYL/1/

    var ele = $('#skill-bars');
    if( ele.is(':visible') ) {
        ...
    }