Counter-Up2 icon indicating copy to clipboard operation
Counter-Up2 copied to clipboard

Only counts the first element with the class "counter"

Open paulhoppewhk opened this issue 3 years ago • 9 comments

I have three numbers next to each other I want to count up, but only the first one is counting. I tested this with adding an fourth one on top and only that one was counted. How can I archive that all numbers with the "counter" class get counted up?

I was using the previevs version "counterUp" and all numbers where counted but, but I had an issue that the final numbers change after scrolling, that is why I switched to "counterUp 2.

paulhoppewhk avatar Nov 29 '21 14:11 paulhoppewhk

I've also found this issue.

Using this with Wordpress ACF Repeater Fields and I have 4 columns in a loop. Only the first is using the counter, the rest do not.

RobertHowdle avatar Dec 02 '21 11:12 RobertHowdle

Same

valeriaedw avatar Jan 04 '22 23:01 valeriaedw

Had the same issue, used the following code to make it work-

const counters = document.querySelectorAll( '.counter' );
for(const el of counters) {
	counterUp( el, {
		duration: 1000,
		delay: 16,
	})
}

tripleten avatar Jan 05 '22 17:01 tripleten

Same

Please check my last message.

tripleten avatar Jan 06 '22 05:01 tripleten

From the example in the readme file with Intersection Obersver:

const callback = entries => {
	entries.forEach( entry => {
		const el = entry.target
		if ( entry.isIntersecting ) ) {
			counterUp( el, {
				duration: 2000,
				delay: 16,
			} )
		}
	} )
}

const IO = new IntersectionObserver( callback, { threshold: 1 } )

const el = document.querySelector( '.counter' )
IO.observe( el )

This only targets one element.

If you want to add to multiple elements you need to add a loop inside the if statement.

Say you have the following in HTML:

<div class="counters">
  <span class="counter">10</span>
  <span class="counter">20</span>
  <span class="counter">30</span>
</div>

Then your javascript goes as follows:

  1. Include dependency

<script src="https://unpkg.com/[email protected]/dist/index.js"></script>

  1. Add script to your js file or inline
<script type="text/javascript">

const counterUp = window.counterUp.default

const callback = entries => {
	entries.forEach( entry => {
		const el = entry.target
		if ( entry.isIntersecting && ! el.classList.contains( 'is-visible' ) ) {
                    for(const counter of counters) {
      	              counterUp( counter, {
      		              duration: 1000,
      		              delay: 16,
      	              })
                    el.classList.add( 'is-visible' )
                  }
	    }
	} )
}

// observer
const IO = new IntersectionObserver( callback, { threshold: 1 } )

// First element to target
const el = document.querySelector( '.counter' )

// all numbers
const counters = document.querySelectorAll( '.counter' )
IO.observe( el )

</script>

This should work for you.

joelveloso avatar Feb 16 '22 10:02 joelveloso

@joelveloso Thanks, that makes all elements with .counter count, however they count all simultaneously and not when each an one of the elements come into viewport. Any hints how to accomplish that?

siavashvj avatar Feb 23 '22 22:02 siavashvj

To use on multiple elements throughout a page, give each a new class name (e.g. 'counter2, counter3, etc.) and amend the bottom of the initialization script like this:

const el = document.querySelector( '.counter' ); IO.observe( el ); const el2 = document.querySelector('.counter2'); IO.observe(el2); const el3 = document.querySelector('.counter3'); IO.observe(el3);

maxms avatar Mar 16 '22 15:03 maxms

@joelveloso Thanks, that makes all elements with .counter count, however they count all simultaneously and not when each an one of the elements come into viewport. Any hints how to accomplish that?

I'd also be keen on this, could anyone provide an example please?

Ben-Atherton avatar Sep 08 '22 15:09 Ben-Atherton

The solution by @joelveloso works for me however on pages that do not have the counter I get the following error:

Uncaught TypeError: Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element'.

Any ideas on how to address this?

peterjrees avatar Jul 04 '23 07:07 peterjrees