视频试看5分钟

本文介绍了一种通过JavaScript控制视频播放时间的方法,以实现视频试看功能。具体实现了当视频播放达到预设时间(例如5分钟)后,自动暂停播放并显示重新开始按钮,用户点击后可从头开始播放。

缺陷:用户在控制台能直接拿到视频url地址

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <title>视频试看</title>
</head>
<style type="text/css">
  *{
    margin: 0;
    padding: 0;
  }
  .videobox{
    position: relative;
    width: 100%;
  }
  .videobox video{ width: 100%;  }
  .zhezhao{
    position: absolute;
    background: #000;
    width: 100%;
    top: 0;
    bottom: 0;
    display: none;
    z-index: 9;
    cursor: pointer;
  }
  .shikanjieshu{
    color: #fff;
    position: absolute;
    width: 100%;
    top: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
  .chongxinkaishibox{
    text-align: center;
    font-size: 12px;
  }
  .chongxinkaishibox img{
    width: 30px;
  }
</style>
<body>
<!--   https://y1pindao.com/app/img/mp4/afd83049775b55c1f9e93132d9ff1e3f.mp4 -->
    <div class="videobox">
      <video src="https://y1pindao.com/app/img/mp4/afd83049775b55c1f9e93132d9ff1e3f.mp4"  poster="loading.gif" controls preload="auto" id="player"></video>
      <div class="zhezhao">
        <div class="shikanjieshu">
          <div class="chongxinkaishibox">
            <img src="chongxinkaishi.png" />
            <div>试看结束,点击重新开始</div>
          </div>
        </div>
      </div>
    </div>

    <script type="text/javascript">
         let num = 60*5;
         let pl=document.getElementById("player");  
         pl.addEventListener("timeupdate",function(){
            if(parseInt(pl.currentTime)>=num){
              pl.pause()
              document.getElementsByClassName('zhezhao')[0].style.display = 'block';
              console.log(pl)
              pl.setAttribute('src','')
            }
         })
          document.getElementsByClassName("zhezhao")[0].addEventListener('click',function(){
            console.log('重新开始')
            document.getElementsByClassName('zhezhao')[0].style.display = 'none';
            pl.setAttribute('src','https://y1pindao.com/app/img/mp4/afd83049775b55c1f9e93132d9ff1e3f.mp4')
            pl.currentTime=0;
            pl.play();
          })
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值