Skip to content

Instantly share code, notes, and snippets.

@banhaclong20
Created January 1, 2019 16:53
Show Gist options
  • Save banhaclong20/2fe814a32856a6a6738fb00364937ce4 to your computer and use it in GitHub Desktop.
Save banhaclong20/2fe814a32856a6a6738fb00364937ce4 to your computer and use it in GitHub Desktop.
If ele on screen - js
// 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