/*! LazyYT (lazy load Youtube videos plugin) - v0.3.0 - 2014-03-07 * Usage:
loading...
* Copyright (c) 2014 Tyler Pearson; Licensed MIT */ ;(function ($) { 'use strict'; function setUp(el) { var $el = el, width = $el.data('width'), height = $el.data('height'), id = $el.data('youtube-id'), youtubeParameters = $el.data('parameters') || ''; if (typeof width === 'undefined' || typeof height === 'undefined' || typeof id === 'undefined') { throw new Error('lazyYT is missing a required data attribute.'); } $el.css({ 'position': 'relative', 'height': height, 'width': width, //'background': 'url(http://img.youtube.com/vi/' + id + '/maxresdefault.jpg) center center no-repeat', 'cursor': 'pointer', '-webkit-background-size': 'cover', '-moz-background-size': 'cover', '-o-background-size': 'cover', 'background-size': 'cover' }) .html('

') .addClass('lazyYT-image-loaded'); $.getJSON('https://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=json', function (data) { $('#lazyYT-title-' + id).text(data.entry.title.$t); }); $el.on('click', function (e) { e.preventDefault(); if (!$el.hasClass('lazyYT-video-loaded') && $el.hasClass('lazyYT-image-loaded')) { $el.html('') .removeClass('lazyYT-image-loaded') .addClass('lazyYT-video-loaded'); } }); } $.fn.lazyYT = function () { return this.each(function () { var $el = $(this).css('cursor', 'pointer'); setUp($el); }); }; }(jQuery));