Skip to content

Instantly share code, notes, and snippets.

@dev-w3
Created March 13, 2020 07:06
Show Gist options
  • Save dev-w3/f5e6dc04d664e62f3c58fcab1103039a to your computer and use it in GitHub Desktop.
Save dev-w3/f5e6dc04d664e62f3c58fcab1103039a to your computer and use it in GitHub Desktop.
jQuery event to trigger action when a Element is made visible
jQuery(document).ready(function ($) {
let alreadyThere = false;
$.fn.isVisible = function (topSet = 0) {
_this = $(this);
_this = (typeof _this == 'object' || typeof _this == 'function') ? _this.get(0) : _this;
const rect = _this.getBoundingClientRect();
return (
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top + topSet <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
);
};
$(window).scroll(function () {
if (!alreadyThere && $('.navigation.post-navigation').isVisible()) {
alreadyThere = true; // Perfrom action once
/*
Action Code
*/
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment