I'm trying to add a class to all instances of the .column
class that are in the viewport.
This JS Fiddle add a class .swoosh
to all .column
in the viewport, but when I use the same exact js with my markup it does not add the .swoosh
class to the .column
in the viewport.
- I checked if jquery is loading and it is.
- The code is valid because it works on JS Fiddle
But for some reason, the .column
in the viewport in this page are not getting the class .swoosh
.
Here is the code I am trying to run:
function isElementInViewport(el) {
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document. documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document. documentElement.clientWidth)
);
}
$.fn.checkViewportAndSetClass = function() {
$(this).each(function(){
if (isElementInViewport(this)) {
$(this).addClass("swoosh");
}
});
};
$('.column').checkViewportAndSetClass();
$(window).on("scroll", function() {
$('.column').checkViewportAndSetClass();
});