I'm using the Intersection Observer API on a site. For every instance of using it, I'm using the same config (main viewport). My issue is that I need to have different things happen when the callback is fired. For some, I want to lazy load an image. For some, I want to initialize a carousel, etc.
Is there a way to use the same observer for all of these different applications or do I have to use a different observer for each unique callback?
You can reuse the same intersection observer and same callback for all your different targets, since the callback is provided the target element you can use that information to customize what the callback does.
In example below I change the message on the screen depending on which differently colored
div
is in view reusing the same IntersectionObserver instance and same callback function: