Skip to content
🔎 Intersection Observer API wrapper for Blazor applications
C# TypeScript JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE Update issue templates May 25, 2019
src Merge branch 'feat/dev' of… Jun 3, 2019
.gitattributes feat(project): initial commit May 25, 2019
Blazor.IntersectionObserver.sln feat(tests): some dotnet tests Jun 3, 2019 feat(conduct): added code of conduct May 25, 2019
LICENCE fix(docs): service or component text Jun 4, 2019
codecoverage.runsettings feat(tests), chore(docs): added props for the component Jun 4, 2019


Package Version NuGet Downloads License Build Status

A comprehensive wrapper around the Intersection Observer API, giving you all the goodness of observing intersections in a performant way.

This is a wrapper around the Intersection Observer API so that you can use it in Blazor. It has the same API structure with convenience methods and components for a better dev experience.

Get Started

1. Installation

Install BlazorIntersectionObserver through NuGet.

> dotnet add package BlazorIntersectionObserver

2. Register the service

Now you'll need to add the service to the service configuration.

using Blazor.IntersectionObserver;

public class Startup
    public void ConfigureServices(IServiceCollection services)

3. Use it!

For the quickest setup, use the IntersectionObserve component. This provides an implicit context object which contains the observer entry! Easy!

Component setup

@using Blazor.IntersectionObserver.Components

        Hey... look I'm @(context?.IsIntersecting ? "intersecting!": "not intersecting!")


Service setup

To directly use the service, you just need to inject it and observe the element(s).

@using Blazor.IntersectionObserver
@inject IntersectionObserverService ObserverService

<img ref="ImageElement" src="@(IsIntersecting ? "" : "")"/>

@functions {
    public ElementRef ImageElement { get; set; }
    public bool IsIntersecting { get; set; }
    public bool HasObserver { get; set; }
    protected override void OnAfterRender() 
        if (!HasObserver) 

    public async void SetupObserver()
        HasObserver = true;

        await ObserverService.Observe(ImageElement, (entries) =>
            var entry = entries.FirstOrDefault();
            IsIntersecting = entry.IsIntersecting;

Documentation and Usage


You can pass through options to the ObserverService methods, these are the same as the Intersection Observer API options.


var options = new IntersectionObserverOptions {
    Root = BodyRef, 
    Threshold = new List<double> { 0.25, 0.5, 1 },
    RootMargin = "10px 10px 10px 10px"

Service Methods


This a shorthand way of observing an element by providing:

  • The element you want to observe.
  • The callback to trigger on an intersection update.
  • The intersection observer options.

This returns an IntersectionObserver instance, allowing you to disconnect the observer or unobserve an element. Or if you wish, observe additional elements.

var observer = ObserverService.Observe(ElementRef, (entries) => {
    IsIntersecting = entries.FirstOrDefault().IsIntersecting;
}, options);


The Create method follows the same approach as the Intersection Observer API, you create the observer and then pass elements you wish to observe by calling the Observe method on the observer instance. To create the observer, provide the following:

  • The callback to trigger on an intersection update.
  • The intersection observer options.

This returns an IntersectionObserver instance, allowing you to Observe elements. This also provides the ability to disconnect or unobserve the element.

var observer = ObserverService.Create((entries) => {
    IsIntersecting = entries.FirstOrDefault().IsIntersecting;
}, options);


IntersectionObserver Methods


To observe an element, provide the element reference to the IntersectionObserver instance by calling Observe.



To unobserve an element, provide the element reference to the IntersectionObserver instance by calling Unobserve.



To disconnect the observer, call Disconnect on the IntersectionObserver instance.


This is a useful method to clean up observers when components are disposed of, i.e.

@using Blazor.IntersectionObserver
@implements IDisposable
@inject IntersectionObserverService ObserverService

<div ref="NicolasCageRef"></div>

@functions {
    private IntersectionObserver Observer;
    @* Code... *@
    public void Dispose()



Rather than directly interfacing with the service, you can use this convenience component for quick and easy observing. You can access the observer entry through the implicit @context!

@* Injecting service... *@

        Hey... look I'm @(context?.IsIntersecting ? "intersecting!": "not intersecting!")

@* Component code... *@
  • OnChange (EventCallback<IntersectionObserverEntry>) - When the intersection observer has a entry update.
  • IsIntersecting (bool) - Whether the element is intersecting - used for two-way binding.
  • Options (IntersectionObserverOptions) - The options for the observer.
  • Once (bool) - Only observe once for an intersection, then the instance disposes of itself.
  • Style (string) - The style for the element.
  • Class (string) - The class for the element.

Implementation Detail

To avoid creating an unnecessary number of observers for every element being observed, if a Blazor Observer shares exactly the same options as another, they will both use the same IntersectionObserver instance in JS. As each Blazor Observer has a unique id and callback, the elements that are being observed will still be passed to their respective Blazor Observer.

Feature Requests

There's so much that IntersectionObserver can do, so if you have any requests or you want better documentation and examples, feel free to make a pull request or create an issue!


  • Blazor Extensions - They have a great way of setting up a blazor library, check their repos out!
You can’t perform that action at this time.