0

On a Wordpress page, I have this code to start a count-up, but I only want it to start counting when the user has scrolled far enough down the page so that it appears onscreen (it's in a containing DIV called "three-counter-section").

How do you retrofit this code to enable that?

$('.counter').each(function() {
var $this = $(this),
countTo = $this.attr('data-count');
$({ countNum: $this.text()}).animate({
countNum: countTo
},

{ duration: 11000,
easing:'easeInOutCubic',
step: function() {
$this.text(Math.floor(this.countNum));
},
complete: function() {
$this.text(this.countNum);
}
});  
});
MacGyver_97
  • 229
  • 3
  • 14
  • Possible duplicate of [JQuery: if div is visible](https://stackoverflow.com/questions/40468981/jquery-if-div-is-visible) – Obsidian Age Jun 12 '19 at 21:37
  • Not a duplicate of the proposed answer. That seems to apply only if something was visible/invisible via CSS (and I tried it anyway, but didn't work). – MacGyver_97 Jun 12 '19 at 22:33

1 Answers1

0

You can simple do following to trigger an event when any element is visible.

setInterval(function(){
  if($('.div1').is(':visible')){
    //do something
  }
  else {
    //do nothing..
  }      
}, 100);

** Change .div1 as you needed.

Praveen Govind
  • 2,619
  • 2
  • 32
  • 45