使用JavaScript及HTML、CSS完成秒表计时器

2024-04-23 06:20

本文主要是介绍使用JavaScript及HTML、CSS完成秒表计时器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

案例要求

1.界面为一个显示计时面板和三个按钮分别为:开始,暂停,重置
2.点击开始,面板开始计时,
3.点击暂停,面板停止
4.点击重置,计时面板重新为0

案例源码

<!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 {font-family: Arial, sans-serif;background-color: #f4f4f4;margin: 0;padding: 0;display: flex;justify-content: center;align-items: center;height: 100vh;}.container {text-align: center;}#timerDisplay {font-size: 36px;margin-bottom: 20px;}.clock {width: 200px;height: 200px;border: 8px solid #007bff;border-radius: 50%;position: relative;margin-bottom: 20px;}.hand {width: 2px;background-color: #007bff;position: absolute;top: 50%;left: 50%;transform-origin: bottom center;}.hour-hand {height: 50px;}.minute-hand {height: 70px;}.second-hand {height: 80px;background-color: red;}.center-dot {width: 8px;height: 8px;background-color: #007bff;border-radius: 50%;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);}.buttons {display: flex;justify-content: center;}.button {background-color: #007bff;color: #fff;border: none;padding: 10px 20px;cursor: pointer;margin-right: 10px;}
</style>
</head>
<body><div class="container"><div id="timerDisplay">00:00:00</div><div class="clock"><div class="hand hour-hand"></div><div class="hand minute-hand"></div><div class="hand second-hand"></div><div class="center-dot"></div></div><div class="buttons"><button id="startButton" class="button">开始</button><button id="pauseButton" class="button">暂停</button><button id="resetButton" class="button">重置</button></div>
</div><script>
var timer;
var hours = 0;
var minutes = 0;
var seconds = 0;function startTimer() {timer = setInterval(updateTimer, 1000);
}function pauseTimer() {clearInterval(timer);
}function resetTimer() {clearInterval(timer);hours = 0;minutes = 0;seconds = 0;updateDisplay();
}function updateTimer() {seconds++;if (seconds == 60) {seconds = 0;minutes++;}if (minutes == 60) {minutes = 0;hours++;}updateDisplay();updateClock();
}function updateDisplay() {var displayHours = (hours < 10) ? "0" + hours : hours;var displayMinutes = (minutes < 10) ? "0" + minutes : minutes;var displaySeconds = (seconds < 10) ? "0" + seconds : seconds;document.getElementById("timerDisplay").innerText = displayHours + ":" + displayMinutes + ":" + displaySeconds;
}function updateClock() {var hourHand = document.querySelector(".hour-hand");var minuteHand = document.querySelector(".minute-hand");var secondHand = document.querySelector(".second-hand");var hourRotation = (hours % 12) * 30 + minutes * 0.5;var minuteRotation = minutes * 6 + seconds * 0.1;var secondRotation = seconds * 6;hourHand.style.transform = `translate(-1px, -100%) rotate(${hourRotation}deg)`;minuteHand.style.transform = `translate(-1px, -100%) rotate(${minuteRotation}deg)`;secondHand.style.transform = `translate(-1px, -100%) rotate(${secondRotation}deg)`;
}document.getElementById("startButton").addEventListener("click", startTimer);
document.getElementById("pauseButton").addEventListener("click", pauseTimer);
document.getElementById("resetButton").addEventListener("click", resetTimer);
</script></body>
</html>

案例效果图

这篇关于使用JavaScript及HTML、CSS完成秒表计时器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/928009

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(