Run animation when element is visible on screen

可紊 提交于 2019-12-13 04:16:44

问题


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/


回答1:


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

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


来源:https://stackoverflow.com/questions/14521509/run-animation-when-element-is-visible-on-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!