视频回放,进度条时间显示(vue3)

效果:

<template>
    <div class="time-rule">
        <div :style="{ paddingLeft: initiate + 'px',paddingRight:`${1439-finish}`+ 'px' }">
            <div v-if="initiate!==0" style="background: #FF4D23;">&nbsp;</div>
        </div>
        <!-- 整个时间选择器的容器,设置了宽度、高度和背景颜色 -->
        <div class="time-day" @mousedown="handleMouseDown" @mousemove="handleMouseMove" @mouseup="handleMouseUp">
            <div class="time-minute" v-for="hour in timesInDay" :key="hour">{{ hour }}</div>
        </div>
        <!-- 前选择的时间点的红色竖线,它的位置由用户的操作来决定。 -->
        <div class="time-cursor" :style="{ left: cursorPosition1 + 'px' }">
            <!-- 显示当前选择的具体时间的文本框,它的位置相对于time-cursor,可以通过拖动来改变选择的时间 -->
            <div class="time-cursor-text">{{ formatTime(cursorPosition1) }}</div>
        </div>
        <div class="time-cursor" :style="{ left: cursorPosition2 + 'px' }">
            <div class="time-cursor-text">{{ formatTime(cursorPosition2) }}</div>
        </div>
    </div>
</template>

<script setup>
    import { ref, computed } from 'vue';
    const initiate = ref(40);
    const finish = ref(410);
    const cursorPosition1 = ref(40); // 第一个光标位置
    const cursorPosition2 = ref(410); // 第二个光标位置
    // 时间显示
    const timesInDay = Array.from({ length: 24 }, (_, i) => {
        const hour = String(i).padStart(2, '0');
        return `${hour}:00`;
    });
    // 鼠标按下事件处理方法
    const handleMouseDown = (e) => {
        const rect = e.target.getBoundingClientRect();
        const x = e.clientX - rect.left;
        // 根据当前鼠标位置选择更新哪个光标的位置
        if (x <= cursorPosition2.value / 2) {
            cursorPosition1.value = x + 0.5;
        } else {
            cursorPosition2.value = x + 0.5;
        }
    };
    // 鼠标移动事件处理方法
    const handleMouseMove = (e) => {
        if (e.buttons === 1) {
            const rect = e.target.getBoundingClientRect();
            const x = e.clientX - rect.left;
            // 根据当前鼠标位置选择更新哪个光标的位置
            if (x <= cursorPosition2.value / 2) {
                cursorPosition1.value = x + 0.5;
            } else {
                cursorPosition2.value = x + 0.5;
            }
        }
    };
    // 鼠标释放事件处理方法
    const handleMouseUp = () => {
        // 清除光标位置
        cursorPosition1.value = 0;
        cursorPosition2.value = 0;
    };
    // 下方时间框时间
    const formatTime = (value) => {
        const hours = Math.floor(value / 60);
        const minutes = value % 60;
        const formattedHours = String(hours).padStart(2, '0');
        const formattedMinutes = String(minutes).padStart(2, '0');
        return `${formattedHours}:${formattedMinutes}`;
    };
</script>

<style scoped>
    .time-rule {
        position: relative;
        height: 65px;
        /* margin: 0 auto; */
        width: 100%;
        font-size: 12px;
        max-width: 1442px;
        background-color: #CCC;
        margin-top: 30px;
    }

    .time-day {
        display: flex;
        position: absolute;
        left: 0;
        top: 15px;
        height: 100%;
        width: 1440px;
        cursor: pointer;
        border-top: 1px solid #999;
    }

    .time-minute {
        width: calc(100% / 24);
        height: 8px;
        text-align: center;
        border-left: 1px solid #999;
        position: relative;
    }

    .time-cursor {
        position: absolute;
        left: 0;
        top: 0px;
        height: 60px;
        width: 2px;
        background-color: #FF4D23;
        text-align: center;

        transform: translateX({
                {
                cursorPosition
            }
        }

        px);
    }

    .time-cursor-text {
        position: absolute;
        padding: 0 5px;
        left: -20px;
        top: 48px;
        border: 1px solid #FF4D23;
        height: 12px;
        line-height: 12px;
        cursor: move;
        background-color: white;
        -ms-user-select: none;
        user-select: none;
    }
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值