Ei kuvausta

jquery.fitvids.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. var div = document.createElement('div'),
  19. ref = document.getElementsByTagName('base')[0] || document.getElementsByTagName('script')[0];
  20. div.className = 'fit-vids-style';
  21. div.innerHTML = '&shy;<style> \
  22. .fluid-width-video-wrapper { \
  23. width: 100%; \
  24. position: relative; \
  25. padding: 0; \
  26. } \
  27. \
  28. .fluid-width-video-wrapper iframe, \
  29. .fluid-width-video-wrapper object, \
  30. .fluid-width-video-wrapper embed { \
  31. position: absolute; \
  32. top: 0; \
  33. left: 0; \
  34. width: 100%; \
  35. height: 100%; \
  36. } \
  37. </style>';
  38. ref.parentNode.insertBefore(div,ref);
  39. if ( options ) {
  40. $.extend( settings, options );
  41. }
  42. return this.each(function(){
  43. var selectors = [
  44. "iframe[src*='player.vimeo.com']",
  45. "iframe[src*='www.youtube.com']",
  46. "iframe[src*='www.kickstarter.com']",
  47. "object",
  48. "embed"
  49. ];
  50. if (settings.customSelector) {
  51. selectors.push(settings.customSelector);
  52. }
  53. var $allVideos = $(this).find(selectors.join(','));
  54. $allVideos.each(function(){
  55. var $this = $(this);
  56. if (this.tagName.toLowerCase() === 'embed' && $this.parent('object').length || $this.parent('.fluid-width-video-wrapper').length) { return; }
  57. var height = ( this.tagName.toLowerCase() === 'object' || ($this.attr('height') && !isNaN(parseInt($this.attr('height'), 10))) ) ? parseInt($this.attr('height'), 10) : $this.height(),
  58. width = !isNaN(parseInt($this.attr('width'), 10)) ? parseInt($this.attr('width'), 10) : $this.width(),
  59. aspectRatio = height / width;
  60. if(!$this.attr('id')){
  61. var videoID = 'fitvid' + Math.floor(Math.random()*999999);
  62. $this.attr('id', videoID);
  63. }
  64. $this.wrap('<div class="fluid-width-video-wrapper"></div>').parent('.fluid-width-video-wrapper').css('padding-top', (aspectRatio * 100)+"%");
  65. $this.removeAttr('height').removeAttr('width');
  66. });
  67. });
  68. };
  69. })( jQuery );