web页面实现全背景视频功能方案:使用bideo.js来处理object-fit在ie浏览器下不兼容问题

简介: web页面实现全背景视频功能方案:使用bideo.js来处理object-fit在ie浏览器下不兼容问题

object-fit 兼容问题


【视频资源】:https://raw.githubusercontent.com/rishabhp/bideo.js/master/night.mp4

先看下面例子:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>全屏HTML5网页背景视频</title>
        <style>
            html,
            body {
                margin: 0;
                padding: 0;
            }
            #container {
                overflow: hidden;
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                text-align: center;
            }
            #main {
                display: inline-block;
                position: relative;
                top: 50%;
                transform: translateY(-50%);
                color: #fff;
            }
            #video {
                width: 100%;
                height: 100%; 
                position: absolute;
                top: 0;
                left: 0;
                z-index: -1;
                object-fit: fill;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <!-- 内容 -->
            <section id="main">
                <div id="head">
                    <h1>Bideo.js</h1>
                    <p class="sub_head">A JS library that makes it super easy to add fullscreen background videos.</p>
                </div>
            </section>
            <!-- 视频背景 -->
            <video id="video" src="/night.mp4" autoplay loop muted></video>
        </div>
    </body>
</html>


使用 360 极速浏览器的极速模式效果如下:

image.png

使用 360 极速浏览器的 ie 兼容模式效果如下:

image.png


原因是我们在 css 代码里面使用了 object-fit: fill;,而 object-fit 在 ie 模式下是不兼容的。

https://caniuse.com/?search=object-fit

163e9567000645b8b56d2ae157a4a588.png



bideo.js

为了解决上面 object-fit 在 ie 模式下不兼容的问题,这里我们使用 bideo.js,它能实现全屏HTML5网页背景视频。


GitHub地址:https://github.com/rishabhp/bideo.js

Fullscreen HTML5 Background Video for the Web.

/**
 * Full Background Video
 *
 * More info on Audio/Video Media Events/Attributes/Methods
 * - https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events
 * - http://www.w3schools.com/tags/ref_av_dom.asp
 */
(function (global) {
  // Define Bideo constructor on the global object
  global.Bideo = function () {
    // Plugin options
    this.opt = null;
    // The Video element
    this.videoEl = null;
    // Approximate Loading Rate
    //
    // The value will be a number like 0.8
    // which means to load 4 seconds of the video
    // it takes 5 seconds. If the number is super low
    // like 0.2 (regular 3g connections) then you can
    // decide whether to play the video or not.
    // This behaviour will be controller with
    // the `acceptableLoadingRate` option.
    this.approxLoadingRate = null;
    // Methods to which `this` will be bound
    this._resize = null;
    this._progress = null;
    // Time at which video is initialized
    this.startTime = null;
    this.onLoadCalled = false;
    // Initialize and setup the video in DOM`
    this.init = function (opt) {
      // If not set then set to an empty object
      this.opt = opt = opt || {};
      var self = this;
      self._resize = self.resize.bind(this);
      // Video element
      self.videoEl = opt.videoEl;
      // Meta data event
      self.videoEl.addEventListener('loadedmetadata', self._resize, false);
      // Fired when enough has been buffered to begin the video
      // self.videoEl.readyState === 4 (HAVE_ENOUGH_DATA)
      self.videoEl.addEventListener('canplay', function () {
        // Play the video when enough has been buffered
        if (!self.opt.isMobile) {
          self.opt.onLoad && self.opt.onLoad();
          if (self.opt.autoplay !== false) self.videoEl.play();
        }
      });
      // If resizing is required (resize video as window/container resizes)
      if (self.opt.resize) {
        global.addEventListener('resize', self._resize, false);
      }
      // Start time of video initialization
      this.startTime = (new Date()).getTime();
      // Create `source` for video
      this.opt.src.forEach(function (srcOb, i, arr) {
        var key
          , val
          , source = document.createElement('source');
        // Set all the attribute key=val supplied in `src` option
        for (key in srcOb) {
          if (srcOb.hasOwnProperty(key)) {
            val = srcOb[key];
            source.setAttribute(key, val);
          }
        }
        self.videoEl.appendChild(source);
      });
      if (self.opt.isMobile) {
        if (self.opt.playButton) {
          self.opt.videoEl.addEventListener('timeupdate', function () {
            if (!self.onLoadCalled) {
              self.opt.onLoad && self.opt.onLoad();
              self.onLoadCalled = true;
            }
          });
          self.opt.playButton.addEventListener('click', function () {
            self.opt.pauseButton.style.display = 'inline-block';
            this.style.display = 'none';
            self.videoEl.play();
          }, false);
          self.opt.pauseButton.addEventListener('click', function () {
            this.style.display = 'none';
            self.opt.playButton.style.display = 'inline-block';
            self.videoEl.pause();
          }, false);
        }
      }
      return;
    }
    // Called once video metadata is available
    //
    // Also called when window/container is resized
    this.resize = function () {
      // IE/Edge still don't support object-fit: cover
      if ('object-fit' in document.body.style) return;
      // Video's intrinsic dimensions
      var w = this.videoEl.videoWidth
        , h = this.videoEl.videoHeight;
      // Intrinsic ratio
      // Will be more than 1 if W > H and less if H > W
      var videoRatio = (w / h).toFixed(2);
      // Get the container DOM element and its styles
      //
      // Also calculate the min dimensions required (this will be
      // the container dimentions)
      var container = this.opt.container
        , containerStyles = global.getComputedStyle(container)
        , minW = parseInt( containerStyles.getPropertyValue('width') )
        , minH = parseInt( containerStyles.getPropertyValue('height') );
      // If !border-box then add paddings to width and height
      if (containerStyles.getPropertyValue('box-sizing') !== 'border-box') {
        var paddingTop = containerStyles.getPropertyValue('padding-top')
          , paddingBottom = containerStyles.getPropertyValue('padding-bottom')
          , paddingLeft = containerStyles.getPropertyValue('padding-left')
          , paddingRight = containerStyles.getPropertyValue('padding-right');
        paddingTop = parseInt(paddingTop);
        paddingBottom = parseInt(paddingBottom);
        paddingLeft = parseInt(paddingLeft);
        paddingRight = parseInt(paddingRight);
        minW += paddingLeft + paddingRight;
        minH += paddingTop + paddingBottom;
      }
      // What's the min:intrinsic dimensions
      //
      // The idea is to get which of the container dimension
      // has a higher value when compared with the equivalents
      // of the video. Imagine a 1200x700 container and
      // 1000x500 video. Then in order to find the right balance
      // and do minimum scaling, we have to find the dimension
      // with higher ratio.
      //
      // Ex: 1200/1000 = 1.2 and 700/500 = 1.4 - So it is best to
      // scale 500 to 700 and then calculate what should be the
      // right width. If we scale 1000 to 1200 then the height
      // will become 600 proportionately.
      var widthRatio = minW / w;
      var heightRatio = minH / h;
      // Whichever ratio is more, the scaling
      // has to be done over that dimension
      if (widthRatio > heightRatio) {
        var new_width = minW;
        var new_height = Math.ceil( new_width / videoRatio );
      }
      else {
        var new_height = minH;
        var new_width = Math.ceil( new_height * videoRatio );
      }
      this.videoEl.style.width = new_width + 'px';
      this.videoEl.style.height = new_height + 'px';
    };
  };
}(window));



使用bideo.js实现全背景视频

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>全屏HTML5网页背景视频</title>
        <style>
            html,
            body {
                margin: 0;
                padding: 0;
            }
            #container {
                overflow: hidden;
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                text-align: center;
            }
            #main {
                display: inline-block;
                position: relative;
                top: 50%;
                transform: translateY(-50%);
                color: #fff;
            }
            #video {
                width: 100%;
                height: 100%; 
                position: absolute;
                top: 0;
                left: 0;
                z-index: -1;
                object-fit: fill;
            }
        </style>
        <script src="./bideo.js"></script>
    </head>
    <body>
        <div id="container">
            <!-- 内容 -->
            <section id="main">
                <div id="head">
                    <h1>Bideo.js</h1>
                    <p class="sub_head">A JS library that makes it super easy to add fullscreen background videos.</p>
                </div>
            </section>
            <!-- 视频背景 -->
            <video id="video" src="/night.mp4" autoplay loop muted></video>
        </div>
        <script>
            window.onload = function() {
                (function () {
                    let bv = new Bideo();
                    bv.init({
                        // Video element
                        videoEl: document.querySelector("#video"),
                        // Container element
                        container: document.querySelector("body"),
                        // Resize
                        resize: true,
                        // Array of objects containing the src and type
                        // of different video formats to add
                        src: [],
                        // What to do once video loads (initial frame)
                        onLoad: function () {}
                    });
                })();
            }
        </script>
    </body>
</html>


bideo.js 之后,360 极速浏览器的 ie 兼容模式效果如下:

7beeb05a70374dfe96f3b085e93d215b.png

目录
相关文章
|
1月前
|
JavaScript 前端开发
JavaScript分页功能
JavaScript分页功能
|
11天前
|
JavaScript
js实现简洁实用的网页计算器功能源码
这是一款使用js实现简洁实用的网页计算器功能源码。可实现比较基本的加减乘除四则运算功能,界面简洁实用,是一款比较基本的js运算功能源码。该源码可兼容目前最新的各类主流浏览器。
20 2
|
28天前
|
人工智能 JavaScript 网络安全
ToB项目身份认证AD集成(三完):利用ldap.js实现与windows AD对接实现用户搜索、认证、密码修改等功能 - 以及针对中文转义问题的补丁方法
本文详细介绍了如何使用 `ldapjs` 库在 Node.js 中实现与 Windows AD 的交互,包括用户搜索、身份验证、密码修改和重置等功能。通过创建 `LdapService` 类,提供了与 AD 服务器通信的完整解决方案,同时解决了中文字段在 LDAP 操作中被转义的问题。
|
1月前
|
JavaScript 前端开发 API
|
1月前
|
JavaScript API UED
vue.js怎么实现全屏显示功能
【10月更文挑战第7天】
18 1
|
1月前
|
编解码 前端开发 JavaScript
使用 CSS 打印样式为 Web 页面设置专业的打印机效果
使用 CSS 打印样式为 Web 页面设置专业的打印机效果
50 2
|
1月前
|
资源调度 JavaScript UED
如何使用Vue.js实现单页应用的路由功能
【10月更文挑战第1天】如何使用Vue.js实现单页应用的路由功能
|
1月前
|
JavaScript 搜索推荐
JS中的模糊查询功能
JS中的模糊查询功能
27 1
|
19天前
|
前端开发 开发者
WEB自定义页面请求响应
Web组件支持在应用拦截到页面请求后自定义响应请求能力。开发者通过onInterceptRequest()接口来实现自定义资源请求响应 。自定义请求能力可以用于开发者自定义Web页面响应、自定义文件资源响应等场景。
22 0
|
1月前
|
前端开发 JavaScript
使用 JavaScript 实现图片预览功能
使用 JavaScript 实现图片预览功能
24 0