Start animation on progress bar when its visible on screen

后端 未结 3 883
青春惊慌失措
青春惊慌失措 2021-02-06 19:04

I want to create a webpage that contains several sections. In one of those sections are something like progress bars. These progress bars are \'animated\' so that the user sees

3条回答
  •  [愿得一人]
    2021-02-06 19:24

    Let me try something

        function startProgressBar() {
    
                $(".meter > span").each(function() {
                    $(this)
                        .data("origWidth", $(this).width())
                        .width(0)
                        .animate({
                            width: $(this).data("origWidth")
                        }, 1200);
                });
    
        }
    
    $(window).scroll(function() {
        var target = $('#third');
        var targetPosTop = target.position().top; // Position in page
        var targetHeight = target.height(); // target's height
        var $target = targetHeight + targetPosTop; // the whole target position
        var $windowst = $(window).scrollTop()-($(window).height()/2);     // yes divided by 2 to get middle screen view.
    
        if (($windowst >= $targetPosTop) && ($windowst < $target)){
                     // start progressbar I guess
                     startProgressBar();
        }
    
    });
    

    Give it a try, let me know.

提交回复
热议问题