Created
September 21, 2013 20:49
-
-
Save berryp/6654043 to your computer and use it in GitHub Desktop.
jQuery method to test if any part of a given element is visible in the 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
| /** | |
| * jQuery method to test if any part of a given element is visible in the viewport. | |
| * Source: http://upshots.org/javascript/jquery-test-if-element-is-in-viewport-visible-on-screen | |
| **/ | |
| $.fn.isOnScreen = function () { | |
| var win = $(window); | |
| var viewport = { | |
| top: win.scrollTop(), | |
| left: win.scrollLeft() | |
| }; | |
| viewport.right = viewport.left + win.width(); | |
| viewport.bottom = viewport.top + win.height(); | |
| var bounds = this.offset(); | |
| bounds.right = bounds.left + this.outerWidth(); | |
| bounds.bottom = bounds.top + this.outerHeight(); | |
| return (!(viewport.right < bounds.left || viewport.left > bounds.right || | |
| viewport.bottom < bounds.top || viewport.top > bounds.bottom)); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment