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>Document</title>
    <style>
        #div1 {
            width: 200px;
            height: 40px;
            border: 1px solid brown;
        }
        #div2 {
            height: 40px;
            background-color: brown;
        }
    </style>
    <script>
        window.onload=function(){
            var oDiv1=document.getElementById("div1");
            var oDiv2=document.getElementById("div2");
            var oSp=document.getElementById("sp");
            oDiv2.style.width=0+'px';
            //定时器  100毫秒
            var timer=setInterval(function(){
                oDiv2.style.width=parseInt(oDiv2.style.width)+1+'px';
                //百分数保留三位小数
                oSp.innerHTML=(parseInt(oDiv2.style.width)/200).toFixed(3)+"%";

                // 加载完成
                if(parseInt(oDiv2.style.width)==200){
                    // 清除定时器
                    clearInterval(timer);
                    alert("加载完成")
                    
                }
            },100);
        }
      
    </script>
</head>
<body>
   <div id="div1">
       <div id="div2">

       </div>
   </div>
    <span id="sp"></span>
</body>
</html>

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Video.js是一个基于Vue.js和Video.jsHTML5视频播放器组件,可以帮助我们快速搭建一个美观实用的视频播放器。在Vue Video.js中,要实现进度条拖动,需要做以下几个步骤: 1.首先,在Vue Video.js中,可以通过vjs-player组件来实现视频播放功能,我们需要给这个组件添加ref属性,以便在后面使用。 2.在vjs-player的mounted生命周期中,我们可以通过this.$refs.player来获取到video.js的player对象,然后通过player.on方法来监听时间变化事件。 3.在监听事件中,可以获取到当前播放的时间和视频总时长,从而计算出当前进度条的位置。同时,也可以监听进度条的mousedown和mousemove事件,来实现进度条拖动的功能。 4.最后,在进度条拖动结束的时候,需要通过player.currentTime()方法来设置视频的当前播放时间。 下面是相关代码片段: ```vue <template> <div> <vjs-player ref="player" :options="playerOptions"></vjs-player> <div class="progress-bar-wrap"> <div class="progress-bar" ref="progressBar" @mousedown="onProgressBarMouseDown($event)"> <div class="progress-bar-inner" :style="{width: progressWidth}"></div> <div class="progress-bar-thumb" :style="{left: progressWidth}"></div> </div> </div> </div> </template> <script> export default { data() { return { playerOptions: { autoplay: false, controls: true, sources: [{ src: 'https://example.com/path/to/video.mp4', type: 'video/mp4' }] }, progressWidth: 0, isDragging: false } }, mounted() { const player = this.$refs.player.$player player.on('timeupdate', () => { const currentTime = player.currentTime() const duration = player.duration() this.progressWidth = `${(currentTime / duration) * 100}%` }) document.addEventListener('mousemove', this.onDocumentMouseMove) document.addEventListener('mouseup', this.onDocumentMouseUp) }, beforeDestroy() { document.removeEventListener('mousemove', this.onDocumentMouseMove) document.removeEventListener('mouseup', this.onDocumentMouseUp) }, methods: { onProgressBarMouseDown(event) { this.isDragging = true this.onDocumentMouseMove(event) }, onDocumentMouseMove(event) { if (!this.isDragging) return const progressBar = this.$refs.progressBar const rect = progressBar.getBoundingClientRect() const progressWidth = ((event.clientX - rect.left) / rect.width) * 100 this.progressWidth = `${Math.min(Math.max(progressWidth, 0), 100)}%` }, onDocumentMouseUp() { if (!this.isDragging) return const player = this.$refs.player.$player const duration = player.duration() player.currentTime(duration * (parseInt(this.progressWidth) / 100)) this.isDragging = false } } } </script> <style> .progress-bar-wrap { width: 100%; height: 8px; background-color: #f2f2f2; } .progress-bar { position: relative; width: 100%; height: 100%; cursor: pointer; } .progress-bar-inner { position: absolute; top: 0; left: 0; height: 100%; background-color: #00bfff; } .progress-bar-thumb { position: absolute; top: -5px; left: -8px; width: 16px; height: 16px; border-radius: 50%; background-color: #00bfff; } </style>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值