jquery.fitvids.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(){
  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('name')){
  62. var videoName = 'fitvid' + $.fn.fitVids._count;
  63. $this.attr('name', videoName);
  64. $.fn.fitVids._count++;
  65. }
  66. $this.wrap('<div class="fluid-width-video-wrapper"></div>').parent('.fluid-width-video-wrapper').css('padding-top', (aspectRatio * 100)+'%');
  67. $this.removeAttr('height').removeAttr('width');
  68. });
  69. });
  70. };
  71. // Internal counter for unique video names.
  72. $.fn.fitVids._count = 0;
  73. // Works with either jQuery or Zepto
  74. })( window.jQuery || window.Zepto );