4.4.0 • Published 3 years ago

scroll-triggers v4.4.0

Weekly downloads
72
License
MIT
Repository
github
Last release
3 years ago

scroll-triggers npm

A tiny, dependency-less javascript library to add classes as elements scroll into viewport.

Features

  • Add a class when an element comes into view (great for animations)
  • Set an image when an element comes into view (great for lazy loading)
  • Set the width of an element based on scroll % (great for scroll progress bars)
  • API for both HTML and Javascript

Install

npm install scroll-triggers

Setup

import 'scroll-triggers';

// alternative
import scrollTriggers from 'scroll-triggers';

Events

Custom events are fired/listened on the element:

EventTypeDescription
scrolltriggers:inViewFiredElement is in view
scrolltriggers:outOfViewFiredElement is out of view
scrolltriggers:pauseListenedPauses scroll-triggers
scrolltriggers:resumeListenedResumes scroll-triggers

Options

List of available options:

NameTypeDescription
className{string}Class to be added/removed when element is in view
start{string|Element|NodeList} CSS SelectorAdd class when the specified element is in view
end{string|Element|NodeList} CSS SelectorRemoves class when the specified element is in view
position{string = 'bottom'} "top|middle|bottom"Add class at when element hits the specified position of page
positionEnd{string = 'bottom'} "auto|top|middle|bottom"Removes class when specified element hits the specified position of page
offset{number}The offset controls the distance (negative or positive) between the top border of the element and the top border of the window. This is useful to fine tune when a class is added.
image{string} Path to imageSet an image when an element comes into view, if the element is an it will set it as the src, otherwise it will be set as background-image
src{string} Path to resourceSet the src property when an element comes into view
srcset{string} Path to resourceSet the srcset property when an element comes into view
progress{boolean = false}Set the width of an element based on scroll %
once{boolean = true}Whether scroll-triggers should be executed once or not
fixed{boolean = true}Needed for fixed (position: fixed) elements
inView{function}Callback executed when element is in view
outOfView{function}Callback executed when element is out view

Usage

See the example.

HTML

Add class when element is in view.

<div data-scroll data-scroll-class="class-to-add"></div>

Add class when another element is in view

<div data-scroll data-scroll-class="class-to-add" data-scroll-start=".some .selector"></div>

Add class when another element is in view and remove when it gets to another element

<div data-scroll data-scroll-class="class-to-add" data-scroll-start=".some .selector" data-scroll-end=".some .lower .selector"></div>

Add class at when element hits bottom of page

<div data-scroll data-scroll-class="class-to-add" data-scroll-position="bottom"></div>

Add class at when element hits middle of page

<div data-scroll data-scroll-class="class-to-add" data-scroll-position="middle"></div>

Set an image when an element comes into view as a background image

<div data-scroll data-scroll-image="/path/to/image.jpg"></div>

<!--
  This will generate the markup below:
  <div data-scroll data-scroll-image="/path/to/image.jpg" style="background-image: url('/path/to/image.jpg'); background-repeat: no-repeat;"></div>
-->

Set the width of an element based on scroll % (great for progress bars)

<div data-scroll data-scroll-progress></div>

Set the src property when an element comes into view (great for lazy load)

<iframe data-scroll data-scroll-src="https://wikipedia.org/wiki/Main_Page"/></iframe>

Set the srcset property when an element comes into view (great for lazy load)

<picture>
  <source media="(min-width: 650px)" data-scroll data-scroll-srcset="http://placehold.it/465x465?text=Min-650" />
</picture>

JavaScript

import scrollTriggers from 'scroll-triggers';

scrollTriggers([
  {
    el: '.some-selector',
    start: '.selector',
    end: '.selector',
    className: 'class-to-add',
    image: 'image/path.jpg',
    src: 'http://url-to-resource.com',
    srcSet: 'http://url-to-resource.com',
    position: 'top|middle|bottom',
    positionEnd: 'auto|top|middle|bottom',
    offset: -20,
    progress: true|false,
    once: true|false,
    fixed: true|false,
    inView: (el, options) => {},
    outOfView: (el, options) => {}
  }
]);
4.4.0

3 years ago

4.3.0

4 years ago

4.2.0

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.17.0

5 years ago

3.16.0

6 years ago

3.15.2

6 years ago

3.15.1

6 years ago

3.15.0

6 years ago

3.14.0

6 years ago

3.13.0

6 years ago

3.12.0

7 years ago

3.11.0

7 years ago

3.10.0

7 years ago

3.9.1

7 years ago

3.9.0

7 years ago

3.8.0

7 years ago

3.7.0

7 years ago

3.6.0

7 years ago

3.5.0

7 years ago

3.4.2

7 years ago

3.4.1

7 years ago

3.4.0

7 years ago

3.3.0

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.0.0

7 years ago

1.4.0

7 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago