javascript - How to tell what fraction of element is inside viewport? -
how determine percentage of dom element inside current viewport ? want calculate ratio of area of element inside viewport , total area of element.
related question: how tell if dom element visible in current viewport?
see getboundingclientrect
, window.innerheight
.
let {top, height} = element.getboundingclientrect(), percentvisible = math.max(0, math.min(1, (window.innerheight - top) / height));
i'm guessing @ use case, in modern browsers, see intersectionobserver
, intersectionratio
.
Comments
Post a Comment