jquery.fitvids.js 3.4 KB

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