简体   繁体   中英

Execute JQuery Function on div view

I wonder how to activate this jQuery when the 'about' div is in view. or maybe when we scroll over the div.

(function($) {
  $.fn.countTo = function(options) {
    options = options || {};

    return $(this).each(function() {

      var settings = $.extend({}, $.fn.countTo.defaults, {
        from: $(this).data('from'),
        to: $(this).data('to'),
        speed: $(this).data('speed'),
        refreshInterval: $(this).data('refresh-interval'),
        decimals: $(this).data('decimals')
      }, options);


      var loops = Math.ceil(settings.speed / settings.refreshInterval),
        increment = (settings.to - settings.from) / loops;


      var self = this,
        $self = $(this),
        loopCount = 0,
        value = settings.from,
        data = $self.data('countTo') || {};

      $self.data('countTo', data);


      if (data.interval) {
        clearInterval(data.interval);
      }
      data.interval = setInterval(updateTimer, settings.refreshInterval);


      render(value);

      function updateTimer() {
        value += increment;
        loopCount++;

        render(value);

        if (typeof(settings.onUpdate) == 'function') {
          settings.onUpdate.call(self, value);
        }

        if (loopCount >= loops) {
          // remove the interval
          $self.removeData('countTo');
          clearInterval(data.interval);
          value = settings.to;

          if (typeof(settings.onComplete) == 'function') {
            settings.onComplete.call(self, value);
          }
        }
      }

      function render(value) {
        var formattedValue = settings.formatter.call(self, value, settings);
        $self.html(formattedValue);
      }
    });
  };

  $.fn.countTo.defaults = {
    from: 0,
    to: 0,
    speed: 1000,
    refreshInterval: 100,
    decimals: 0,
    formatter: formatter,
    onUpdate: null,
    onComplete: null
  };

  function formatter(value, settings) {
    return value.toFixed(settings.decimals);
  }
}(jQuery));

jQuery(function($) {
  // custom formatting example
  $('.count-number').data('countToOptions', {
    formatter: function(value, options) {
      return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
    }
  });


  $('.timer').each(count);

  function count(options) {
    var $this = $(this);
    options = $.extend({}, options || {}, $this.data('countToOptions') || {});
    $this.countTo(options);
  }
  $(function() {
    $('div[onload]').trigger('onload');
  });
});

You can set your code as a non anonymous function and use jQuery's Waypoints plugin to trigger the function.

If you don't want to use a plugin then you can trigger the function inside a $(window).scroll() and make some calculations. First comment out your line this line: $('.timer').each(count); . You'll wrap that line inside the scroll checker like this:

//listening scrolling action

$(window).scroll(function() {
    var div_start = $("#about-row-b").offset().top;
    var div_final = $("#about-row-b").offset().top + $("#about-row-b").outerHeight();
    var viewport_end = $(window).scrollTop() + $(window).height();

    if((viewport_end > div_start) && (viewport_end < div_final)){
        // v v v call your function here!!!
        $('.timer').each(count);
    }
});

If you are only interested on the numbers going up you might want to check Benjamin Intal's Counter Up jQuery plugin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM