vue 模拟视频进度条组件(可支持点击,拖拽)

本文介绍了如何在Vue中创建一个模拟视频进度条组件,该组件支持用户通过点击和拖拽来控制视频播放进度。详细讲解了关键代码实现和交互逻辑。
摘要由CSDN通过智能技术生成

videoProgress.vue

<template>
    <div
        ref="progressWrapper"
        class="video-progress"
        @click="onTrackClick"
        @mousemove="mouseMove">
        <div class="video-progress-track" :style="{ 'width': progress + '%'}"></div>
        <div class="video-progress-point" ref="slider" :style="{ 'left': progress + '%'}" @mousedown="mouseDown"></div>
    </div>
</template>
<script>
export default {
    props: {
        progress: {
            type: Number,
            default: 0
        }
    },
    data() {
        return {
            isDrag: false,
            distanceX: 0,
            videoProgress: 0
        }
    },
    watch: {
        isDrag(val) {
            this.$emit('getDragStatus', val)
        },
        progress(val) {
            if (this.$refs.progressWrapper) {
                this.videoProgress = (this.progress * (this.$refs.progressWrapper.offsetWidth * this.zoom)) / 100
            }
        }
    },
    methods: {
        onTrackClick(event) {
            this.isDrag = false
            const sliderElement = this.$refs.slider
            if (event.target === sliderElement) return
            this.calculateDiatance(event.offsetX)
        },
        mouseDown(event) {
            if (this.progress === 100) return
            this.isDrag = true
            this.distanceX = event.pageX - this.videoProgress
            document.addEventListener('mouseup', () => {
                this.isDrag = false
            })
        },
        mouseMove(event) {
            if (this.isDrag) {
                const offsetX = event.pageX - this.distanceX
                this.calculateDiatance(offsetX)
            }
        },
        calculateDiatance(pageX) {
            const parentWidth = this.$refs.progressWrapper.offsetWidth
            const sliderWidth = this.$refs.slider.offsetWidth
            if (pageX > parentWidth - sliderWidth) {
     
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值