michaelrafailyk / stickymate

Stickymate is a tool that designed to help web developers easily create scroll-based animations without JavaScript knowledge, just setting animation params directly in the HTML markup.
https://michaelrafailyk.github.io/stickymate/
MIT License
63 stars 5 forks source link

IntersectionObserver #6

Open michaelrafailyk opened 3 years ago

michaelrafailyk commented 3 years ago

Starting work on alternate way of watching elements with using Intersection Observer to avoid constant listening to the scroll event.

michaelrafailyk commented 3 years ago

I tried next scheme. When the Observer starts to see an element in the viewport, the window scroll event listener starts to watch this element. When an element leaves the viewport, the listener been removed. I believe it's good for performance.

The first tests revealed possible problems. If the transformation moves the element horizontally outside the viewport, the element can never go back, since the Observer will not see it when scrolling vertically. ๐Ÿ˜• problem-explanation

michaelrafailyk commented 3 years ago

All right, we can add rootMargin with 0px 100vw to IntersectionObserver, it is extend range of watching horizontally.

problem-explanation

1 situation. Element outside of viewport horizontally (but in range of rootMargin). Parent of element and every elements upper including body have overflow: auto (it mean default). The observer sees the element. Work well๐Ÿ‘ŒBut devs will avoid this situation because page will have horizontal scrollbar ๐Ÿ˜•

2 situation. Element outside of viewport horizontally (but in range of rootMargin). Parent of element or one of upper elements have overflow: hidden. Itโ€™s no horizontal scrollbar now but IntersectionObserver can't see this element because it is out of document layout ๐Ÿ˜•

michaelrafailyk commented 3 years ago

One more way how to watch element out of viewport.

Pros: Observer watching of fake element that is possible to observe. Cons: Additional elements in DOM.

problem-explanation-4

michaelrafailyk commented 3 years ago

I figured out, when scrolling fast, Intersection Observer is missing a moment of intersection about 1/4 of second. It mean if user scrolling very fast he can loose a first frames of animation when element appearing on the viewport. So yes, reaction is not up in time and people on the web telling about it. May be it is not a problem, keep watching.