视频大小适配是个头疼的问题。之前利用了 Advanced Responsive Video Embedder 这个插件结果发现还是手机端显示有些问题,不能正常播放,这下我们利用 FitVids 来做到电脑和手机适配。已经完美解决 这次我们不用加任何插件,完全可以用 JS 代码来实现。首先,我们需要在主题的 js 目录下创建一个 js 文件,名字叫做 jquery.fitvids.js 加入如下代码,然后保存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
(function ( $ ) { 'use strict' ; $.fn.fitVids = function ( options ) { var settings = { customSelector: null , ignore: null }; if (!document .getElementById('fit-vids-style' )) { var head = document .head || document .getElementsByTagName('head' )[0 ]; 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%;}' ; var div = document .createElement("div" ); div.innerHTML = '<p>x</p><style id="fit-vids-style">' + css + '</style>' ; head.appendChild(div.childNodes[1 ]); } if ( options ) { $.extend( settings, options ); } return this .each(function ( ) { var selectors = [ 'iframe[src*="player.vimeo.com"]' , 'iframe[src*="youtube.com"]' , 'iframe[src*="youtube-nocookie.com"]' , 'iframe[src*="kickstarter.com"][src*="video.html"]' , 'object' , 'embed' ]; if (settings.customSelector) { selectors.push(settings.customSelector); } var ignoreList = '.fitvidsignore' ; if (settings.ignore) { ignoreList = ignoreList + ', ' + settings.ignore; } var $allVideos = $(this ).find(selectors.join(',' )); $allVideos = $allVideos.not('object object' ); $allVideos = $allVideos.not(ignoreList); $allVideos.each(function ( ) { var $this = $(this ); if ($this .parents(ignoreList).length > 0 ) { return ; } if (this .tagName.toLowerCase() === 'embed' && $this .parent('object' ).length || $this .parent('.fluid-width-video-wrapper' ).length) { return ; } if ((!$this .css('height' ) && !$this .css('width' )) && (isNaN ($this .attr('height' )) || isNaN ($this .attr('width' )))) { $this .attr('height' , 9 ); $this .attr('width' , 16 ); } var height = ( this .tagName.toLowerCase() === 'object' || ($this .attr('height' ) && !isNaN (parseInt ($this .attr('height' ), 10 ))) ) ? parseInt ($this .attr('height' ), 10 ) : $this .height(), width = !isNaN (parseInt ($this .attr('width' ), 10 )) ? parseInt ($this .attr('width' ), 10 ) : $this .width(), aspectRatio = height / width; if (!$this .attr('id' )){ var videoID = 'fitvid' + Math .floor(Math .random()*999999 ); $this .attr('id' , videoID); } $this .wrap('<div class="fluid-width-video-wrapper"></div>' ).parent('.fluid-width-video-wrapper' ).css('padding-top' , (aspectRatio * 100 )+'%' ); $this .removeAttr('height' ).removeAttr('width' ); }); }); }; })( window .jQuery || window .Zepto );
接下来我们要修改 functions.php 文件咯 加入如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13
function add_fitvids ( ) { wp_register_script('jquery_fitvids' , get_template_directory_uri(). '/js/jquery.fitvids.js' , array('jquery' ), '2.0.110526' ); wp_enqueue_script('jquery_fitvids' ); add_action('wp_head' , 'add_fitthem' ); function add_fitthem ( ) { ?> <script type="text/javascript" > jQuery(document ).ready( function ( ) { jQuery('.video' ).fitVids(); }); </script><?php } } add_action('wp_enqueue_scripts', 'add_fitvids');
这段代码就是引入了上面我们放入的 js 文件,并在 WordPress 模板加载的时候进行初始化设置。 然后我们加入一段代码对某些模仿器进行适配。 针对 YouTube 或 Vimeo 等支持 oembed 的视频源,我们再在 functions.php 加入如下代码来进行控制
1 2 3 4 5
function add_embed_filter ( $html ) { $return = '<div class="video">' . $html . '</div>' ; return $return; } add_filter('oembed_dataparse' , 'add_embed_filter' );
对于国内的一些视频网站,视频源为 embed,我们再在 functions.php 加入如下代码来控制
1 2 3 4 5 6 7
function add_oembed_filter ( $html ) { $return = '<div class="video">' . $html . '</div>' ; return $return; } add_filter('embed_youku' , 'add_oembed_filter' ); add_filter('embed_56com' , 'add_oembed_filter' ); add_filter('embed_tudou' , 'add_oembed_filter' );
我们在发布视频时只需要在外面套 2 行代码就行啦。 比如我的一个视频是从土豆获取来的 HTML 代码,源代码为
1
<embed src ="http://www.tudou.com/v/bL4ioykS6ho/&resourceId=0_05_02_99/v.swf" type ="application/x-shockwave-flash" width ="300" height ="150" ></embed>
那么我们在发布时只需要在外面套一层 div 就 OK 了
1 2 3
<div class ="video" > .... </div >
最后的代码如下
1
<div class ="video" ><embed src ="http://www.tudou.com/v/bL4ioykS6ho/&resourceId=0_05_02_99/v.swf" type ="application/x-shockwave-flash" width ="300" height ="150" ></embed></div>
在文章中的 HTML 代码编辑器中插入如上代码即可实现手机端和电脑端的视频完美适配。 以上就是利用 Fitvids 来对视频进行适配操作的全部过程。