Skip to content

Instantly share code, notes, and snippets.

@matori
Last active August 29, 2015 14:15
Show Gist options
  • Save matori/5f61f39b9a2abb982f1e to your computer and use it in GitHub Desktop.
Save matori/5f61f39b9a2abb982f1e to your computer and use it in GitHub Desktop.
element is in viewport?
'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