Last active
October 1, 2016 20:08
-
-
Save englishextra/2e9322e5eea9412f5086f7427009b903 to your computer and use it in GitHub Desktop.
if element is in viewport
This file contains 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
/*! | |
* if element is in viewport | |
* gist.github.com/englishextra/2e9322e5eea9412f5086f7427009b903 | |
* stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport | |
* jsfiddle.net/englishextra/9mwdxgez/ | |
* @param e an HTML element | |
* var p = document.getElementById("h1") || ""; | |
* if(p){var g=function(_that){if(isInViewport(p)) | |
* {window.removeEventListener("scroll",_that);alert(1);}}; | |
* window.addEventListener("scroll",function h_w(){g(h_w);});} | |
*/ | |
var isInViewport = function (w, d) { | |
var g = function (e) { | |
return (e = e ? e.getBoundingClientRect() || "" : "") ? | |
0 <= e.top && 0 <= e.left && e.bottom <= (w.innerHeight || | |
d.clientHeight) && e.right <= (w.innerWidth || | |
d.clientWidth) : !0; | |
}; | |
return g; | |
}(window, document.documentElement || ""); | |
var p = document.getElementById("h1") || ""; | |
if (p) { | |
var g = function (_that) { | |
if (isInViewport(p)) { | |
window.removeEventListener("scroll", _that); | |
alert(1); | |
} | |
}; | |
window.addEventListener("scroll", function h_w() { | |
g(h_w); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment