Enhancement

Videoplay

Play videos as they scroll into view and pause them as they leave

Import
@parallelogram-js/core/components/Videoplay
Selector
[data-videoplay]

Playground

Autoplaying video

Scroll it out of view to pause it; pause it yourself and it stays paused.

<video data-videoplay autoplay muted loop playsinline width="640" height="360"
       src="videos/test-pattern.mp4"></video>

About

Only videos with the autoplay attribute are managed. Videoplay removes the attribute when it mounts and starts playback itself, so the browser neither downloads a video far below the fold nor plays it off screen; without JavaScript the attribute still autoplays the video. Managed videos play once enough of the video is visible and pause when it scrolls away or the page is hidden. A video the user pauses stays paused until the user plays it again. They play inline, so iPhone Safari doesn't open them full screen, and are muted for autoplay unless automute says otherwise.

When the user prefers reduced motion, the video isn't played and its controls are shown instead.

Usage

Register the component and the framework loads it the first time a page contains [data-videoplay].

import { Parallelogram } from '@parallelogram-js/core';

const app = Parallelogram.create();
app.components.add('[data-videoplay]', () => import('@parallelogram-js/core/components/Videoplay'));
app.run();

The framework is optional. Videoplay.enhanceAll() mounts the component on every matching element by itself, for a page that wants one enhancement rather than a framework:

import Videoplay from '@parallelogram-js/core/components/Videoplay';

Videoplay.enhanceAll();

It takes a selector and the component's options, and returns the instance it mounted them with. What it leaves out is what the framework adds: loading a component only on the pages that use it, mounting again after a page swap, and ordering with dependsOn.

Attributes

NameTypeDefaultDescription
data-videoplayflagnoneMarks a video, or an element containing one
data-videoplay-targetselectornoneThe video, when the element isn't one or doesn't contain one
data-videoplay-thresholdnumber0.3Visible fraction that starts playback For every instance, Videoplay.defaults.playThreshold.
data-videoplay-pause-thresholdnumber0.1Visible fraction below which playback pauses For every instance, Videoplay.defaults.pauseThreshold.
data-videoplay-autopausebooleantruePause when the video leaves the viewport For every instance, Videoplay.defaults.pauseOnExit.
data-videoplay-automutebooleannonetrue mutes and false unmutes when playing; unset mutes only when autoplay needs it For every instance, Videoplay.defaults.muteWhenPlaying.
data-videoplay-restore-volumebooleanfalseRestore the original volume when pausing For every instance, Videoplay.defaults.restoreVolumeOnPause.
data-videoplay-backgroundbooleanfalseKeep playing while the page is hidden For every instance, Videoplay.defaults.enableInBackground.
data-videoplay-preloadbooleantrueSet preload="metadata" when mounting For every instance, Videoplay.defaults.preloadOnMount.
data-videoplay-require-interactionbooleanfalseManage playback only after the user has clicked, tapped or used a key on the video For every instance, Videoplay.defaults.requireUserInteraction.
data-videoplay-playsinlinebooleantruePlay inline; false lets iPhone Safari play full screen For every instance, Videoplay.defaults.playsInline.
data-videoplay-enhancedset by the componentflagnoneSet once the video is managed

Events

NameDetailDescription
videoplay:play{ reason: string }Playback started DOM event and event bus On video.
videoplay:pause{ reason: string }Playback paused DOM event and event bus On video.
videoplay:play-error{ reason: string; error: string }The browser refused to start playback DOM event and event bus On video.
videoplay:error{ error: Event }The video failed to load DOM event and event bus On video.
videoplay:mount{ element: HTMLElement; video: HTMLVideoElement; hasAutoplay: boolean; playThreshold: number; timestamp: number }A video was set up Event bus