video在mobile端UC浏览器的特殊行为

2020-12-11 loading

# Video 使用

<section id="videoDom">
  <video width="100%" id="video" muted controls autoplay preload="auto">
    <source
      src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
      type="video/webm"
    />

    <source
      src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4"
      type="video/mp4"
    />

    Sorry, your browser doesn't support embedded videos.
  </video>
</section>

# Video 在手机端的表现

环境:Vivo Y97,UC 浏览器

表现:当时播放进行时,在滚动页面的过程中,视频会悬浮在页面顶端;并且层级很高,能够遮挡 z-index=2147483647 的悬浮层

# 处理事项

  • 出现菜单时,暂停视频
  • 监听 scroll 事件,视频滚动到顶部时,暂停视频
  • 将 video 设为css display:none(爱奇艺的方式也是这样,点击菜单,菜单遮挡视频,此时的视频是 none 的状态,只是音频还在继续播放)
  • 当 display = none 时,要么将视频全部遮挡,要么设置个默认封面和播放图标,提升用户体验

# 测试代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>测试video</title>
    <style>
      #menu {
        width: 60%;
        height: 100%;
        position: fixed;
        top: 0;
        right: 0;
        background-color: rgba(0, 0, 0, 0.8);
        padding: 10px;
        z-index: 2147483647;
        display: none;
        color: white;
      }
      ul {
        list-style: lower-roman;
        line-height: 2;
      }
      #videoDom {
        width: 100%;
        background-color: black;
      }
    </style>
  </head>

  <body>
    <h1 id="btn">点击出现菜单</h1>
    <section id="menu">
      <ul>
        <li>hahah</li>
        <li>hahah</li>
        <li>hahah</li>
        <li>hahah</li>
        <li>hahah</li>
        <li>hahah</li>
        <li>hahah</li>
        <li>hahah</li>
        <li>hahah</li>
      </ul>
    </section>

    <section id="videoDom">
      <video width="100%" id="video" muted controls autoplay preload="auto">
        <source
          src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
          type="video/webm"
        />

        <source
          src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4"
          type="video/mp4"
        />

        Sorry, your browser doesn't support embedded videos.
      </video>
    </section>

    <script>
      const btn = document.getElementById('btn')
    const menu = document.getElementById('menu')
    const videoDom = document.getElementById('videoDom')
    const video = document.getElementById('video')
    let videoTop = 0;//video 距离屏幕的top值
    let ifSetPause = false;// 是否是滚动过程中主动设为暂停
    window.onload = ()=>{
      videoTop = videoDom.offsetTop
      btn.onclick = () => {
        const videoHeight = video.clientHeight
        videoDom.style.height = videoHeight + 'px'
        video.pause();
        menu.style.display = 'block';
        video.style.display = 'none'
      }
      menu.onclick = () => {
        menu.style.display = 'none'
        video.style.display = 'block'
        video.play()
      }
    }
    window.addEventListener('scroll',()=>{
      if(window.scrollY >= videoTop){
        console.log('------------>-------',window.scrollY,video.paused)
        if(!video.paused){
          ifSetPause = true
          video.pause();
          video.style.display = 'none'
        } 
      }else{
        console.log('---------other----------',window.scrollY,video.paused)
        video.style.display = 'block'
        if(ifSetPause){
          video.play()
          ifSetPause=false
        }
      }
    })
    video.addEventListener('loadedmetadata',()=>{
      console.log(video.duration)
    })
    </script>
  </body>
</html>

# 相关链接

支付宝打赏
支付宝打赏
微信打赏
微信打赏