casper/assets/js/lib/jquery.fitvids.js

90 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

/*jshint browser:true */
2013-10-31 17:35:57 +00:00
/*!
* FitVids 1.3
2013-10-31 17:35:57 +00:00
*
*
* Copyright 2017, Chris Coyier + Dave Rupert + Ghost Foundation
* This is an unofficial release, ported by John O'Nolan
2013-10-31 17:35:57 +00:00
* Credit to Thierry Koblentz - http://www.alistapart.com/articles/creating-intrinsic-ratios-for-video/
* Released under the MIT license
2013-10-31 17:35:57 +00:00
*
*/
2017-05-31 18:09:50 +00:00
;(function( $ ){
2013-10-31 17:35:57 +00:00
2017-05-31 18:09:50 +00:00
'use strict';
2013-10-31 17:35:57 +00:00
$.fn.fitVids = function( options ) {
var settings = {
2017-05-31 18:09:50 +00:00
customSelector: null,
ignore: null
2013-10-31 17:35:57 +00:00
};
if(!document.getElementById('fit-vids-style')) {
// appendStyles: https://github.com/toddmotto/fluidvids/blob/master/dist/fluidvids.js
var head = document.head || document.getElementsByTagName('head')[0];
var css = '.fluid-width-video-container{flex-grow: 1;width:100%;}.fluid-width-video-wrapper{width:100%;position:relative;padding:0;}.fluid-width-video-wrapper iframe,.fluid-width-video-wrapper object,.fluid-width-video-wrapper embed {position:absolute;top:0;left:0;width:100%;height:100%;}';
2017-05-31 18:09:50 +00:00
var div = document.createElement("div");
div.innerHTML = '<p>x</p><style id="fit-vids-style">' + css + '</style>';
head.appendChild(div.childNodes[1]);
2013-10-31 17:35:57 +00:00
}
if ( options ) {
$.extend( settings, options );
}
return this.each(function(){
var selectors = [
2017-05-31 18:09:50 +00:00
'iframe[src*="player.vimeo.com"]',
'iframe[src*="youtube.com"]',
'iframe[src*="youtube-nocookie.com"]',
'iframe[src*="kickstarter.com"][src*="video.html"]',
'object',
'embed'
2013-10-31 17:35:57 +00:00
];
if (settings.customSelector) {
selectors.push(settings.customSelector);
}
2017-05-31 18:09:50 +00:00
var ignoreList = '.fitvidsignore';
if(settings.ignore) {
ignoreList = ignoreList + ', ' + settings.ignore;
}
2013-10-31 17:35:57 +00:00
var $allVideos = $(this).find(selectors.join(','));
2017-05-31 18:09:50 +00:00
$allVideos = $allVideos.not('object object'); // SwfObj conflict patch
$allVideos = $allVideos.not(ignoreList); // Disable FitVids on this video.
2013-10-31 17:35:57 +00:00
$allVideos.each(function(){
var $this = $(this);
2017-05-31 18:09:50 +00:00
if($this.parents(ignoreList).length > 0) {
return; // Disable FitVids on this video.
}
2013-10-31 17:35:57 +00:00
if (this.tagName.toLowerCase() === 'embed' && $this.parent('object').length || $this.parent('.fluid-width-video-wrapper').length) { return; }
2017-05-31 18:09:50 +00:00
if ((!$this.css('height') && !$this.css('width')) && (isNaN($this.attr('height')) || isNaN($this.attr('width'))))
{
$this.attr('height', 9);
$this.attr('width', 16);
}
2013-10-31 17:35:57 +00:00
var height = ( this.tagName.toLowerCase() === 'object' || ($this.attr('height') && !isNaN(parseInt($this.attr('height'), 10))) ) ? parseInt($this.attr('height'), 10) : $this.height(),
width = !isNaN(parseInt($this.attr('width'), 10)) ? parseInt($this.attr('width'), 10) : $this.width(),
aspectRatio = height / width;
2017-05-31 18:09:50 +00:00
if(!$this.attr('name')){
var videoName = 'fitvid' + $.fn.fitVids._count;
$this.attr('name', videoName);
$.fn.fitVids._count++;
2013-10-31 17:35:57 +00:00
}
$this.wrap('<div class="fluid-width-video-container"><div class="fluid-width-video-wrapper"></div></div>').parent('.fluid-width-video-wrapper').css('padding-top', (aspectRatio * 100)+'%');
2013-10-31 17:35:57 +00:00
$this.removeAttr('height').removeAttr('width');
});
});
};
2017-05-31 18:09:50 +00:00
// Internal counter for unique video names.
$.fn.fitVids._count = 0;
2013-10-31 17:35:57 +00:00
// Works with either jQuery or Zepto
})( window.jQuery || window.Zepto );