').parent(".fluid-width-video-wrapper").css("padding-top",100*e+"%"),b.removeAttr("height").removeAttr("width")}})})}})(jQuery);
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Introducing FitVids.js
2 | A lightweight, easy-to-use jQuery plugin for fluid width video embeds.
3 |
4 | FitVids automates [the Intrinsic Ratio Method by Thierry Koblentz](http://www.alistapart.com/articles/creating-intrinsic-ratios-for-video/) to achieve fluid width videos in your responsive web design.
5 |
6 | ## How Do I Use It?
7 | Include jQuery 1.7+ and FitVids.js in your layout and target your videos container with `fitVids()`.
8 |
9 | ```html
10 |
11 |
12 |
18 | ```
19 |
20 | This will wrap each video in a `div.fluid-width-video-wrapper` and apply the necessary CSS. After the initial Javascript call, it's all percentage-based CSS magic.
21 |
22 | ## Currently Supported Players
23 |
24 |
YouTube
Y
25 |
Vimeo
Y
26 |
Blip.tv
Y
27 |
Viddler
Y
28 |
Kickstarter
Y
29 |
30 |
31 | ## Add Your Own Video Vendor
32 | Have a custom video player? We now have a `customSelector` option where you can add your own specific video vendor selector (_mileage may vary depending on vendor and fluidity of player_):
33 |
34 | ```javascript
35 | $("#thing-with-videos").fitVids({ customSelector: "iframe[src^='http://mycoolvideosite.com'], iframe[src^='http://myviiids.com']"});
36 | // Selectors are comma separated, just like CSS
37 | ```
38 |
39 | _Note:_ This will be the quickest way to add your own custom vendor as well as test your player's compatibility with FitVids.
40 |
41 | ## Changelog
42 | * _09.02.11 - v1 - 2.376kb_
43 | * Added `customSelector` option
44 | * _09.02.11 - v1 - 2.135kb_
45 | * Initial release
46 | * Vimeo,YouTube, Blip.tv, Viddler, Kickstarter
47 |
48 | ## Credits
49 | @ChrisCoyier, @davatron5000, @TrentWalton, @raygunray
50 |
--------------------------------------------------------------------------------
/jquery.fitvids.js:
--------------------------------------------------------------------------------
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 |
13 | (function( $ ){
14 |
15 | "use strict";
16 |
17 | $.fn.fitVids = function( options ) {
18 | var settings = {
19 | customSelector: null
20 | };
21 |
22 | var div = document.createElement('div'),
23 | ref = document.getElementsByTagName('base')[0] || document.getElementsByTagName('script')[0];
24 |
25 | div.className = 'fit-vids-style';
26 | div.innerHTML = '';
43 |
44 | ref.parentNode.insertBefore(div,ref);
45 |
46 | if ( options ) {
47 | $.extend( settings, options );
48 | }
49 |
50 | return this.each(function(){
51 | var selectors = [
52 | "iframe[src*='player.vimeo.com']",
53 | "iframe[src*='youtube.com']",
54 | "iframe[src*='youtube-nocookie.com']",
55 | "iframe[src*='kickstarter.com']",
56 | "object",
57 | "embed"
58 | ];
59 |
60 | if (settings.customSelector) {
61 | selectors.push(settings.customSelector);
62 | }
63 |
64 | var $allVideos = $(this).find(selectors.join(','));
65 |
66 | $allVideos.each(function(){
67 | var $this = $(this);
68 | if (this.tagName.toLowerCase() === 'embed' && $this.parent('object').length || $this.parent('.fluid-width-video-wrapper').length) { return; }
69 | var height = ( this.tagName.toLowerCase() === 'object' || ($this.attr('height') && !isNaN(parseInt($this.attr('height'), 10))) ) ? parseInt($this.attr('height'), 10) : $this.height(),
70 | width = !isNaN(parseInt($this.attr('width'), 10)) ? parseInt($this.attr('width'), 10) : $this.width(),
71 | aspectRatio = height / width;
72 | if(!$this.attr('id')){
73 | var videoID = 'fitvid' + Math.floor(Math.random()*999999);
74 | $this.attr('id', videoID);
75 | }
76 | $this.wrap('').parent('.fluid-width-video-wrapper').css('padding-top', (aspectRatio * 100)+"%");
77 | $this.removeAttr('height').removeAttr('width');
78 | });
79 | });
80 | };
81 | })( jQuery );
82 |
--------------------------------------------------------------------------------
/tests.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Fluid Width Videos with Fitvids.js
6 |
34 |
35 |
36 |