Hi.
I had a small issue needing to only initialize a jquery function once, and only when a specified element is in view…as well as checking to see if the element is in view and initializing if so on initial page load.
So here’s the results..cobbled together from various online sources and tips.
//only trigger on element, and only trigger once (countup)
$(function() {
var done = false;
var oTop = $(‘#countUp’).offset().top – window.innerHeight;
//on load
var pTop = $(‘body’).scrollTop();
if( pTop > oTop ){
start_count();
}
//on scroll
$(window).scroll(function(){
var pTop = $(‘body’).scrollTop();
if( pTop > oTop ){
if (!done){
start_count();
done = true;
}
}
});
});