Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntersectionObserver #6

Open
michaelrafailyk opened this issue Jan 15, 2021 · 4 comments
Open

IntersectionObserver #6

michaelrafailyk opened this issue Jan 15, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@michaelrafailyk
Copy link
Owner

michaelrafailyk commented Jan 15, 2021

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

@michaelrafailyk
Copy link
Owner Author

michaelrafailyk commented Jan 15, 2021

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
Copy link
Owner Author

michaelrafailyk commented Jan 15, 2021

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
Copy link
Owner Author

michaelrafailyk commented Jan 17, 2021

One more way how to watch element out of viewport.

  • Use a while to check if either parent has an overflow: hidden.
  • If yes – we create a new fake element with position: absolute pointer-events: none left: 0px top: 0px width: 100% height: 100% and place it before our original element (that can be out of viewport).
  • Link original element to fake element any way you decide (property with unique name, for example).
  • Place fake element to IntersectionObserver instead of original element.

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

problem-explanation-4

@michaelrafailyk michaelrafailyk added the enhancement New feature or request label Feb 1, 2021
@michaelrafailyk
Copy link
Owner Author

michaelrafailyk commented Feb 4, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant