jquery.fitvids.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*jshint browser:true */
  2. /*!
  3. * FitVids 1.1
  4. *
  5. * Copyright 2013, Chris Coyier - http://css-tricks.com + Dave Rupert - http://daverupert.com
  6. * Credit to Thierry Koblentz - http://www.alistapart.com/articles/creating-intrinsic-ratios-for-video/
  7. * Released under the WTFPL license - http://sam.zoy.org/wtfpl/
  8. *
  9. */
  10. ;(function( $ ){
  11. 'use strict';
  12. $.fn.fitVids = function( options ) {
  13. var settings = {
  14. customSelector: null,
  15. ignore: null
  16. };
  17. if(!document.getElementById('fit-vids-style')) {
  18. // appendStyles: https://github.com/toddmotto/fluidvids/blob/master/dist/fluidvids.js
  19. var head = document.head || document.getElementsByTagName('head')[0];
  20. var css = '.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%;}';
  21. var div = document.createElement("div");
  22. div.innerHTML = '<p>x</p><style id="fit-vids-style">' + css + '</style>';
  23. head.appendChild(div.childNodes[1]);
  24. }
  25. if ( options ) {
  26. $.extend( settings, options );
  27. }
  28. return this.each(function(){
  29. var selectors = [
  30. 'iframe[src*="player.vimeo.com"]',
  31. 'iframe[src*="youtube.com"]',
  32. 'iframe[src*="youtube-nocookie.com"]',
  33. 'iframe[src*="kickstarter.com"][src*="video.html"]',
  34. 'object',
  35. 'embed'
  36. ];
  37. if (settings.customSelector) {
  38. selectors.push(settings.customSelector);
  39. }
  40. var ignoreList = '.fitvidsignore';
  41. if(settings.ignore) {
  42. ignoreList = ignoreList + ', ' + settings.ignore;
  43. }
  44. var $allVideos = $(this).find(selectors.join(','));
  45. $allVideos = $allVideos.not('object object'); // SwfObj conflict patch
  46. $allVideos = $allVideos.not(ignoreList); // Disable FitVids on this video.
  47. $allVideos.each(function(count){
  48. var $this = $(this);
  49. if($this.parents(ignoreList).length > 0) {
  50. return; // Disable FitVids on this video.
  51. }
  52. if (this.tagName.toLowerCase() === 'embed' && $this.parent('object').length || $this.parent('.fluid-width-video-wrapper').length) { return; }
  53. if ((!$this.css('height') && !$this.css('width')) && (isNaN($this.attr('height')) || isNaN($this.attr('width'))))
  54. {
  55. $this.attr('height', 9);
  56. $this.attr('width', 16);
  57. }
  58. var height = ( this.tagName.toLowerCase() === 'object' || ($this.attr('height') && !isNaN(parseInt($this.attr('height'), 10))) ) ? parseInt($this.attr('height'), 10) : $this.height(),
  59. width = !isNaN(parseInt($this.attr('width'), 10)) ? parseInt($this.attr('width'), 10) : $this.width(),
  60. aspectRatio = height / width;
  61. if(!$this.attr('id')){
  62. var videoID = 'fitvid' + count;
  63. $this.attr('id', videoID);
  64. }
  65. $this.wrap('<div class="fluid-width-video-wrapper"></div>').parent('.fluid-width-video-wrapper').css('padding-top', (aspectRatio * 100)+'%');
  66. $this.removeAttr('height').removeAttr('width');
  67. });
  68. });
  69. };
  70. // Works with either jQuery or Zepto
  71. })( window.jQuery || window.Zepto );