如何使用滚动触发的jQuery进行计数?

如何使用滚动触发的jQuery进行计数?,jquery,scroll,count,Jquery,Scroll,Count,嗨,我正在尝试建立一个计数器,当对象滚动到时计数。它在滚动上工作,但当您再次滚动时会刷新。默认间隔设置为1 如何让它只计数一次然后停止? $(window).scroll( function(){ /* Count Up Numbers on Scroll */ $('.count-up').each( function(i){ var bottom_of_object = $(this).offset().top + $(this).outerHeight()

嗨,我正在尝试建立一个计数器,当对象滚动到时计数。它在滚动上工作,但当您再次滚动时会刷新。默认间隔设置为1

如何让它只计数一次然后停止?

$(window).scroll( function(){
    /* Count Up Numbers on Scroll */
    $('.count-up').each( function(i){

        var bottom_of_object = $(this).offset().top + $(this).outerHeight();
        var bottom_of_window = $(window).scrollTop() + $(window).height();

        /* If the object is completely visible in the window, fade it it */
        if( bottom_of_window > bottom_of_object ){

            $(this).countTo(); 
        }
    });
});

您需要有某种“标志”或触发器,在第一次滚动发生后触发,一旦触发,您就不会再次计数,例如:

var countIt=true;
$(窗口)。滚动(函数(){
/*在卷轴上数数数字*/
$('.count up')。每个(函数(i){
变量bottom\u of_object=$(this.offset().top+$(this.outerHeight();
var bottom_of_window=$(window.scrollTop()+$(window.height());
/*如果对象在窗口中完全可见,则为true
然后淡入
*/
if(窗口的底部>对象的底部&&countIt){
$(this.countTo();
/*将countIt标志设置为false,这样它就不会再次播放它
由于这将是错误的,因此将不再满足上述条件
*/
countIt=false;
}
});
});