Last active
August 29, 2015 14:15
-
-
Save matori/5f61f39b9a2abb982f1e to your computer and use it in GitHub Desktop.
element is in viewport?
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
'use strict' | |
# http://stackoverflow.com/a/7557433 | |
# https://gist.github.com/anonymous/f09b5cd487b5ad55c46e | |
module.exports = (element, partial = false, position = 0.85)-> | |
rect = element.getBoundingClientRect() | |
windowHeight = window.innerHeight or document.documentElement.clientHeight | |
windowWidth = window.innerWidth or document.documentElement.clientWidth | |
unless partial | |
rect.top >= 0 and | |
rect.left >= 0 and | |
rect.bottom <= windowHeight and | |
rect.right <= windowWidth | |
else | |
rect.left >= 0 and rect.right <= windowWidth and | |
((rect.top >= 0 and rect.top <= windowHeight * position) or | |
(rect.bottom >= windowHeight * (1 - position) and rect.bottom <= windowHeight)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment