One of the ways to find out if element is in the visible part of the screen or not
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
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