javascript 时间刻度尺 ruler 插件

如图时间刻度尺:

完整代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta name="viewport"
        content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .ruler-wrap {
            width: 100%;
            max-width: 600px;
            /*height: 60px;*/
            line-height: 1px;
            overflow: hidden;
            margin: 0 auto 50px;
        }
        .text-input {
            display: block;
            width: 100px;
            height: 30px;
            border-radius: 5px;
            background: #f6f6f6;
            border: none;
            text-align: center;
            font-size: 14px;
            color: #4142cc;
            font-weight: bold;
            letter-spacing: 1px;
            margin: 0 auto;
        }

        .text-input:focus {
            outline: none;
        }
    </style>
</head>

<body>

    <!-- 刻度尺容器(必要的) -->
    <input id="rulerText" class="text-input" type="text" readonly value="">
    <div class="ruler-wrap" id="ruler"></div>
    <!-- 引入刻度尺插件js -->
    <script src="js/ruler.js"></script>
    <script>
        var rulerText = document.getElementById("rulerText");
        rulerText.value = "12:00";
        //调用刻度尺方法
        ruler.initPlugin({
            el: "ruler", //容器id
            startValue: 120,
            color: '#fff',
            background: "#333",
            markColor: "#409EFF",
            success: function (res) {
                if (res === 240) {
                    rulerText.value = "24:00";
                    return;
                }
                var zeroTime = new Date();//获取当天零点时间
                zeroTime.setHours(0, 0, 0, 0)
                zeroTime.setMinutes((res * 6));

                var hours = zeroTime.getHours() < 10 ? "0" + zeroTime.getHours() : zeroTime.getHours();
                var minutes = zeroTime.getMinutes() < 10 ? "0" + zeroTime.getMinutes() : zeroTime.getMinutes();

                rulerText.value = hours + ":" + minutes;
            }
        });

    </script>

</body>

</html>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
时间刻度尺可以使用 Vue.js 的自定义组件来实现。下面是一个简单的实现示例: ```vue <template> <div class="time-scale"> <div class="time-scale__ruler"> <div class="time-scale__ruler-line" v-for="i in rulerCount" :key="i" :style="{ left: i * rulerWidth + 'px' }"></div> </div> <div class="time-scale__labels"> <div class="time-scale__label" v-for="label in labels" :key="label" :style="{ left: label.position + 'px' }">{{ label.text }}</div> </div> </div> </template> <script> export default { props: { startTime: { type: Date, required: true }, endTime: { type: Date, required: true }, labelInterval: { type: Number, default: 1 } }, computed: { rulerCount() { const duration = this.endTime - this.startTime; const interval = this.labelInterval * 60 * 60 * 1000; return Math.ceil(duration / interval); }, rulerWidth() { return 100 / this.rulerCount; }, labels() { const labels = []; const interval = this.labelInterval * 60 * 60 * 1000; for (let i = 0; i <= this.rulerCount; i++) { const time = new Date(this.startTime.getTime() + i * interval); labels.push({ text: time.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }), position: i * this.rulerWidth }); } return labels; } } }; </script> <style scoped> .time-scale { position: relative; height: 50px; border: 1px solid #ccc; } .time-scale__ruler { position: absolute; top: 0; left: 0; right: 0; bottom: 20px; } .time-scale__ruler-line { position: absolute; top: 0; bottom: 0; width: 1px; background-color: #ccc; } .time-scale__labels { position: absolute; bottom: 0; left: 0; right: 0; height: 20px; display: flex; justify-content: space-between; } .time-scale__label { position: absolute; bottom: 0; font-size: 12px; white-space: nowrap; } </style> ``` 在上面的示例中,我们定义了一个名为 `TimeScale` 的组件,它接受三个属性: - `startTime`:起始时间,必需的。 - `endTime`:结束时间,必需的。 - `labelInterval`:标签间隔,以小时为单位,默认为 1。 然后,我们使用计算属性来计算刻度尺中的一些值,例如刻度线的数量、刻度线的宽度和标签的位置和文本。最后,我们在模板中使用 `v-for` 指令来渲染刻度线和标签。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值