vue2实现简易时钟效果

 更新时间:2022年08月30日 11:46:42   作者:蜂巢糖FCT  
这篇文章主要为大家详细介绍了vue2实现简易时钟效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue2实现简易时钟效果的具体代码,供大家参考,具体内容如下

1.vue2+纯css实现

预览效果:

2.代码如下:

<template>
    <div class="main">
        <div class="time">
            <div class="hour_wrap">
                <div class="hour_item" :style="{transform:'translate(-50%,-50%)'+'rotate('+30*(index+1)+'deg)'}" v-for="(item,index) in 12" :key="index">
                   
                    {{index+1}}
                     <div class="ke"></div>
                </div>
            </div>
            <div class="minute_wrap">
                <div class="minute_item" :style="{transform:'translate(-50%,-50%)'+'rotate('+6*(index+1)+'deg)'}" v-for="                        (item,index) in 60" :key="index">
                     <div class="ke"></div>
                </div>
            </div>
            <div class="hour_hand" :style="{transform:'translate(-50%,-100%)'+'rotate('+30*hour+'deg)'}"></div>
            <div class="minute_hand" :style="{transform:'translate(-50%,-100%)'+'rotate('+6*minute+'deg)'}"></div>
            <div class="second_hand" :style="{transform:'translate(-50%,-100%)'+'rotate('+6*second+'deg)'}"></div>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            interval:{},
            date:'',
            hour:0,
            minute:0,
            second:0,

        }
    },
    mounted(){
        this.interval = setInterval(()=>{
            this.date = this.getDate();
            this.hour = this.date.toString().split(' ')[1].split(':')[0];
            this.minute = this.date.toString().split(' ')[1].split(':')[1];
            this.second = this.date.toString().split(' ')[1].split(':')[2];
        },1000);
    },
    beforeDestroy(){
        clearInterval(this.interval);
    },
    methods:{
       getDate(time,format){
        var tf = function (i) {
            return (i < 10 ? '0' : '') + i
        };
        var now = time?new Date(time):new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        if(format=='yyyy-mm-dd HH:mm:ss'){
          return year + "-" + tf(month) + "-" + tf(date)+' '+hour+':'+tf(minute)+':'+tf(second);
        }else{
          return year + "/" + tf(month) + "/" + tf(date)+' '+hour+':'+tf(minute)+':'+tf(second);
        }
    }
    }
}
</script>
<style scoped lang="less">
.time{
    border-radius:50%;
    width: 140px;
    height: 140px;
    border: 1px solid #000;
    position: relative;
    .hour_wrap{
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 3;
        .hour_item{
            position: absolute;
            top: 50%;
            left: 50%;
            height: 100%;
            width: 12px;
            font-size: 12px;
            text-align: center;
            transform-origin: 6px 70px; 
            //transform: translate(-50%,-50%);
            .ke{
                width: 3px;
                height: 8px;
                background-color: #000;
                margin: 0 auto;
            }
        }
    }
    .minute_wrap{
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 2;
        .minute_item{
            position: absolute;
            top: 50%;
            left: 50%;
            height: 100%;
            width: 10px;
            font-size: 12px;
            text-align: center;
            transform-origin: 5px 70px; 
            //transform: translate(-50%,-50%);
            .ke{
                width: 2px;
                height: 4px;
                background-color: #000;
                margin: 0 auto;
                margin-top: 10px;
            }
        }
    }
    .hour_hand{
        width: 3px;
        height: 30px;
        background-color: #000;
        position: absolute;
        top: 50%;
        left: 50%;
        transform-origin: 1.5px 30px;
    }
    .minute_hand{
        width: 2px;
        height: 50px;
        background-color: #000;
        position: absolute;
        top: 50%;
        left: 50%;
        transform-origin: 1px 50px;
    }
    .second_hand{
        width: 1px;
        height: 60px;
        background-color: #000;
        position: absolute;
        top: 50%;
        left: 50%;
        transform-origin: 0.5px 60px;
    }
}

</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue中如何创建多个ueditor实例教程

    vue中如何创建多个ueditor实例教程

    这篇文章主要给大家介绍了关于vue中如何创建多个ueditor的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • Vue项目打包部署到GitHub Pages的实现步骤

    Vue项目打包部署到GitHub Pages的实现步骤

    本文主要介绍了Vue项目打包部署到GitHub Pages的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • 详解Vue如何实现颜色选择与调色板功能

    详解Vue如何实现颜色选择与调色板功能

    颜色选择和调色板是Web开发中常用的功能,Vue作为一个流行的JavaScript框架,可以方便地实现颜色选择和调色板功能,本文讲讲如何在Vue中进行颜色选择和调色板吧
    2023-06-06
  • vue3之向echarts组件传值的问题分析

    vue3之向echarts组件传值的问题分析

    这篇文章主要介绍了vue3之向echarts组件传值的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • Vue + element实现动态显示后台数据到options的操作方法

    Vue + element实现动态显示后台数据到options的操作方法

    最近遇到一个需求需要实现selector选择器中选项值options 数据的动态显示,而非写死的数据,本文通过实例代码给大家分享实现方法,感兴趣的朋友一起看看吧
    2021-07-07
  • vue如何将v-for中的表格导出来

    vue如何将v-for中的表格导出来

    这篇文章主要介绍了vue如何将v-for中的表格导出来,需要的朋友可以参考下
    2018-05-05
  • vue3+electron12+dll开发客户端配置详解

    vue3+electron12+dll开发客户端配置详解

    本文将结合实例代码,介绍vue3+electron12+dll客户端配置,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-06-06
  • vue使用urlEncode问题

    vue使用urlEncode问题

    这篇文章主要介绍了vue使用urlEncode问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • vue恢复初始数据this.$data,this.$options.data()解析

    vue恢复初始数据this.$data,this.$options.data()解析

    这篇文章主要介绍了vue恢复初始数据this.$data,this.$options.data()解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Vue渲染函数详解

    Vue渲染函数详解

    下面小编就为大家带来一篇Vue渲染函数详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09

最新评论