Skip to content

Instantly share code, notes, and snippets.

@guillaumepiot
Created October 20, 2014 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaumepiot/a61094256997ddd8cfe6 to your computer and use it in GitHub Desktop.
Save guillaumepiot/a61094256997ddd8cfe6 to your computer and use it in GitHub Desktop.
JQUERY - Check if element is in view
# * Add jquery support for checking if an element is in view
$.fn.inView = () ->
$win = $(window)
viewport = {
top: $win.scrollTop()
left: $win.scrollLeft()
}
# No jQuery
# doc = document.documentElement
# left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0)
# top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0)
viewport.right = viewport.left + $win.width()
viewport.bottom = viewport.top + $win.height()
bounds = this.offset()
if not bounds
return false
bounds.right = bounds.left + this.outerWidth()
bounds.bottom = bounds.top + this.outerHeight()
return viewport.right > bounds.right and viewport.bottom > bounds.bottom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment