jquery.fitvid.js 3.2 KB

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