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月前
|
Web App开发 JavaScript 前端开发
添加浮动按钮点击滚动到网页底部的纯JavaScript演示代码 IE9、11,Maxthon 1.6.7,Firefox30、31,360极速浏览器7.5.3.308下测试正常
添加浮动按钮点击滚动到网页底部的纯JavaScript演示代码 IE9、11,Maxthon 1.6.7,Firefox30、31,360极速浏览器7.5.3.308下测试正常
|
2月前
|
Web App开发 iOS开发
Web 浏览器
【8月更文挑战第27天】Web 浏览器。
50 2
|
29天前
|
自然语言处理 JavaScript 前端开发
一文梳理JavaScript中常见的七大继承方案
该文章系统地概述了JavaScript中七种常见的继承模式,包括原型链继承、构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合继承等,并探讨了每种模式的实现方式及其优缺点。
一文梳理JavaScript中常见的七大继承方案
|
10天前
|
JavaScript 前端开发
|
5天前
|
JavaScript API
深入解析JS中的visibilitychange事件:监听浏览器标签间切换的利器
深入解析JS中的visibilitychange事件:监听浏览器标签间切换的利器
12 0
|
1月前
|
JavaScript 前端开发
js之浏览器对象|28
js之浏览器对象|28
|
10天前
|
JavaScript 前端开发
原生js常见报错及其处理方案
原生js常见报错及其处理方案
13 0
|
16天前
|
机器学习/深度学习 自然语言处理 前端开发
前端大模型入门:Transformer.js 和 Xenova-引领浏览器端的机器学习变革
除了调用API接口使用Transformer技术,你是否想过在浏览器中运行大模型?Xenova团队推出的Transformer.js,基于JavaScript,让开发者能在浏览器中本地加载和执行预训练模型,无需依赖服务器。该库利用WebAssembly和WebGPU技术,大幅提升性能,尤其适合隐私保护、离线应用和低延迟交互场景。无论是NLP任务还是实时文本生成,Transformer.js都提供了强大支持,成为构建浏览器AI应用的核心工具。
|
2月前
|
机器学习/深度学习 人工智能 前端开发
【人工智能】利用TensorFlow.js在浏览器中实现一个基本的情感分析系统
使用TensorFlow.js在浏览器中进行情感分析是一个非常实用的应用场景。TensorFlow.js 是一个用于在JavaScript环境中训练和部署机器学习模型的库,使得开发者能够在客户端直接运行复杂的机器学习任务。对于情感分析,我们可以使用预先训练好的模型来识别文本中的积极、消极或中性情感。
83 4
【人工智能】利用TensorFlow.js在浏览器中实现一个基本的情感分析系统
|
2月前
|
机器学习/深度学习 存储 前端开发
实战揭秘:如何借助TensorFlow.js的强大力量,轻松将高效能的机器学习模型无缝集成到Web浏览器中,从而打造智能化的前端应用并优化用户体验
【8月更文挑战第31天】将机器学习模型集成到Web应用中,可让用户在浏览器内体验智能化功能。TensorFlow.js作为在客户端浏览器中运行的库,提供了强大支持。本文通过问答形式详细介绍如何使用TensorFlow.js将机器学习模型带入Web浏览器,并通过具体示例代码展示最佳实践。首先,需在HTML文件中引入TensorFlow.js库;接着,可通过加载预训练模型如MobileNet实现图像分类;然后,编写代码处理图像识别并显示结果;此外,还介绍了如何训练自定义模型及优化模型性能的方法,包括模型量化、剪枝和压缩等。
42 1