After consulting a guide, I found code that determines if an element is within the viewport.
The script functions by comparing the element's position, obtained through getBoundingClientRect()
, with the viewport's height calculated using documentElement.clientHeight
.
In this scenario, to check only if the lower edge overlaps, adjustments were made to evaluate solely domRect.bottom
.
This approach categorizes bottom: 0px;
as overlapping and bottom: 1px;
as non-overlapping. To classify bottom:0px;
as overlapping, increment documentElement.clientHeight
by 1.
function isIntersectingBottom(ele) {
const domRect = ele.getBoundingClientRect();
console.log(domRect.bottom);
console.log(document.documentElement.clientHeight);
return (
domRect.bottom >= document.documentElement.clientHeight + 1
);
}
const ele = document.querySelector('.fixed');
console.log(isIntersectingBottom(ele));
.fixed{
position: fixed;
bottom: -10px;
left: 0px;
width: 300px;
height: 300px;
background: blue;
border: 3px solid red;
}
<div class="fixed"></div>