Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment