jquery.fitvids.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*global jQuery */
  2. /*jshint multistr:true browser:true */
  3. /*!
  4. * FitVids 1.0
  5. *
  6. * Copyright 2011, 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. * Date: Thu Sept 01 18:00:00 2011 -0500
  11. */
  12. (function( $ ){
  13. "use strict";
  14. $.fn.fitVids = function( options ) {
  15. var settings = {
  16. customSelector: null
  17. };
  18. if(!document.getElementById('fit-vids-style')) {
  19. var div = document.createElement('div'),
  20. ref = document.getElementsByTagName('base')[0] || document.getElementsByTagName('script')[0];
  21. div.className = 'fit-vids-style';
  22. div.id = 'fit-vids-style';
  23. div.style.display = 'none';
  24. div.innerHTML = '&shy;<style> \
  25. .fluid-width-video-wrapper { \
  26. width: 100%; \
  27. position: relative; \
  28. padding: 0; \
  29. } \
  30. \
  31. .fluid-width-video-wrapper iframe, \
  32. .fluid-width-video-wrapper object, \
  33. .fluid-width-video-wrapper embed { \
  34. position: absolute; \
  35. top: 0; \
  36. left: 0; \
  37. width: 100%; \
  38. height: 100%; \
  39. } \
  40. </style>';
  41. ref.parentNode.insertBefore(div,ref);
  42. }
  43. if ( options ) {
  44. $.extend( settings, options );
  45. }
  46. return this.each(function(){
  47. var selectors = [
  48. "iframe[src*='player.vimeo.com']",
  49. "iframe[src*='youtube.com']",
  50. "iframe[src*='youtube-nocookie.com']",
  51. "iframe[src*='kickstarter.com'][src*='video.html']",
  52. "object",
  53. "embed"
  54. ];
  55. if (settings.customSelector) {
  56. selectors.push(settings.customSelector);
  57. }
  58. var $allVideos = $(this).find(selectors.join(','));
  59. $allVideos = $allVideos.not("object object"); // SwfObj conflict patch
  60. $allVideos.each(function(){
  61. var $this = $(this);
  62. if (this.tagName.toLowerCase() === 'embed' && $this.parent('object').length || $this.parent('.fluid-width-video-wrapper').length) { return; }
  63. var height = ( this.tagName.toLowerCase() === 'object' || ($this.attr('height') && !isNaN(parseInt($this.attr('height'), 10))) ) ? parseInt($this.attr('height'), 10) : $this.height(),
  64. width = !isNaN(parseInt($this.attr('width'), 10)) ? parseInt($this.attr('width'), 10) : $this.width(),
  65. aspectRatio = height / width;
  66. if(!$this.attr('id')){
  67. var videoID = 'fitvid' + Math.floor(Math.random()*999999);
  68. $this.attr('id', videoID);
  69. }
  70. $this.wrap('<div class="fluid-width-video-wrapper"></div>').parent('.fluid-width-video-wrapper').css('padding-top', (aspectRatio * 100)+"%");
  71. $this.removeAttr('height').removeAttr('width');
  72. });
  73. });
  74. };
  75. })( jQuery );