Created
May 15, 2014 20:03
-
-
Save catdad/ef90479dca9a688d6c8e to your computer and use it in GitHub Desktop.
Checks if an element is visible inside another element.
This file contains hidden or 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
//this code will check if there is any overlap between two elements | |
//it was made to be used to check if a portion of a scrollably div is visible inside its wrapper | |
function isInView(containerBB, elBB) { | |
return (!( | |
elBB.top >= containerBB.bottom || | |
elBB.left >= containerBB.right || | |
elBB.bottom <= containerBB.top || | |
elBB.right <= containerBB.left | |
)); | |
} | |
//usage | |
var container = document.getElementById('containerID'); | |
var content = document.getElementById('contentID'); | |
var isVisible = isInView( container.getBoundingClientRect(), content.getBoundingClientRect() ); | |
// based on a solution in http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment