I think the IntersectionObserver would be good for this:
The code was taken from a previous thread so hopefully is robust enough.
I think the IntersectionObserver would be good for this:
The code was taken from a previous thread so hopefully is robust enough.