Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

youtube-background

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

youtube-background

Simple jQuery plugin for embedding YouTube videos as a cover background

  • 1.0.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.8K
decreased by-9.53%
Maintainers
1
Weekly downloads
 
Created
Source

jquery.youtube-background

v1.0.9

Another 100 liner in a form of a jQuery plugin created to facilitate YouTube embeds as a cover background using YouTube Embed API.

I wrote this code several times over the years and never bothered to make it reusable. Now when I needed it again I could not even find where I wrote it last...

Goodbye careless days... I'm getting old...

DEMO HERE ⤻

Features

  • Fade in CSS animation
  • Play/Pause button
  • Mute/Unmute button
  • No CSS required
  • No longer jQuery dependant

Usage

Usage is pretty simple, add a data attribute data-youtube containing a full YouTube link or just the YouTube ID.

You can trigger all elements containing the noted attribute with $("[data-youtube]").youtube_background();, or specify your selector, on jQuery document ready event.

Note: From version 1.0.6 jQuery is no longer a dependency, but purely optional. To initialise youtube video backgrounds without jQuery use: new VideoBackgrounds('[data-youtube]');.

P.S. https://www.youtube.com/player_api is injected automatically, only once per script init. I've seen some implementations like Elementor WP plugin that inject it several times, for no reason. Anyway, you're welcome.

Quick Example

	<style>
		/* optional css fade in animation */
		iframe {
			transition: opacity 500ms ease-in-out;
			transition-delay: 250ms;
		}
	</style>

	<!-- target element -->
    <div data-youtube="https://www.youtube.com/watch?v=eEpEeyqGlxA"></div>

    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('[data-youtube]').youtube_background();
        });
    </script>

Properties

PropertyDefaultAcceptsDescription
play-buttonfalsebooleanAdds a toggle pause button
mute-buttonfalsebooleanAdds a toggle mute button
autoplaytruebooleanAutoplay loaded video
mutedtruebooleanLoad video muted
looptruebooleanLoop loaded video
mobilefalsebooleanKeep the youtube embed on mobile
fit-boxfalsebooleanSet iframe to fit the container, meaning width: 100%; height: 100%
inline-stylestruebooleanEnable/disable inline styles from the iframe and wrapper. The default wrapper styles are: background-size: cover;, background-repeat: no-repeat; and background-position: center;; the default iframe styles are top: 50%;, left: 50%;, transform: translateX(-50%) translateY(-50%);, position: absolute;, and opacity: 0;
load-backgroundtruebooleanFetch background from youtube
offset200intshowinfo:0 id deprecated since September 25, 2018. - this setting makes the video a bit larger than it's viewport to hide the info elements
resolution16:9stringdeclare video resolution (work in progress)
pausefalsebooleanAdds a toggle pause button (deprecated)
start-at0intVideo starts playing at desired time in seconds

Noted properties can be added as html attributes as:

  • data-ytbg-play-button
  • data-ytbg-mute-button
  • data-ytbg-autoplay
  • data-ytbg-mooted
  • data-ytbg-loop
  • data-ytbg-mobile
  • data-ytbg-offset
  • data-ytbg-resolution
  • data-ytbg-fit-box
  • data-ytbg-load-background
  • data-ytbg-inline-styles
  • data-ytbg-start-at
Example - Properties as HTML attributes
    <div data-ytbg-play-button="true" data-youtube="https://www.youtube.com/watch?v=eEpEeyqGlxA"></div>

    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('[data-youtube]').youtube_background();
        });
    </script>
Example - Properties as JSON
    <div data-youtube="https://www.youtube.com/watch?v=eEpEeyqGlxA"></div>

    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('[data-youtube]').youtube_background({
      				'play-button': true
      			});
        });
    </script>

Development

Development setup uses GULP with Rollup to bundle ES modules into IIFE jquery.youtube-background.js and jquery.youtube-background.min.js

To install the required packages for running GULP, run:

npm install

To run the server on http://localhost:4040, run:

gulp

Code will automatically be packaged into IIFE while you develop.

To generate minified version of the code, run:

gulp build --production

todo

  • Autoplay property
  • Mute property and button #4
  • Add another wrapper so video can fade in when loaded
  • Add play-pause, mute-unmute events
  • Test the execution order
  • Refactor the code to provide foundation for unified solution from multiple providers and sources called video-background
  • https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
  • Pause and mute on inactivity option - Play only when in viewport if IntersectionObserver available, also can be toggled as an option
  • Perform final tests on BrowserStack and comment the code

THE END.

Keywords

FAQs

Package last updated on 11 Nov 2021

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc