R
R
Rasul Gitinov2018-03-02 10:57:35
JavaScript
Rasul Gitinov, 2018-03-02 10:57:35

Starting counters of digits when scrolling to them, how to do it?

There is a function that runs a digit counter:

$('.counter').each(function() {
  $(this).prop('Counter', $(this).data('from')).animate({
    Counter: $(this).text()
  }, {
    duration: $(this).data('duration'),
    easing: 'swing',
    step: function(now) {
      $(this).text(Math.ceil(now))
    }
  })
});

The counter works for elements with the ".counter" class, only these elements are scattered across the page.

I wanted to make them start calculating when the user scrolls to them, but when I wrote such a function, it did not work correctly, because they have one class and the function cannot understand that you need to monitor each element separately.

Please tell me how to implement this function, thanks.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
aloky, 2018-03-02
@aloky

https://webkab.ru/dejstvie-pri-poyavlenii-elementa... - I think it will help

E
Evgeny Kalibrov, 2018-03-02
@rework

Are you seriously? Today I already answered this question and gave the code How to run a js script at a specific place?
I 'll duplicate it again:
Put the code in a function and call it on the scroll event when it reaches the element you need:

function start_count() {
    $('.count').each(function () {
        $(this).prop('Counter',0).animate({
            Counter: $(this).text()
        }, {
            duration: 5555,
            easing: 'swing',
            step: function (now) {
                $(this).text(Math.ceil(now));
            }
        });
    });
}

$(function() {
    var oTop = $('.count').offset().top - window.innerHeight;
    $(window).scroll(function(){
        var pTop = $('body').scrollTop();
        if( pTop > oTop ){
            start_count();
        }
    });
});

A
Aginvlad, 2019-01-30
@Aginvlad

https://t.me/feenproject/16

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question