当前位置: 首页 > news >正文

vue js实现时钟以及刻度效果

2025.01.08今天我学习如何用js实现时钟样式,效果如下:

一、html代码如下:

<template><!--圆圈--><div class="notice_border"><div class="notice_position notice_name_class" v-for="item in [3,6,9,12]">{{ item }}</div><!--最中心的点--><div class="notice_node"/><!--时针--><div class="notice_hour_class" ref="hour_time"/><!--分针--><div class="notice_minutes_class" ref="second_time"/><!--刻度线--><div class="scale_class" :ref="`scale_${index}`" v-for="(item,index) in 60" :key="index"/></div>
</template>

二、js代码如下:

<script>
export default{data(){return{}},mounted(){this.get_notice_time();},methods:{get_notice_time(){let notice_time = '12:00';//时间格式for (let i = 0; i < 60; i++) {let scale_class = this.$refs[`scale_${i}`];  // 获取每个元素scale_class[0].style.transform = `rotate(${i * 6}deg)`;  // 修改样式}let hour_time = this.$refs.hour_time;//时针let second_time = this.$refs.second_time;//分针if (notice_time!= 0) {hour_time.style.transform = `rotate(${moment(notice_time.split(':')[0], 'HH').format('H') * 30}deg)`;//时针一次转30°second_time.style.transform = `rotate(${moment(notice_time.split(':')[1], 'mm').format('m') * 6}deg)`;//分针一次转6°}}}
}
</script>

三、style代码如下:

<style>
//圆圈样式
.notice_border {width: 58%;height: 57%;border-radius: 100%;border: 5px solid #3673E3;position: absolute;right: 10%;top: 14%;
}//字体位置
.notice_position {position: absolute;color: skyblue;font-weight: bolder;
}//字体样式 3,6,9,12
.notice_name_class:nth-child(1) {right: 7%;top: 41%;
}.notice_name_class:nth-child(2) {right: 44%;bottom: 4%;
}.notice_name_class:nth-child(3) {left: 8%;top: 41%;
}.notice_name_class:nth-child(4) {left: 42%;top: 4%;
}//节点样式
.notice_node {position: absolute;width: 10%;height: 10%;border-radius: 100%;background-color: #3673E3;left: 45%;top: 45%;z-index: 2;
}//时针样式
.notice_hour_class {position: absolute;width: 5%;height: 20%;background-color: red;left: 47.5%;top: 30%;border-radius: 5px 5px 0 0;z-index: 1;transform-origin: bottom; //绕着底部旋转//transform: rotate(90deg);
}//分针样式
.notice_minutes_class {position: absolute;width: 5%;height: 33%;background-color: #3673E3;//background-color: white;left: 47.5%;top: 18%;border-radius: 5px 5px 0 0;transform-origin: bottom; //绕着底部旋转
}//默认刻度线
.scale_class {position: absolute;width: 1%;height: 7%;background-color: #3673E3;top: 0;left: 50%;transform-origin: center 60px;//设置中心点旋转,要绕着notice_node 
}//循环每一个刻度线,5的倍数
.scale_class:nth-of-type(5n-2) {width: 2.5%;height: 10%;
}
</style>

四、完整代码如下:

可以用作时钟的通用组件。

<template><!--圆圈--><div class="notice_border"><div class="notice_position notice_name_class" v-for="item in [3,6,9,12]">{{ item }}</div><!--最中心的点--><div class="notice_node"/><!--时针--><div class="notice_hour_class" ref="hour_time"/><!--分针--><div class="notice_minutes_class" ref="second_time"/><!--刻度线--><div class="scale_class" :ref="`scale_${index}`" v-for="(item,index) in 60" :key="index"/></div>
</template>
<script>
import moment from "moment";export default {data(){return{notice_time:'',//时间}},props: {// 获取传入时间props_time: {type: [String,Number],}},watch:{props_time(newVal,oldVal){this.notice_time = newVal;this.get_notice_time();},deep:true},methods: {get_notice_time() {//let notice_time = this.notice_time;//时间格式let notice_time = '12:00';//时间格式for (let i = 0; i < 60; i++) {let scale_class = this.$refs[`scale_${i}`];  // 获取每个元素scale_class[0].style.transform = `rotate(${i * 6}deg)`;  // 修改样式}let hour_time = this.$refs.hour_time;//时针let second_time = this.$refs.second_time;//分针if (notice_time != 0) {//防止时间为空hour_time.style.transform = `rotate(${moment(notice_time.split(':')[0], 'HH').format('H') * 30}deg)`;//时针一次转30°second_time.style.transform = `rotate(${moment(notice_time.split(':')[1], 'mm').format('m') * 6}deg)`;//分针一次转6°}}}
}
</script>
<style scoped lang="less">
.notice_border {width: 58%;height: 57%;border-radius: 100%;border: 5px solid #3673E3;position: relative;
}.notice_position {position: absolute;color: skyblue;font-weight: bolder;
}.notice_name_class:nth-child(1) {right: 7%;top: 41%;
}.notice_name_class:nth-child(2) {right: 44%;bottom: 4%;
}.notice_name_class:nth-child(3) {left: 8%;top: 41%;
}.notice_name_class:nth-child(4) {left: 42%;top: 4%;
}.notice_node {position: absolute;width: 10%;height: 10%;border-radius: 100%;background-color: #3673E3;left: 45%;top: 45%;z-index: 2;
}.notice_hour_class {position: absolute;width: 5%;height: 20%;background-color: red;left: 47.5%;top: 30%;border-radius: 5px 5px 0 0;z-index: 1;transform-origin: bottom;//transform: rotate(90deg);
}.notice_minutes_class {position: absolute;width: 5%;height: 33%;background-color: #3673E3;left: 47.5%;top: 18%;border-radius: 5px 5px 0 0;transform-origin: bottom;
}.scale_class {position: absolute;width: 1%;height: 7%;background-color: #3673E3;top: 0;left: 50%;transform-origin: center 60px;
}.scale_class:nth-of-type(5n-2) {width: 2.5%;height: 10%;
}
</style>

http://www.lryc.cn/news/518064.html

相关文章:

  • unity学习15:预制体prefab
  • 基于Thinkphp6+uniapp的陪玩陪聊软件开发方案分析
  • MySQL - 子查询和相关子查询详解
  • Android 系统签名 keytool-importkeypair
  • 安卓漏洞学习(十八):Android加固基本原理
  • Docker 使用Dockerfile创建镜像
  • 【Python运维】利用Python实现高效的持续集成与部署(CI/CD)流程
  • 成功!QT 5.15.2编译mysql驱动
  • 安卓NDK视觉开发——手机拍照文档边缘检测实现方法与库封装
  • 第二届 Sui 游戏峰会将于 3 月 18 日在旧金山举行
  • 自动驾驶相关知识学习笔记
  • uniapp - 基于uniapp+vue3实现自定义增强版table表格组件体验「兼容H5+小程序+App端」
  • 新时期下k8s 网络插件calico 安装
  • 【SQL】COUNT()函数 用法详解
  • 【HTML+CSS+JS+VUE】web前端教程-6-图片路径详解
  • C++中面向对象的三大特性是什么?
  • Centos 修改 yum 源为阿里云
  • Qt之Cannot create children for a parent that is in a different thread问题分析
  • 均值滤波从图像复原角度的解释
  • Tableau数据可视化与仪表盘搭建-数据连接
  • VsCode对Arduino的开发配置
  • 2024版idea 插件无法加载
  • VLMs之Agent之CogAgent:CogAgent的简介、安装和使用方法、案例应用之详细攻略
  • Unity3D仿星露谷物语开发19之库存栏丢弃及交互道具
  • Kafka优势剖析-消费者组、并行消费
  • Docker+Jmeter+InfluxDB+Grafana 搭建性能监控平台
  • Maven 详细配置:Maven settings 配置文件的详细说明
  • 【文本分类】bert二分类
  • 单例模式-如何保证全局唯一性?
  • 设计模式学习笔记——结构型模式