2017-02-13 15 views
0

私はビューポートにあるときにターゲットまで数値を数えるこの数値カウンタスクリプトを構築しました。残念ながら、何らかの理由で再びカウントダウンされます。 http://jsfiddle.net/tw6g2oeu/325/Jqueryナンバーカウンタは一度だけ

がどのように私は戻ってカウントから機能を停止することができます:

$(window).scroll(function(){ 
    // This is then function used to detect if the element is scrolled into view 
    function elementScrolled(elem) 
    { 
    var docViewTop = $(window).scrollTop(); 
    var docViewBottom = docViewTop + $(window).height(); 
    var elemTop = $(elem).offset().top; 
    return ((elemTop <= docViewBottom) && (elemTop >= docViewTop)); 
    } 

    // This is where we use the function to detect if ".numbers" is scrolled into view 
    if(elementScrolled('.numbers')) { 

$('.arv').each(function() { 
    $(this).prop('Counter',0).animate({ 
     Counter: $(this).text() 
    }, { 
     duration: 4000, 
     easing: 'linear', 
     step: function (now) { 
      $(this).text(Math.ceil(now)); 
     } 
    }); 
}); 

    } 
}); 

ここJSFiddleですか? jQueryのウェイポイントプラグインを使用して終了

EDIT:

jQuery('.numbers').waypoint(function() { 
    $('.arv').each(function() { 
    $(this).prop('Counter',0).animate({ 
     Counter: $(this).text() 
    }, { 
     duration: 4000, 
     easing: 'linear', 
     step: function (now) { 
      $(this).text(Math.ceil(now)); 
     } 
    }); 
}); 
this.destroy() 
},{offset:'75%'}); 

this.destroy()。メソッドは、複数回の発火を防ぎます。

+0

あなたはデバウンス*あなたのスクロールイベント(他のオプションがあります)*にする必要があります。基本的には、ウィンドウが微量にスクロールするたびに、あなたのイベントが発生します。これは、100秒の時間であり、それぞれが4秒でイン・アウトを緩和します。表示するハンドラの中に 'console.log(" scroll ")を追加してください。 –

+0

スクロールイベントは何回トリガされたか分かりません。イベントなしで達成しようとするものを達成することができます。これを確認してくださいhttp://stackoverflow.com/questions/487073/check-if-element-is-visible-after-scrolling – Khaleel

答えて

1

jQueryタグを追加して以来です。 jQueryウェイポイントプラグインを使用することができます。それはあなたのために仕事をするでしょう。

USAGE:

$('.entry').waypoint(function() { 
    alert('You have scrolled to an entry.'); \\ increment your counter here 
});