Created
January 1, 2019 16:53
-
-
Save banhaclong20/2fe814a32856a6a6738fb00364937ce4 to your computer and use it in GitHub Desktop.
If ele on screen - js
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
// A simple jQuery function to check if the element is visible on screen | |
// https://gist.github.com/agaase/6648737 | |
$.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