前端学习之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 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的