HTML制作一个时钟走动效果

大家好,今天制作一个时钟走动效果!

先看具体效果:
在这里插入图片描述

一、以下是一个简单的时钟走动效果的实现,使用了HTML、JavaScript和CSS技术。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实时时钟</title>
<style>
    body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        background-color: #f0f0f0;
    }
    .clock {
        width: 200px;
        height: 200px;
        border: 8px solid #333;
        border-radius: 50%;
        position: relative;
        padding: 20px;
        box-sizing: border-box;
    }
    .clock .number {
        position: absolute;
        width: 100%;
        height: 100%;
        text-align: center;
        color: #333;
        font-size: 18px;
        font-family: Arial, sans-serif;
    }
    .clock .hand {
        position: absolute;
        width: 50%;
        height: 6px;
        background: black;
        top: 50%;
        transform-origin: 100%;
        transform: rotate(90deg);
        transition: all 0.05s;
        transition-timing-function: cubic-bezier(0.4, 2.3, 0.3, 1);
    }
</style>
</head>
<body>
<div class="clock">
    <div class="hand hour-hand"></div>
    <div class="hand min-hand"></div>
    <div class="hand second-hand"></div>
</div>

<script>
    function setClock() {
        const currentDate = new Date();
        const secondsRatio = currentDate.getSeconds() / 60;
        const minutesRatio = (secondsRatio + currentDate.getMinutes()) / 60;
        const hoursRatio = (minutesRatio + currentDate.getHours()) / 12;

        setRotation(document.querySelector('.second-hand'), secondsRatio);
        setRotation(document.querySelector('.min-hand'), minutesRatio);
        setRotation(document.querySelector('.hour-hand'), hoursRatio);
    }

    function setRotation(element, rotationRatio) {
        element.style.transform = `rotate(${rotationRatio * 360}deg)`;
    }

    setInterval(setClock, 1000);
    setClock(); // initial call to set the clock when the page loads
</script>
</body>
</html>

代码解释:

  1. HTML:定义了一个时钟容器 .clock,里面包含了三个子元素,分别表示时针、分针和秒针。
  2. CSS:定义了时钟的样式,包括大小、边框、位置等。.hand 类用于定义时钟指针的样式。
  3. JavaScript:通过 setClock 函数获取当前时间,并计算秒针、分针和时针的旋转角度。setRotation 函数用于设置每个指针的旋转角度。使用 setInterval 每秒调用 setClock 更新时钟。

效果一般般后面又修改了一下:
当然可以,我们可以增加一些额外的功能来使时钟的走动效果更加复杂和有趣。例如,我们可以添加时针、分针和秒针的阴影效果,使它们看起来更加立体。我们还可以添加一个数字时钟显示,以及一个动态的日期显示。

二、下面是一个更加复杂的时钟走动效果的实现:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>复杂实时时钟</title>
<style>
    body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        background-color: #f0f0f0;
        font-family: Arial, sans-serif;
    }
    .clock {
        position: relative;
        width: 300px;
        height: 300px;
        border: 10px solid #333;
        border-radius: 50%;
        box-shadow: 0 0 20px 0 rgba(0,0,0,0.4);
    }
    .clock .hand {
        position: absolute;
        width: 50%;
        height: 6px;
        background: black;
        top: 50%;
        transform-origin: 100%;
        border-radius: 6px;
        box-shadow: 0 0 10px 2px rgba(0,0,0,0.6);
    }
    .clock .second-hand {
        background-color: red;
    }
    .clock .number {
        position: absolute;
        width: 100%;
        height: 100%;
        text-align: center;
        color: #333;
        font-size: 24px;
        line-height: 300px;
    }
    .clock-info {
        text-align: center;
        margin-top: 20px;
        font-size: 20px;
    }
</style>
</head>
<body>
<div class="clock">
    <div class="hand hour-hand"></div>
    <div class="hand min-hand"></div>
    <div class="hand second-hand"></div>
    <div class="number">12</div>
</div>
<div class="clock-info">
    <span id="digital-clock"></span>
    <div id="date"></div>
</div>

<script>
    function updateClock() {
        const currentDate = new Date();
        const secondsRatio = currentDate.getSeconds() / 60;
        const minutesRatio = (secondsRatio + currentDate.getMinutes()) / 60;
        const hoursRatio = (minutesRatio + currentDate.getHours()) / 12;

        setRotation(document.querySelector('.second-hand'), secondsRatio);
        setRotation(document.querySelector('.min-hand'), minutesRatio);
        setRotation(document.querySelector('.hour-hand'), hoursRatio);

        updateDigitalClock(currentDate);
        updateDate(currentDate);
    }

    function setRotation(element, rotationRatio) {
        element.style.transform = `rotate(${rotationRatio * 360}deg)`;
    }

    function updateDigitalClock(date) {
        const hours = date.getHours().toString().padStart(2, '0');
        const minutes = date.getMinutes().toString().padStart(2, '0');
        const seconds = date.getSeconds().toString().padStart(2, '0');
        document.getElementById('digital-clock').textContent = `${hours}:${minutes}:${seconds}`;
    }

    function updateDate(date) {
        const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        const day = days[date.getDay()];
        const month = months[date.getMonth()];
        const year = date.getFullYear();
        document.getElementById('date').textContent = `${day}, ${month} ${date.getDate()}, ${year}`;
    }

    setInterval(updateClock, 1000);
    updateClock(); // initial call to set the clock when the page loads
</script>
</body>
</html>

代码解释:

  1. HTML:添加了一个数字时钟显示 (<span id="digital-clock"></span>) 和一个日期显示 (<div id="date"></div>)。
  2. CSS:增加了 .clock-info 类来样式化数字时钟和日期显示。
  3. JavaScript
    • updateClock 函数现在同时更新指针、数字时钟和日期。
    • updateDigitalClock 函数用于更新数字时钟显示。
    • updateDate 函数用于更新日期显示。

源码下载:时钟走动效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/766923.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Linux】生物信息学常用基本命令

wget网址用于直接从网上下载某个文件到服务器&#xff0c;当然也可以直接从网上先把东西下到本地然后用filezilla这个软件来传输到服务器上。 当遇到不会的命令时候&#xff0c;可以使用man “不会的命令”来查看这个命令的详细信息。比如我想要看看ls这个命令的详细用法&…

Linux磁盘监控小技巧

作者&#xff1a;田逸&#xff08;formyz&#xff09; 默认情况下&#xff0c;使用Nrpe插件check_disk加选项“-w”与”-c”除了输出我们想监控的磁盘分区外&#xff0c;还输出了一些形如“/dev/shm”不需要监控的项目(如下图所示)&#xff0c;倒对查看起到了一些干扰作用。 从…

解决使用monaco-editor编译器,编译器展示内容没有超过编译器高度,但是出现滚动条问题

前言&#xff1a; 最近在完成项目时&#xff0c;有使用编译器进行在线编辑的功能&#xff0c;就选用了monaco-editor编译器&#xff0c;但是实现功能之后&#xff0c;发现即使在编译器展示的内容没有超过编译器高度的情况下&#xff0c;编译器依旧存在滚动条&#xff0c;会展示…

64、基于去噪卷积神经网络的彩色图像去噪(matlab)

1、基于去噪卷积神经网络的彩色图像去噪的原理及流程 基于去噪卷积神经网络的彩色图像去噪是一种基于深度学习的图像处理技术&#xff0c;可以有效地去除图像中的噪声&#xff0c;提高图像的质量。下面是在Matlab中实现基于去噪卷积神经网络的彩色图像去噪的原理及流程&#x…

01:Linux的基本命令

Linux的基本命令 1、常识1.1、Linux的隐藏文件1.2、绝对路径与相对路径 2、基本命令2.1、ls2.2、cd2.3、pwd / mkdir / mv / touch / cp / rm / cat / rmdir2.4、ln2.5、man2.6、apt-get 本教程是使用的是Ubuntu14.04版本。 1、常识 1.1、Linux的隐藏文件 在Linux中&#xf…

centos7 mqtt服务mosquitto搭建记录

1、系统centos7.6&#xff0c;安装默认版本 yum install mosquitto 2、启动运行 systemctl start mosquitto 3、设置自启动 systemctl enable mosquitto 4、修改配置文件 vim /etc/mosquitto/mosquitto.conf 监听端口&#xff0c;默认为1883&#xff0c;需要修改删除前面…

Python番外篇之责任转移:有关于虚拟机编程语言的往事

编程之痛 如果&#xff0c;你像笔者一样&#xff0c;有过学习或者使用汇编语言与C、C等语言的经历&#xff0c;一定对下面所说的痛苦感同身受。 汇编语言 将以二进制表示的一条条CPU的机器指令&#xff0c;以人类可读的方式进行表示。虽然&#xff0c;人类可读了&#xff0c…

thinksboard新建table表格

html文件 <div fxFlex fxLayoutAlign"left top" style"display: block"> <!-- <mat-card appearance"raised" style"max-height: 80vh; overflow-y: auto;">--><div><button mat-raised-button (click)&…

数据结构(JAVA)—代码题

01-数据结构—判断题 02-数据结构—选择题 03 数据结构—多选填空程序填空 ​ 01-顺序表的建立及遍历 import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; import java.util.Scanner;public class Main {public static void main(St…

告别熬夜改稿:AI降重工具让论文降重变得轻松又有趣

已经天临五年了&#xff0c;大学生们还在为论文降重烦恼……手动降重确实是个难题&#xff0c;必须要先付点小经费去靠谱的网站查重&#xff0c;再对着红字标注去改&#xff0c;后面每一次的论文呢查重结果都像赌//博&#xff0c;谁也不知道明明是同一篇文章&#xff0c;第二次…

【C语言】union 关键字

在C语言中&#xff0c;union关键字用于定义联合体。联合体是一种特殊的数据结构&#xff0c;它允许不同的数据类型共享同一段内存。所有联合体成员共享同一个内存位置&#xff0c;因此联合体的大小取决于其最大成员的大小。 定义和使用联合体 基本定义 定义一个联合体类型时…

【MySQL】MySQL锁冲突排障纪要

【MySQL】MySQL锁冲突排障纪要 开篇词&#xff1a;干货篇&#xff1a;1.查看当前innodb status,里面包含事务,锁占用情况2.查看mysql进程状态3.查看innodb事务&#xff0c;锁&#xff0c;锁等待情况4.定位持有锁的线程信息 总结篇&#xff1a;一、锁冲突的原因二、锁冲突的表现…

【Python】列表

目录 一、列表的概念 二、列表的创建 1.变量名 [ ] ..... 2.通过Python内置 的I ist类的构造函数来创建列表 三、操作列表元素的方法 1. 修改 2. 增加元素 3. 删除 4. 其他操作 四、遍历列表 五、列表排序 六、列表切片&#xff08;list slicing&#xff09; 七、…

Python入门 2024/7/2

目录 格式化的精度控制 字符串格式化 对表达式进行格式化 小练习&#xff08;股票计算小程序&#xff09; 数据输入 布尔类型和比较运算符 if语句 小练习&#xff08;成人判断&#xff09; if-else语句 if-elif-else语句 练习&#xff1a;猜猜心里数字 嵌套语句 猜…

JavaScript中的Array(数组)对象

目录 一、Array数组对象 1、介绍 2、创建数组对象并赋值 3、访问数组元素 二、Array对象属性 1、constructor属性 2、length属性 3、prototype属性 三、Array对象的常用方法 1、isArray() 2、concat() 3、pop() 4、shift() 5、push() 6、unshift() 7、reverse(…

前端进阶:Vue.js

目录 框架&#xff1a; 助解&#xff1a; 框架&#xff1a; VUE 什么是Vue.js? Vue.js优点 Vue安装 方式一&#xff1a;直接用<script>引入 方式二&#xff1a;命令行工具 第一个Vue程序 代码 代码解释&#xff1a; 运行 Vue指令 v-text v-html v-tex…

git 中有关 old mode 100644、new mode 10075的问题解决小结

问题&#xff1a; 同一个文件被修改后&#xff0c;最后代码没有变&#xff08;代码刚开始修改了&#xff0c;最后又删除还原了&#xff09;&#xff0c;文件变了&#xff0c;导致提交了一个空文件 git diff 提示 filemode 发生改变&#xff08;old mode 100644、new mode 1007…

RabbitMQ进阶篇

文章目录 发送者的可靠性生产者重试机制实现生产者确认 MQ的可靠性数据持久化交换机持久化队列持久化消息持久化 Lazy Queue(可配置~)控制台配置Lazy模式代码配置Lazy模式更新已有队列为lazy模式 消费者的可靠性消费者确认机制失败重试机制失败处理策略 业务幂等性唯一消息ID业…

layui-页面布局

1.布局容器 分为固定和完整宽度 class layui-container 是固定宽度 layui-fluid是完整宽度

傻瓜交换机多网段互通组网、设备无法配置网关案例

记录一下&#xff1a; 一、傻瓜交换机多网段互通组网 1、客户在核心交换机上创建了VLAN10&#xff0c;VLAN20。 VLAN10&#xff1a;IP192.168.10.254 VLAN20&#xff1a;IP192.168.20.254 在核心交换机下挂了一台傻瓜交换机&#xff0c;傻瓜交换机接入了一台OA服务器IP&#…