H5 移动端自定义video的控制条

最近在做一个视频播放的项目,用video自带的控制条(controls),点击全屏之后,再退出全屏之后,会导致输入框触发不了键盘事件,一直找不到原因,最后实在没办法,只能自己写全屏事件来解决这个问题。

1、首先把列表的video改为用图片展示

2、全屏后把设置video的宽高为100%,并去掉video的controls属性,这样就隐藏了video自带的控制条

video.style.width = '100%';
video.style.height = '100%';

3、全屏后手机自带的顶部导航栏隐藏没有赢藏,需要设置状态栏

//参数:true - 全屏;false - 不全屏
plus.navigator.setFullscreen(true);

4、写控制条

    处理控制条时间

video.addEventListener('timeupdate',()=>{
  totalTime = video.duration;//总时间
  let timeDisplay = video.currentTime;//当前播放时间
  let remainTime = totalTime - timeDisplay;//剩余时间
  //playTime 剩余时间 显示在控制条上的时间 doTime是把取到的时间转为时间格式
  this.videoControl.playTime = this.doTime(remainTime);
  //进度条
  btn.style.left = (timeDisplay/totalTime)*100+'%';
  bgc.style.width = (timeDisplay/totalTime)*100 +'%';
}) 
doTime(time){
  var res = "00";
  if(time>60){
    var mi = Math.floor(time/60);
    if(mi < 10){
      res = "0"+mi;
    }else {
      res = mi;
    }
    res = res +":";
    var s = Math.floor(time%60);
    if(s<10){
      res += "0"+s;
    }else {
      res += s;
    }
  }else {
    res = res + ":";
    s = Math.floor(time) || 0;
    if(s<10){
      res += "0"+s;
    }else {
      res +=s;
    }
  }
  return res;
},

拖动进度条

  var btn = document.getElementById('bt_');
  btn.addEventListener('touchmove',(event)=>{
  var allTime = this.parseTime(this.doTime(video.duration));
  let bgw = document.getElementById('bg_').offsetWidth;
  let btnw = event.targetTouches[0].pageX  - 90;
  let percent  = btnw / bgw;
  if(btnw <= bgw && btnw >=0){
    btn.style.left = btnw+'px';
    video.pause();
    video.currentTime = percent * allTime;
  }
})

控制条及全屏html css

<div v-if="showFullScreen" class="video-fullscreen" @click="showControls">
  <div style="width: 100%;height: 100%;position: relative">
    <video id="bigvideo"
           :src="fullScreenUrl"
           preload="none">
    </video>
    <div class="video-contorls" id="controls">
      <ul class="video-control">
        <li @click.stop="videoPause">
          <img v-if="videoControl.status == 'play'" src="../../assets/images/video-play.png" style="width: 20px;height: 16px"/>
          <img v-if="videoControl.status == 'pause'" src="../../assets/images/video-pause.png" style="width: 20px;height: 16px"/>
        </li>
        <li style="margin-right: 10px">
          <span>{{videoControl.playTime}}</span>
        </li>
        <li class="video-progress">
          <div id="progress">
            <div style="width:100%;" class="abgs_a">
              <div id="box_">
                <div id="bg_">
                  <div id="bgcolor_"></div>
                </div>
                <div id="bt_"></div>
              </div>
            </div>
          </div>
        </li>

        <li @click.stop="closeFullScreen()">
          <img src="../../assets/images/video-shrink.png" style="width: 18px;height: 18px"/>
        </li>
      </ul>
      
    </div>

  </div>

</div>
.video-fullscreen{
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background-color: #000;
  z-index: 9999;
}
.video-progress{
  display: inline-block;
  width: 40%;
  margin-left: 10px;
  margin-right: 20px;
}

#box_ {
  position:relative;
  width:100%;
  border:1px solid transparent;
}
#bg_ {
  height:5px;
  margin-top:6px;
  border:1px solid #ddd;
  overflow:hidden;
}
#bgcolor_ {
  background:#ff6600;
  width: 0px;
  height:5px;
}
#bt_ {
  width:16px;
  height:16px;
  background-color:rgb(243,243,243);
  border-radius:50%;
  overflow:hidden;
  position:absolute;
  margin-left:-5px;
  border:1px solid #d0cbcb;
  top:1px;
  cursor:pointer;
}
.video-contorls .play-icon{
  vertical-align: top;
  margin-right: 5px;
  margin-top: -4px;
  display: inline-block;
  border-style: solid;
  border-width: 8px 0 8px 15px;
  border-color: transparent transparent transparent #666;
}
.video-contorls .pause-icon{
  width: 8px;
  height: 15px;
  vertical-align: top;
  margin-right: 5px;
  margin-top: 1px;
  display: inline-block;
  border-style: double;
  border-width: 0px 0px 0px 10px;
  border-color: #666;
}
.video-control{
  padding: 15px 0 30px 0px;
}
.video-control li{
  list-style: none;
  float: left;
}
.video-control li:first-child{
  margin-left: 10px;
  margin-right: 10px;
}

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我提供如下回答: 1. 带自定义控制H5视频播放器 HTML5的video标签提供了默认的视频播放控件,但是这些控件样式相对简单,如果需要自定义控制,可以使用JavaScript和CSS来实现。下面是一个简单的示例代码: HTML代码: ``` <video id="myVideo" controls> <source src="video.mp4" type="video/mp4"> <source src="video.webm" type="video/webm"> Your browser does not support the video tag. </video> ``` JavaScript代码: ``` var video = document.getElementById("myVideo"); var playBtn = document.getElementById("playBtn"); var progressBar = document.getElementById("progressBar"); function togglePlay() { if (video.paused) { video.play(); playBtn.innerHTML = "Pause"; } else { video.pause(); playBtn.innerHTML = "Play"; } } function updateProgressBar() { var progress = (video.currentTime / video.duration) * 100; progressBar.style.width = progress + "%"; } video.addEventListener("play", function() { playBtn.innerHTML = "Pause"; }); video.addEventListener("pause", function() { playBtn.innerHTML = "Play"; }); video.addEventListener("timeupdate", updateProgressBar); playBtn.addEventListener("click", togglePlay); ``` CSS代码: ``` #myVideo { width: 640px; height: 360px; } #playBtn { background-color: #333; color: #fff; padding: 10px; border: none; border-radius: 5px; cursor: pointer; } #progressBarContainer { background-color: #ccc; height: 10px; margin-top: 10px; } #progressBar { background-color: #333; height: 100%; width: 0; } ``` 2. H5视频播放器的播放速度控制 HTML5的video标签提供了playbackRate属性,可以用来设置视频的播放速度。下面是一个简单的示例代码: HTML代码: ``` <video id="myVideo" controls> <source src="video.mp4" type="video/mp4"> <source src="video.webm" type="video/webm"> Your browser does not support the video tag. </video> <button id="slowBtn">Slow</button> <button id="normalBtn">Normal</button> <button id="fastBtn">Fast</button> ``` JavaScript代码: ``` var video = document.getElementById("myVideo"); var slowBtn = document.getElementById("slowBtn"); var normalBtn = document.getElementById("normalBtn"); var fastBtn = document.getElementById("fastBtn"); function setPlaybackRate(rate) { video.playbackRate = rate; } slowBtn.addEventListener("click", function() { setPlaybackRate(0.5); }); normalBtn.addEventListener("click", function() { setPlaybackRate(1); }); fastBtn.addEventListener("click", function() { setPlaybackRate(2); }); ``` CSS代码: ``` #myVideo { width: 640px; height: 360px; } button { background-color: #333; color: #fff; padding: 10px; border: none; border-radius: 5px; cursor: pointer; margin-right: 10px; } ``` 以上是我提供的回答,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值