javascript - How to make a counter starts at specific part has an id at a section -
i have used code make counter
(function ($) { $.fn.countto = function (options) { options = options || {}; return $(this).each(function () { // set options current element 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); // how many times update value, , how increment value on each update var loops = math.ceil(settings.speed / settings.refreshinterval), increment = (settings.to - settings.from) / loops; // references & variables change each update var self = this, $self = $(this), loopcount = 0, value = settings.from, data = $self.data('countto') || {}; $self.data('countto', data); // if existing interval can found, clear first if (data.interval) { clearinterval(data.interval); } data.interval = setinterval(updatetimer, settings.refreshinterval); // initialize element starting value render(value); function updatetimer() { value += increment; loopcount++; render(value); if (typeof(settings.onupdate) == 'function') { settings.onupdate.call(self, value); } if (loopcount >= loops) { // remove 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, // number element should start @ to: 0, // number element should end @ speed: 1000, // how long should take count between target numbers refreshinterval: 100, // how element should updated decimals: 0, // number of decimal places show formatter: formatter, // handler formatting value before rendering onupdate: null, // callback method every time element updated oncomplete: null // callback method when element finishes updating }; 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, ','); } }); // start timers $('.timer').each(count); function count(options) { var $this = $(this); options = $.extend({}, options || {}, $this.data('counttooptions') || {}); $this.countto(options); } });
the problem is, counter starts count when site opened. want make counter starts when scrolling part @ section. p.s part has id , there link href id when clicking on scroll part
<h3> <a href="#counts" class="page-scroll">be part of our story</a> </h3> <div class="col-md-4 col-xs-12" > <div class="counts text-center"> <i class="fa fa-cogs fa-4x" aria-hidden="true"></i> <h3 class="">workshops</h3> <p> <h1 class="timer count-title sr-icons" id="count-number" data to="300" data-speed="1500"></h1> </p> </div> </div>
can me solve problem?
Comments
Post a Comment