工做開發中要作前端展現優酷、土豆視頻,但遇到視頻尺寸不能自適應,研究了一下,有如下兩種方法符合本身的須要:javascript
首先看一下優酷、土豆分享視頻的html代碼:html
一、優酷:前端
<iframe height=498 width=510 src="http://player.youku.com/embed/XMTU1MDgzMjM5Ng==" frameborder=0 allowfullscreen></iframe>
二、土豆:html5
<iframe src="http://www.tudou.com/programs/view/html5embed.action?type=2&code=xQ5vDA_iuJk&lcode=RGsNNPq-7p0&resourceId=0_06_05_99" allowtransparency="true" allowfullscreen="true" allowfullscreenInteractive="true" scrolling="no" border="0" frameborder="0" style="width:480px;height:400px;"></iframe>
...........java
解決方法一,javascript實現:jquery
獲取獲取瀏覽器顯示區域的高度,而後根據原視頻的高、寬比例,計算出新的高、寬,來調整iframe的尺寸瀏覽器
使用jquery代碼:服務器
$(function () { resizeIframe(); $(window).resize(function () { resizeIframe(); }); }); //調整iframe尺寸的方法 function resizeIframe() { var expectWidth = $(document).width() * 0.9; //總寬度爲屏幕寬度的0.9倍 $("iframe").each(function () { expectWidth = $(this).parent().width(); $(this).height(expectWidth * $(this).height() / $(this).width()); $(this).width(expectWidth); }); }
不使用jquery的代碼,比較麻煩一點:ide
function resizeIAllframe() { var clientWidth = document.body.clientWidth; clientWidth = clientWidth.toString().replace("px", ""); clientWidth = clientWidth * 0.9; console.log(clientWidth); for (var j = 0; j < document.getElementsByTagName("iframe").length ; j++) { resizeVideo(document.getElementsByTagName("iframe")[j], clientWidth); } } function resizeIframe(objElement, exepectWidth) { var flag = 0; //是否style中定義高寬 var height = objElement.height; var width; if (!height) { height = objElement.style.height; flag = 1; } if (flag) { width = objElement.style.width; } else { width = objElement.width; } if (width) { width = width.replace("px", ""); } if (height) { height = height.replace("px", ""); } if (flag) { objElement.style.width = exepectWidth + "px"; objElement.style.height = (exepectWidth * height / width) + "px"; } else { objElement.width = exepectWidth + "px"; objElement.height = (exepectWidth * height / width) + "px"; } } //頁面加載完後執行 window.onload = function () { resizeIAllframe(); }; //窗口尺寸調整時執行 window.onresize = function () { resizeIAllframe(); };
第二種方法,後臺實現:this
直接在服務器端用正則處理視頻的尺寸,前提是須要客戶端傳過來一個指望寬度
/// <summary> /// 轉換視頻的內容,主要處理視頻的尺寸 /// </summary> /// <param name="videoContent"></param> /// <returns></returns> private string ChangeVideoContent(string videoContent, int expectWidth) { //<iframe(.*?)</iframe> //height="(?<height>\d*)" string iframePattern = @"<iframe(.*?)</iframe>"; string heightPattern = @"height[:=][""]?(?<height>\d*?)(px| |"")"; string widthPattern = @"width[:=][""]?(?<width>\d*?)(px| |"")"; int sWidth, sHeight; return Regex.Replace(videoContent, iframePattern, t => { var heightStr = Regex.Match(t.Value, heightPattern, RegexOptions.Singleline).Groups["height"].Value; if (string.IsNullOrEmpty(heightStr)) { return t.Value; } sHeight = int.Parse(heightStr); var widthStr = Regex.Match(t.Value, widthPattern, RegexOptions.Singleline).Groups["width"].Value; if (string.IsNullOrEmpty(widthStr)) { return t.Value; } sWidth = int.Parse(widthStr); string result = Regex.Replace(t.Value, heightPattern, p => p.Value.Replace(p.Groups["height"].Value, (sHeight * expectWidth / sWidth).ToString()), RegexOptions.Singleline); result = Regex.Replace(result, widthPattern, p => p.Value.Replace(p.Groups["width"].Value, (expectWidth).ToString()), RegexOptions.Singleline); return result; }, RegexOptions.Singleline); }
每種方法都有本身的特色,本身感受哪一個合適用哪一個。