前端学习之DOM编程案例:点名案例和秒表案例

2024-04-19 17:44

本文主要是介绍前端学习之DOM编程案例:点名案例和秒表案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

点名

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>点名案例</title><style>*{margin: 0;padding: 0;}</style>
</head>
<body><div id="container"></div><script>let div0 = document.querySelector('#container')//最外边的大盒子div0.style.width = 800 +'px'div0.style.height = 900 +'px'div0.style.border = '1px dashed red'        div0.style.margin = 'auto'div0.style.textAlign = 'center'//抽奖显示的圆let cirle =  document.createElement('div')cirle.style.borderRadius = '50%'// cirle['borderRadius'] = '50%'// cirle['backgroundColor'] = 'red'cirle.style.backgroundColor = 'red'cirle.style.width = 300 + 'px'cirle.style.height = 300 + 'px'cirle.style.margin = 'auto'cirle.textContent = '点名系统'cirle.style.fontSize = '30px'cirle.style.color = 'white'cirle.style.marginTop = '80px'cirle.style.lineHeight = '300px'cirle.style.textAlign='center'div0.appendChild(cirle)//抽奖按钮let button1 = document.createElement('button')button1.style.width=300+'px'button1.style.height=40+'px'button1.textContent = '开始点名'button1.style.margin='auto'button1.style.marginTop = 40+'px'let btn_status = '开始点名'//奖品let arr = ['张三','李四','王五','周末']    //抽奖逻辑button1.onclick = function(){if (btn_status == '开始点名'){id = setInterval(start,10)}else{btn_status = '开始点名'button1.textContent = btn_statusclearInterval(id)} }div0.appendChild(button1)function start(){index = Math.ceil(Math.random()*4)cirle.textContent = arr[index]btn_status = '停止点名'button1.textContent = btn_status}</script>
</body>
</html>

结果

秒表

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>定时器</title><style>*{margin: 0;padding: 0;}.div0{background-color: aqua;width: 400px;height: 500px;border: 1px dashed red;margin: auto;text-align: center;}.cirle{border-radius: 50%;background-color: red;width: 300px;height: 300px;margin: auto;display: grid;grid-template-columns: repeat(3,1fr);}button{margin-top: 20px;width: 100px;height: 40px;margin: auto;margin-top: 40px;}#time{width: 70px;height: 70px;/* background-color:black; *//* float: left; */margin: auto;/* margin-top: 100px; *//* padding: 10px; */}#time{line-height: 40px;font-size: 70px;}</style>
</head>
<body><!-- 最外边蓝色大边框 --><div class="div0"><!-- 红色圆 --><div class="cirle"><!-- 秒数 --><div id="time" class="time1">0</div><div id="time" class="time0">:</div><div id="time" class="time2">0</div></div><button class="button1">开始计时</button><button class="button2">停止计时</button><button class="button3">重置</button></div><script>let time2= 0let div1 = document.querySelector('.cirle')let t1 = document.querySelector('.time1')let t2 = document.querySelector('.time2')time1 = 0let button1 = document.querySelector('.button1')let button2 = document.querySelector('.button2')let button3 = document.querySelector('.button3')button1.onclick = function(){id = setInterval(time,1000)}// 停止button2.onclick = function(){clearInterval(id)}function time(){time1 = time1+1t2.textContent = time1// 当秒数够一分钟,分针数加1if(time1%60==0){time2 = time2+1t1.textContent = time2}}// 重置button3.onclick = function(){time1 = 0time2 = 0t1.textContent = time2t2.textContent = time1clearInterval(id)}</script>
</body>
</html>

结果

 


不嫌弃的点点关注,点点赞 ଘ(੭ˊᵕˋ)੭* ੈ✩‧

这篇关于前端学习之DOM编程案例:点名案例和秒表案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动