Last active
January 29, 2020 08:41
-
-
Save tiarno/ec445e2e6c5141cbb893 to your computer and use it in GitHub Desktop.
javascript: is element inside an iframe visible within the viewport?
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
// Given an iframe id and an anchor id that is present within the iframe, | |
// determine whether the element is visible/present inside the window viewport. | |
// This is not about the css 'display' property; this shows whether | |
// the window viewport contains the element. | |
var isVisible = function (anchor, iframe_id) { | |
var ifrId = iframe_id || 'bv_page'; | |
var ifrOffset = window.parent.document.getElementById(ifrId).offsetTop; | |
var myloc = document.getElementById(anchor).offsetTop + ifrOffset; | |
var viewtop = window.parent.scrollY; | |
var viewbot = viewtop + window.parent.innerHeight; | |
if (viewbot > myloc && myloc > viewtop){ | |
return true; | |
} else { | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful for testing internal links on a webpage with iframes, especially when links are intercepted by javascript out of your control. This script can be used to show whether the link target is in view after the link is clicked.