scrollytelling
v0.0.2
Published
Module to handle scrollytelling functionality
Downloads
4
Readme
Scrollytelling
Installing
For now, you can clone this repo and npm link
the module but there will be an npm module in the future.
Usage
This module can be used as a simple image switching scrollytelling experience or extended based on your needs.
Simple
For a simple image scrollytelling experience that displays fixed images behind text as the user scrolls, all you need is the following.
Javascript
In your js file, import the scrollytelling module and call it; this will handle the functionality as long as the right data attributes are attached to your html.
import scrollytelling from 'scrollytelling';
const scrolly = scrollytelling();
HTML
In your html, you will need the following:
- an element with a
data-scrollytelling-content
attribute attached that wraps all your content. - elements with
data-trigger
attributes attached that will trigger a change in image. - on your triggers, include another data attribute
data-media
with thesrc
of the image you would like to display when that element comes into view. - (optional): on your trigger(s), include a data attribute
data-media-class
if you would like to include a class attribute on your image in order to style it yourself.
Here is an example
<div data-scrollytelling-content>
<p data-trigger data-media="assets/example.jpg">
<!-- Your text content here -->
</p>
<p data-trigger data-media="assets/another-example.png">
<!-- Your text content here -->
</p>
<p data-trigger data-media="https://apps.bostonglobe.com/path/to/some/image.jpg" data-media-class="my-image-class">
<!-- Your text content here -->
</p>
</div>
Advanced
The scrollytelling module allows you to use your own selectors for triggers. You can also attach your own event listeners.
Javascript
import scrollytelling from 'scrollytelling';
const first = scrollytelling('[data-first-image]');
const other = scrollytelling('[data-other-images]')
const special = scrollytelling('#special');
const last = scrollytelling('[data-last-image]');
first.on('enter', ($trigger, scrollDirection) => {
// Do something when first element is in view
console.log('This element has entered');
});
last.on('exit', ($trigger, scrollDirection) => {
// Do something when last element leaves view
console.log('This element has exited');
})
HTML
<div data-scrollytelling-content>
<p data-first data-media="assets/example.jpg">
<!-- Your text content here -->
</p>
<p data-other data-media="assets/another-example.png">
<!-- Your text content here -->
</p>
<p data-other data-media="assets/aaaand-another-example.png">
<!-- Your text content here -->
</p>
<p id="special" data-media="assets/example.gif">
<!-- Your text content here -->
</p>
<p data-last data-media="https://apps.bostonglobe.com/path/to/some/image.jpg">
<!-- Your text content here -->
</p>
</div>