Socket
Socket
Sign inDemoInstall

@cleverage/responsive-video-background

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @cleverage/responsive-video-background

A JavaScript library to help build components with responsive video and/or image background


Version published
Weekly downloads
65
decreased by-4.41%
Maintainers
1
Install size
13.0 kB
Created
Weekly downloads
 

Readme

Source

<responsive-video-background /> Web Component

GitHub release (latest by date) npm npm GitHub Built by Clever Age

A Web Component that helps responsively using a video as the background of a content block. The video usage can be restricted to large viewports, with an image fallback on thinner ones.

The dimensions of the Web Component are determined by the content inside it. You can style the Web Component with CSS, but it is better to style the content itself, as there could be a layout shift with the component's styles because JavaScript has to run to really make it available.

Accessibility: If there's a video, it will play only if if the user didn't "notify the system that they prefer an interface that removes or replaces the types of motion-based animation that trigger discomfort for those with vestibular motion disorders" thanks to the CSS prefers-reduced-motion media query.

Examples

See on https://cleverage.github.io/responsive-video-background/

Installation

Use from a CDN

The quickest way to get started and try the Web Component is to load <responsive-video-background /> as a module script tag from a CDN:

<script type="module" src="https://unpkg.com/@cleverage/responsive-video-background@^1"></script>

Don't forget the type="module" attribute, this is own JavaScript modules are now loaded in HTML pages.

This is own it is used in this demo in CodePen that you can fork to play with it.

Better for performance: add a local version to your project

<responsive-video-background /> is also available on npm, so you can add it to your project with this shell command:

npm install @cleverage/responsive-video-background

You can then import it in your own project:

import '@cleverage/responsive-video-background';

Your build tools will then be able to optimize where, when and how to load it.

Usage

Syntax

The Web Component's configuration is done with attributes:

<style>
/* …inline CSS styles for the component, to prevent layout shifts */
</style>
<responsive-video-background
    class="…"
    webm="video.webm"
    mp4="video.mp4"
    poster="video-poster.jpg"
    fallback="not-responsive-image.jpg"
    srcset="image-320.jpg 320w, image-640.jpg 640w, image-1024.jpg 1024w"
    sizes="calc(100vh - 2rem)"
    breakpoint="48rem">
  Some content…
</responsive-video-background>

API

AttributeDescriptionExample
webmURL to the WebM version of the videohttps://cleverage.github.io/responsive-video-background/demo/Big_Buck_Bunny_720_10s_1MB.webm
mp4URL to the MP4 version of the videohttps://cleverage.github.io/responsive-video-background/Big_Buck_Bunny_720_10s_1MB.mp4
posterURL to the video's poster image. This should be the first frame of the video, and an highly optimized progressive JPEGhttps://cleverage.github.io/responsive-video-background/Big_Buck_Bunny_720.jpg
fallbackURL to the fallback image, for the src attributehttps://example.com/path/to/image-fallback.jpg
srcsetValue of the srcset attribute for the responsive imagehttps://example.com/path/to/image-320px.jpg 320w, https://example.com/path/to/image-850px.jpg 850w, https://example.com/path/to/image-1600px.jpg 1600w
sizesvalue of the sizes attribute for the responsive imagecalc(100vh - 2rem)
breakpointViewport width breakpoint, if there should be an image on thin viewports and a video on larger viewports48rem

If the srcset option is set, these other ones are mandatory:

  • webm and/or mp4
  • fallback
  • sizes

If the breakpoint option is set, these other ones are mandatory:

  • webm and/or mp4
  • fallback
  • srcset
  • sizes

FAQ

QuestionAnswer
Why isn't the image switching to a video when the viewport becomes larger?This is not a bug. The idea is to prevent a strong visual change when the user changes the viewport, either by resizing the browser, or rotating the device.

Keywords

FAQs

Last updated on 13 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc