Code Monkey home page Code Monkey logo

scroll-scene-element's Introduction

<scroll-scene> element

A tiny custom element for all your scrollytelling needs! The successor to @newswire/scroller.

Key features

  • ๐Ÿœ ~700 bytes brotli'ed, ~800 bytes gzip'ed
  • ๐Ÿ‘€ Uses a highly-performant Intersection Observer to monitor scrolling changes
  • ๐Ÿ“š Smartly uses scroll events to calculate scroll progress only when needed
  • ๐ŸŒป Each <scroll-scene> element may have its own offset and opt-in to progress events
  • ๐Ÿ™…๐Ÿฝโ€ No dependencies

Examples

Installation

npm install scroll-scene-element
// or
yarn add scroll-scene-element
// or
pnpm add scroll-scene-element

Usage

In your HTML add <scroll-scene> container elements around every "scene" you want to track the progression of in your interactive. Feel free to use these elements as the containers of your graphics or other dynamic content and style them as needed - all progression and scroll depth changes will be tracked on the tag and its contents.

<div class="scrollytelling-container">
	<scroll-scene>
		<h2>Scene 1</h2>
		<p>This is the first scene.</p>
	</scroll-scene>
	<scroll-scene>
		<h2>Scene 2</h2>
		<p>This is the first scene.</p>
	</scroll-scene>
	<scroll-scene>
		<h2>Scene 3</h2>
		<p>This is the first scene.</p>
	</scroll-scene>
</div>

Then import the script as an ES module in your bundle or load via a script tag to upgrade the <scroll-scene> elements:

import 'scroll-scene-element';

or

<script src="https://unpkg.com/scroll-scene-element/dist/scroll-scene-element.js" type="module"></script>

If you have experience with Scrollama or @newswire/scroller it may be surprising that there's no "init" step. Thanks to custom elements the initalization happens automatically just by using <scroll-scene>.

Events with <scroll-scene> work just like others in JavaScript giving you the same amount of flexibility. (And familiarity!) scroll-scene-enter, scroll-scene-exit and scroll-scene-progress all bubble up to document. If you know there will only be a single set of <scroll-scene> elements on a page you may listen on document directly:

document.addEventListener('scroll-scene-enter', (event) => {
	// "event" is a CustomEvent, giving it has a `detail` property
	const detail = event.detail;

	// the triggering element
	const element = event.element;

	// just like in standard DOM events, "target" is also the triggering element
	const target = event.target;

	// the bounds of the triggering element
	const bounds = detail.bounds;

	// whether the page was scrolling up or down when the event was triggered
	const isScrollingDown = detail.isScrollingDown;

	// the offset used for this element
	const offset = detail.offset;
});

But what if you have more than one set of <scroll-scene> elements on a page? You might instead attach your listener to a parent element of each set of <scroll-scene> elements:

const container = document.querySelector('.scrollytelling-container');

container.addEventListener('scroll-scene-enter', (event) => {
	// no need to allow it to bubble up to `document`
	event.stopPropagation();

	// "event" is a CustomEvent, giving it has a `detail` property
	const detail = event.detail;

	// ...
});

And finally - you may attach your listener to each <scroll-scene> element individually:

const scenes = document.querySelectorAll('scroll-scene');

scenes.forEach((scene) => {
	scene.addEventListener('scroll-scene-enter', (event) => {
		// no need to allow it to bubble up to `document`
		event.stopPropagation();

		// "event" is a CustomEvent, giving it has a `detail` property
		const detail = event.detail;

		// ...
	});
});

Maybe you only need to know the first time a scene enters (or exits) the viewport? You can use the native once option with addEventListener:

const scene = document.querySelector('scroll-scene');

scene.addEventListener(
	'scroll-scene-enter',
	() => {
		console.log('Entered!');
	},
	{ once: true },
);

scene.addEventListener(
	'scroll-scene-exit',
	() => {
		console.log('Exited!');
	},
	{ once: true },
);

If a <scroll-scene> element opts-in to progress tracking, it will emit a scroll-scene-progress event when the progress changes once it enters and stop emitting the event once it exits. This event will bubble up to document:

<scroll-scene progress>
	<h2>Scene 1</h2>
	<p>This is the first scene.</p>
</scroll-scene>
const scene = document.querySelector('scroll-scene');

scene.addEventListener('scroll-scene-progress', (event) => {
	// "event" is a CustomEvent, giving it has a `detail` property
	const detail = event.detail;

	// the triggering element
	const element = event.element;

	// `event.target` is also the triggering element, just like in standard DOM events
	const target = event.target;

	// the bounds of the triggering element
	const bounds = detail.bounds;

	// the offset used for this element
	const offset = detail.offset;

	// the progress of the element from 0 to 1
	const progress = detail.progress;
});

Attributes

A <scroll-scene> element has two optional attributes:

  • offset: a number between 0 and 1 that determines how far from the top of the viewport the element must be before a scroll-scene-enter or scroll-scene-exit event is triggered. Defaults to 0.5.
  • progress: a boolean that determines whether the scroll-scene-progress event is triggered. Defaults to false.

These can be set on the <scroll-scene> element as an attribute or be passed in as a property.

<scroll-scene offset="0.75"></scroll-scene>
<scroll-scene progress></scroll-scene>
const scene = document.querySelector('scroll-scene');

// as properties
scene.offset = 0.75;
scene.progress = true;

// as attributes
scene.setAttribute('offset', '0.75');
scene.setAttribute('progress', '');

Events

Viewport events

scroll-scene-enter is emit when an element enters the viewport. scroll-scene-exit is emit when an element exits the viewport. Both events bubble up to document. They both have a detail property that contains the following:

  • bounds: the bounds (DOMRectReadOnly) of the triggering element as made available within the IntersectionObserver callback
  • element: the triggering element
  • isScrollingDown: whether the page was scrolling up or down when the event was triggered
  • offset: the offset used for this element

Progress event

scroll-scene-progress is emitted when the progress of an element changes. It bubbles up to document and has a detail property that contains the following:

  • bounds: the bounds (DOMRect) of the triggering element
  • element: the triggering element
  • offset: the offset used for this element
  • progress: the progress of the element from 0 to 1

License

MIT

scroll-scene-element's People

Contributors

rdmurphy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

scroll-scene-element's Issues

Add support for progress events

This should be an opt-in feature that is expected to be flagged on every instance of scroll-scene that wants them to activate.

<scroll-scene progress></scroll-scene>
<!-- may be better to use data attributes -->
<scroll-scene data-progress></scroll-scene>

This flag would cause an instance of scroll-scene to interface with the global observer and set up a scroll listener for calculating scroll depth for that element once it enters the view and removes the listener when it exits the view.

The user would still be expected to set up their own listener for this event to react to it.

document.addEventListener('scroll-scene-progress', ({ detail }) => {
  const element = detail.element;
 const progress = detail.progress;
  // do what you need to do
});

isScrollingDown in progress event listener

Good morning and thank you very much for this plugin.
I was wondering if its possible to detect isScrollingDown boolean from inside the 'scroll-scene-progress' eventListener.

scenesContainer.addEventListener('scroll-scene-progress', ({ detail }) => { const { progress } = detail; const percent=(progress * 100).toFixed(1) const index = scenes.indexOf(detail.element); const isScrollingDown = detail.isScrollingDown; // its undefined }

Or in contrary, (which is much more suitable for my needs) is it possible to access the progress value in the'scroll-scene-enter' eventListener?
scenesContainer.addEventListener('scroll-scene-enter', ({ detail }) => { const index = scenes.indexOf(detail.element); const indice=index + 1 const isScrollingDown = detail.isScrollingDown; const { progress } = detail; const percent=(progress * 100).toFixed(1) console.log(percent) // its undefined }

Once again thank you very much,
Sali

Address setter compatibility with frameworks

I thought I did everything correctly with the custom element property setters but Svelte quickly informed me that I had in fact not.

I think the issue here is that Svelte has special handling for custom elements โ€” if it sees that an attribute has a matching property it will pass it in as a property instead of an attribute. This is confusing because it does not do this for native DOM elements. ยฏ\(ใƒ„)/ยฏ

But anyway, this is probably a real bug with how I implemented the setter for offset. It should be possible to "unset" it by doing scene.offset = null but that crashes and burns right now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.