js实现进度条的拖拽

目录

代码分析

搭建一个进度条

进度条的样式

js控制拖拽

js整体代码


代码分析

搭建一个进度条

html代码实现

 <!-- 外部容器 -->
    <div class="wrappers">
        <!-- 包裹进度条 -->
        <div class="wrapper">
            <!-- 进度条 -->
            <div id="progress">
                <!-- 进度条右边圆角 -->
                <div id="progress-bar"></div>
            </div>
        </div>
        <!-- 设置进度条默认显示 0%-->
        <span id="show">0%</span>
    </div>

进度条的样式

css代码实现

/*清除浏览器默认样式*/
*{
    margin:0;
    padding:0;
}
/* 设置外部容器 */
.wrappers{
    width:900px;
    height:6px;
    margin:100px auto;
}
/* 设置进度条容器 */
.wrapper{
    width:800px;
    height:6px;
    margin-bottom:0;
    line-height:6px;
    position:relative;
    background-color:hsl(108, 100%, 83%);
}
/* 设置进度条显示颜色 */
#progress{
    width:0;
    height:100%;
    background-color:red;
}
/* 设置进度条右侧圆点 */
#progress-bar{
    width:20px;
    height:20px;
    position:absolute;
    bottom:-6px;
    border-radius: 50%;
    background-color:hsl(252, 100%, 83%);
}
/* 设置显示百分比 */
#show{
    position:relative;
    left:840px;
    top:-12px;
}
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
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>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值