I'm using the following function to check if an element is visible or not in the viewport :
function elementInViewport(el) {
let top = el.offsetTop;
let left = el.offsetLeft;
const width = el.offsetWidth;
const height = el.offsetHeight;
while (el.offsetParent) {
el = el.offsetParent;
top += el.offsetTop;
left += el.offsetLeft;
}
return (
top < (window.pageYOffset + window.innerHeight)
&& left < (window.pageXOffset + window.innerWidth)
&& (top + height) > window.pageYOffset
&& (left + width) > window.pageXOffset
);
}
However, I want to check if an element is 50% visible in the viewport. So if half of it is only showing, the check should return true. I know this is possible using the Intersection Observer API, however that is not an option for me as I want this to be compatible with IE11.