节流案例—记录视频播放位置

节流—记录视频播放位置

需求:页面打开,可以记录上一次视频播放位置

分析:

两个事件:

  1. ontimeupdate 事件在视频/音频(audio/video)当前的播放位置发生改变时触发
  2. onloadeddata 事件在当前帧的数据加载完成且还没有足够的数据播放视频/音频(audio/video)的下一帧时触发
<!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>Document</title>
    <style>
        .video{
      
      
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="video">
        <video src="../video/fly.mp4" controls poster="../imges/炫酷.png" width=800px height=auto style="text-align:center;" loop autoplay></video>
    </div>
    <script src="../js/lodash.min.js"></script>
<script>
        //1.获取元素,要对视频进行操作
        const video=document.querySelector('video')
        video.ontimeupdate=_.throttle(()=>{
      
      
            //获取当前的视频时间  video.currentTime
            //把当前的时间存储到本地存储
            localStorage.setItem('currentTime',video.currentTime)
        },1000)
        //打开页面触发事件,就从本地存储里面去除记录的时间,赋值给  video.currentTime
        video.onloadeddata=()=>{
      
      
            video.currentTime=localStorage.getItem('currentTime')||0

        }
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/L19541216/article/details/130176308
今日推荐