Skip to content

Instantly share code, notes, and snippets.

@inter-coder
Last active August 29, 2015 14:21
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save inter-coder/8d21a7f096a416836609 to your computer and use it in GitHub Desktop.
One of the ways to find out if element is in the visible part of the screen or not
HTMLElement.prototype.isVisible = function(){
function getPositionTop(element){
var offset = 0;
while(element) {offset += element["offsetTop"];element = element.offsetParent;}
return offset;
}
var posTop = getPositionTop(this);
var posBottom = posTop + this.offsetHeight;
var visibleTop = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
var visibleBottom = visibleTop + document.body.offsetHeight;
var isInsideLeftRightWindowMargins=((document.body.offsetWidth>=this.offsetLeft) && (this.offsetLeft+this.offsetWidth)>=0);
return ((posBottom >= visibleTop) && (posTop <= visibleBottom) && isInsideLeftRightWindowMargins);
};
document.getElementById("someId").isVisible();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment