日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

如果转载优酷、土豆视频等,怎么让视频自适应宽度?

發布時間:2023/12/29 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 如果转载优酷、土豆视频等,怎么让视频自适应宽度? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工作開發中要做前端展示優酷、土豆視頻,但遇到視頻尺寸不能自適應,研究了一下,有以下兩種方法符合自己的需要:

首先看一下優酷、土豆分享視頻的html代碼:

1、優酷:

<iframe height=498 width=510 src="http://player.youku.com/embed/XMTU1MDgzMjM5Ng==" frameborder=0 allowfullscreen></iframe>

2、土豆:

<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"></iframe>

...........

解決方法一,javascript實現:

獲取獲取瀏覽器顯示區域的高度,然后根據原視頻的高、寬比例,計算出新的高、寬,來調整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的代碼,比較麻煩一點:

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();
         };

第二種方法,后臺實現:

直接在服務器端用正則處理視頻的尺寸,前提是需要客戶端傳過來一個期望寬度

/// <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);
        }

每種方法都有自己的特點,自己感覺哪個合適用哪個。

總結

以上是生活随笔為你收集整理的如果转载优酷、土豆视频等,怎么让视频自适应宽度?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。