Created
October 20, 2014 14:03
-
-
Save guillaumepiot/a61094256997ddd8cfe6 to your computer and use it in GitHub Desktop.
JQUERY - Check if element is in view
This file contains 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
# * 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