Created
September 27, 2017 09:16
-
-
Save dyaa/c5ec181a38cc5d8da6b096746fdeef4c to your computer and use it in GitHub Desktop.
Check if a particular element is in view.
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
| // 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