In this example obviously my div intersects the bottom of the page, how can I know from javascript if this element is intersecting it or not?
if for example: bottom:0px;
not should be intersected
.fixed{
position:fixed;
bottom:-10px;
left:0px;
width:300px;
height:300px;
background:blue;
border:3px solid red;
}
<div class="fixed"></div>
2
Answers
I took reference from this tutorial, where the code checks to see if the element is in the viewport.
The code works by checking the element’s position using
getBoundingClientRect()
against the viewport’s height usingdocumentElement.clientHeight
.In your case, since you only want to check if the bottom border overlaps, I modified the code such that only
domRect.bottom
is checked.Finally, this method will show
bottom:0px;
as intersecting, andbottom: 1px;
as non-intersecting. If you wantbottom:0px;
to intersect, add 1 todocumentElement.clientHeight
You can use getBoundingClientRect() function. It provides information about the size of an element and its position relative to the viewport.
Then by comapring it to
window.innerHeight
you can tell if it intersects the screen or not.