Coder Social home page Coder Social logo

mpchadwick / jquery.nonsuckyyoutubeembed Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 1.0 2.84 MB

A responsive friendly jQuery plugin that gives you a huge performance boost when embedding YouTube videos by first fetching the video's thumbnail, and then fetching and swapping in the iFrame only when the thumbnail is clicked.

License: MIT License

JavaScript 58.84% HTML 41.16%

jquery.nonsuckyyoutubeembed's Introduction

jquery.nonSuckyYouTubeEmbed

Embedding videos using YouTube's iFrame embed code sucks. Just how much does it suck you might ask? Well, each YouTube video you embed forces the user to download roughly 400K of data before they even click play.

Use jquery.nonSuckyYouTubeEmbed.js for a huge performance boost whenever your site calls for embedded YouTube videos. This plugin takes the YouTube video ID as the ID attribute of the element it is called on and fetches the thumbnail image and displays that first (with a play button laid over). Only when the thumbnail is clicked does it fetch the heavy iFrame. Optimized for responsive design environments!

Demo

Check out jquery.nonSuckyYouTubeEmbed live and in action here...

Usage

Create an element (or elements) with the ID attribute set to the video's YouTube ID. For example if the video's URL is youtube.com/watch?v=HoQN7K6HdRw then the ID is HoQN7K6HdRw.

<div id="CqZgd6-xQl8" class="nonSuckyYouTubeEmbed"></div>

Then call the plugin on that (or those) element(s).

$('.nonSuckyYouTubeEmbed').nonSuckyYouTubeEmbed();

HTML Attributes

The following attributes can (and should) be set on each element that jquery.nonSuckyYouTubeEmbed() is called on. These attributes are set on the element rather than as options when calling the plugin so the plugin can easily be called on a group of elements where these attributes are different.

data-width: The width of the iFrame player. While jquery.nonSuckyYouTubeEmbed will make the iFrame responsive / fluid, this value, combined with data-height is used to calculate the iFrame's aspect ratio. This parameter is optional but recommended to avoid black bars on the sides of the video.

data-height: The height of the iFrame player. While jquery.nonSuckyYouTubeEmbed will make the iFrame responsive / fluid, this value, combined with data-width is used to calculate the iFrame's aspect ratio. This parameter is optional but recommended to avoid black bars on the sides of the video.

data-alt: The alt text for the thumbnail image. Optional, but recommended.

Options

The following options are available when calling jquery.nonSuckyYouTubeEmbed...

defaultWidth: This will be used for the iFrame's width if the data-width attribute is not supplied.

defaultHeight: This will be used for the iFrame's height if the data-height attribute is not supplied.

playBtnSrc: Source of play button that is laid over thumbnail. Default uses data URI

playBtnStyle: If you are changing the play button source you will need to change the style as well to ensure that it is centered over the thumbnail. Here's a code snippet on how to do that. Parameter takes a string of CSS that will be used for the inline style attribute on the play button element.

thumbStyle: A string of CSS that will be used for the inline style attribute on the thumbnail image. Uses width: 100%; height: auto; display: inline; cursor: pointer by default to make images responsive. If you are changing this keep in mind that display: inline; is important for to keep the play button laid on top of the thumbnail.

Browser Compatibility

Tested in the following browsers...

On Mac OS X 10.9

  • Chrome 31.0.1650.57
  • Firefox 25.0.1
  • Safari 7.0
  • Opera 12.16

Via BrowserStack

  • IE 7+ (With IE 7 you need to change play button source to non-dataURI)
  • iOS 4+
  • Android 4.0 (Motorola Razr)
  • Android 2.2 (LG Optimus 2.2)

Gotchas

  • The videos are fetched with autoplay=1 in the URL, but autoplay generally does not work on mobile, so users will have to click play one more time :/. See this Stack Overflow thread

jquery.nonsuckyyoutubeembed's People

Contributors

mpchadwick avatar

Stargazers

 avatar Zilvinas Pikelis avatar Bryan "BJ" Hoffpauir avatar Gil Greenberg avatar Soberdash avatar Justin avatar Pedro Coutinho avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

lishxloo

jquery.nonsuckyyoutubeembed's Issues

https

it tries to pull the video over http (doesn't detect current protocol)

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.