vue2實現(xiàn)簡易時鐘效果
更新時間:2022年08月30日 11:46:42 作者:蜂巢糖FCT
這篇文章主要為大家詳細介紹了vue2實現(xiàn)簡易時鐘效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue2實現(xiàn)簡易時鐘效果的具體代碼,供大家參考,具體內(nèi)容如下
1.vue2+純css實現(xiàn)
預覽效果:

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>以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue項目打包部署到GitHub Pages的實現(xiàn)步驟
本文主要介紹了Vue項目打包部署到GitHub Pages的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
詳解Vue如何實現(xiàn)顏色選擇與調(diào)色板功能
顏色選擇和調(diào)色板是Web開發(fā)中常用的功能,Vue作為一個流行的JavaScript框架,可以方便地實現(xiàn)顏色選擇和調(diào)色板功能,本文講講如何在Vue中進行顏色選擇和調(diào)色板吧2023-06-06
Vue + element實現(xiàn)動態(tài)顯示后臺數(shù)據(jù)到options的操作方法
最近遇到一個需求需要實現(xiàn)selector選擇器中選項值options 數(shù)據(jù)的動態(tài)顯示,而非寫死的數(shù)據(jù),本文通過實例代碼給大家分享實現(xiàn)方法,感興趣的朋友一起看看吧2021-07-07
vue3+electron12+dll開發(fā)客戶端配置詳解
本文將結合實例代碼,介紹vue3+electron12+dll客戶端配置,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-06-06
vue恢復初始數(shù)據(jù)this.$data,this.$options.data()解析
這篇文章主要介紹了vue恢復初始數(shù)據(jù)this.$data,this.$options.data()解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

