I want the animation to happen everytime I scroll down (only down, not up). I have come up with a js code but it works for both scrolling up and down.
var scroll = window.requestAnimationFrame || function(callback){
window.setTimeout(callback,1000/60)
};
var elements = document.querySelectorAll(".question-box");
function loop(){
elements.forEach(element => {
if (isElementInViewport(element)){
element.classList.add("visible");
}else{
element.classList.remove("visible");
}
});
scroll(loop);
}
loop();
// Helper function from: http://stackoverflow.com/a/7557433/274826
function isElementInViewport(el) {
// special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
(rect.top <= 0
&& rect.bottom >= 0)
||
(rect.bottom >= (window.innerHeight || document.documentElement.clientHeight) &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight))
||
(rect.top >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight))
);
}