Scrollspy

Ready Added in 2.0.0

Trigger events and animations while scrolling your page.

Usage

The Scrollspy component listens to page scrolling and trigger events based on the scroll position. For example, if you scroll down a page, and an element appears in the viewport for the first time, you can trigger a smooth animation to fade in the element. Just add the gls-scrollspy attribute which takes the following options.

Typically, classes from the Animation component are used together with the Scrollspy component.

<div gls-scrollspy="cls:gls-animation-fade"></div>
  • Left

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Right

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  • <div class="gls-child-width-1-2@m gls-grid-match" gls-grid>
        <div>
            <div class="gls-card gls-card-default gls-card-body" gls-scrollspy="cls: gls-animation-slide-left; repeat: true">
                <h3 class="gls-card-title">Left</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body" gls-scrollspy="cls: gls-animation-slide-right; repeat: true">
                <h3 class="gls-card-title">Right</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
    </div>
    

This example uses the repeat: true option. Scroll up and down to see the triggered animations. The layout is made with the Card component.


Groups

You can also group scrollspy elements, so you won’t have to apply the attribute to each of them. Just add the gls-scrollspy="target: SELECTOR" attribute to a container element, targeting the selector of the items you want to animate inside the container. When using a delay, it will be applied cumulatively to the items that scroll into view.

<div gls-scrollspy="target: > div; cls: gls-animation-fade; delay: 500">
    <div></div>
    <div></div>
</div>
  • Fade

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Fade

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Fade

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Fade

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Fade

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Fade

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  • <div class="gls-child-width-1-3@m" gls-grid gls-scrollspy="cls: gls-animation-fade; target: .gls-card; delay: 500; repeat: true">
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Fade</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Fade</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Fade</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Fade</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Fade</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Fade</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
    </div>
    

Set cls option per target

You can also give each target a separate cls option. Just add the gls-scrollspy-class="CLASS" attribute to a target element. It will override the cls option set on the component.

<div gls-scrollspy="target: > div; cls: gls-animation-fade; delay: 500">
    <div gls-scrollspy-class="gls-animation-slide-top"></div>
    <div gls-scrollspy-class="gls-animation-slide-bottom"></div>
</div>
  • Bottom

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Top

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Bottom

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  • <div class="gls-child-width-1-3@m" gls-grid gls-scrollspy="cls: gls-animation-slide-bottom; target: .gls-card; delay: 300; repeat: true">
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Bottom</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body" gls-scrollspy-class="gls-animation-slide-top">
                <h3 class="gls-card-title">Top</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
        <div>
            <div class="gls-card gls-card-default gls-card-body">
                <h3 class="gls-card-title">Bottom</h3>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </div>
    </div>
    

Component options

Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more

Option Value Default Description
cls String `` Class to toggle when the element enters/leaves viewport.
hidden Boolean true Hides the element while out of view.
offset-top Number 0 Top offset before triggering in view.
offset-left Number 0 Left offset before triggering in view.
repeat Boolean false Applies the cls class every time the element is in view.
delay Number 0 Delay time in ms.

cls is the Primary option and its key may be omitted, if it’s the only option in the attribute value.

<span gls-scrollspy="my-class"></span>

JavaScript

Learn more about JavaScript components.

Initialization

Gloss.scrollspy(element, options);

Events

The following events will be triggered on elements with this component attached:

Name Description
inview Fires after an item moves into the viewport.
outview Fires after an item moves into out of viewport.

Scrollspy nav

To automatically update the active menu item depending on the scroll position of your site, add the gls-scrollspy-nav attribute to any navigation. Each menu item must link to the ID of its corresponding part of the site.

<ul class="gls-nav gls-nav-default" gls-scrollspy-nav="closest: li; scroll: true">
    <li><a href=""></a></li>
    <li><a href=""></a></li>
</ul>

For an example of the scrollspy nav, just check out the fixed nav on the right side of this page or take a look at the test. Any of the following options can be applied to the gls-scrollspy-nav attribute. Separate multiple options with a semicolon.


Scrollspy nav options

Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more

Option Value Default Description
cls String gls-active Class to add to the active links.
closest String gls-scrollspy-init-inview Target to apply the class to.
scroll Boolean false Adds the Scroll component to its links.
overflow Boolean true If overflow is set to true, the first or last item will stay active if above or below the navigation.
offset Number 0 Pixel offset added to scroll top.

Scrollspy nav JavaScript

Learn more about JavaScript components.

Scrollspy nav initialization

Gloss.scrollspyNav(element, options);

Scrollspy nav events

The following events will be triggered on elements with this component attached:

Name Description
active Fires after an item becomes active.