Giter Club home page Giter Club logo

vidbg's Introduction

vidbg.js

A minimal vanilla JavaScript video background plugin.

Working With Wordpress

If you plan on using vidbg.js with WordPress, make your life a little bit easier and download the plugin I made on the Wordpress Repo.

Compatibility

  • All modern web browsers
  • IE 11+
  • All mobile web browsers that support autoplaying HTML5 <video>

Instructions

To get started, download the script. Once you have downloaded the script, include it in your project:

<!-- <head> -->
<link href="dist/vidbg.css" rel="stylesheet" type="text/css">

<!-- End of <body> -->
<script src="dist/vidbg.js"></script>

Initializing the Script

The script accepts three arguments.

var instance = new vidbg(selector, options, attributes)

The selector argument is any JavaScript selector, the options are for configuring vidbg.js, and the attributes argument modifies the actual <video> element.

var instance = new vidbg('.vidbg-box', {
  mp4: 'http://example.com/video.mp4', // URL or relative path to MP4 video
  webm: 'path/to/video.webm', // URL or relative path to webm video
  poster: 'path/to/fallback.jpg', // URL or relative path to fallback image
  overlay: false, // Boolean to display the overlay or not
  overlayColor: '#000', // The overlay color as a HEX
  overlayAlpha: 0.3 // The overlay alpha. Think of this as the last integer in RGBA()
}, {
  // Attributes
})

Note: Supplying both .mp4 and .webm will increase browser compatibility.

Options

const defaultOptions = {
  mp4: null,
  webm: null,
  poster: null,
  overlay: false,
  overlayColor: '#000',
  overlayAlpha: 0.3
}

Attributes

const defaultAttributes = {
  autoplay: true,
  controls: false,
  loop: true,
  muted: true,
  playsInline: true
}

Overlay

Setting the overlay option to true will add an RGBA background over your video. This is helpful when you want to display text or meaningful content over the video background to increase legibility.

Fallback Image Flashing

You may experience the fallback image flash on page load. This is because the fallback image is added through JavaScript. As a result of this, it will load after CSS styles are rendered.

If you'd like to combat this, you can preset the fallback image through CSS. For example, if your selector is .video-box you could do something like:

.video-box .vidbg-container {
  background-image: url(/assets/link-to-fallback.png);
}

Methods

Below are the methods that are available:

// First, create the vidbg.js instance.
var instance = new vidbg('.vidbg-box', {
  mp4: 'http://example.com/video.mp4',
  webm: 'path/to/video.webm',
  poster: 'path/to/fallback.jpg',
})

// Manually resize the video background
instance.resize()

// Remove the <video> element
instance.removeVideo()

License

vidbg.js is licensed under The MIT License. You can view it here.

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.