JAVASCRIPT   69

fitvid.js

Guest on 3rd May 2022 04:21:51 PM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.