Created
March 13, 2020 07:06
-
-
Save dev-w3/f5e6dc04d664e62f3c58fcab1103039a to your computer and use it in GitHub Desktop.
jQuery event to trigger action when a Element is made visible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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