Skip to content

Instantly share code, notes, and snippets.

@dyaa
Created September 27, 2017 09:16
Show Gist options
  • Save dyaa/c5ec181a38cc5d8da6b096746fdeef4c to your computer and use it in GitHub Desktop.
Save dyaa/c5ec181a38cc5d8da6b096746fdeef4c to your computer and use it in GitHub Desktop.
Check if a particular element is in view.
// Let me show approch without JQuery. Simple JS function:
function isVisible(elem) {
var coords = elem.getBoundingClientRect();
var topVisible = coords.top > 0 && coords.top < 0;
var bottomVisible = coords.bottom < shift && coords.bottom > 0;
return topVisible || bottomVisible;
}
// Short example how to use it:
var img = document.getElementById("pic1");
if (isVisible(img)) { img.style.opacity = "1.00"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment