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

【Html】交通灯问题

效果

在这里插入图片描述

实现方式

  • 计时器:setTimeoutsetInterval来计时。setIntervalsetTimeout 在某些情况下可能会出现计时不准确的情况。这通常是由于JavaScript的事件循环机制和其他代码执行所需的时间造成的。
  • 问询:通过getCurrentLight将每个状态的持续时间设置为精确的值,并使用requestAnimationFrame来递归调用getCurrentLight函数,我们可以更准确地控制交通灯的状态。

源码

index.html

<!DOCTYPE html>
<html><head><title>交通灯</title><link type="text/css" rel="styleSheet" href="./index.css" /></head><body><div class="traffic-light"><div class="traffic-container"><div class="light green"></div><div class="light yellow"></div><div class="light red"></div></div><div class="time">90</div></div><script type="module">import {TrafficLight} from './TrafficLight.js';const time = document.querySelector('.time');const trafficDom = document.querySelector('.traffic-light');const light = new TrafficLight({red:3,yellow:2,green:5,initial:'red',});function raf(){requestAnimationFrame(()=>{raf();const current = light.getCurrentLight();time.textContent =current.remain;trafficDom.className = `traffic-light ${current.color}`;console.log(current.color,current.remain);})}raf();</script></body>
</html>

index.css

* {margin: 0;padding: 0;box-sizing: border-box;
}body{width: 100vw;height: 100vh;margin: 0;/* backgroud: #191c29 */background: #fff;
}.traffic-light{width: 200px;margin: 10px auto;-webkit-box-flex: inherit;text-align: center;
}.light{width: 20px;height: 20px;border-radius: 10px;display:inline-block;background-color: gray;margin: 10px auto;
}.red .red{background-color: red;
}.green .green{background-color: green;
}
.yellow .yellow{background-color: yellow;
}.time{font-family: 'DS-Digital';font-size: 40px;
}

TrafficLight.js

export class TrafficLight {constructor(options) {const {red = 60,green = 60,yellow = 3,initial = 'green',} = options || {};this._colors ={red:{seconds: red,next:'yellow',},green:{seconds: green,next:'yellow',},yellow:{seconds : yellow,},};this._switch(initial);}_switch(color){this._currentColor = color;this._seconds = this._colors[color].seconds;this._time = Date.now();}_next(){if(this._currentColor === 'red'){this._colors.yellow.next = 'green';}  else if(this._currentColor === 'green'){this._colors.yellow.next = 'red';} else{}this._switch(this._colors[this._currentColor].next);
}getCurrentLight(){const remain = Math.ceil(this._seconds -(Date.now() - this._time)/1000);if(remain<=0){this._next();return this.getCurrentLight();}return {color: this._currentColor,remain,};}}

字体 DS-Digital

下载字体 DS-Digital
注意:下载安装字体后需要重启浏览器才生效

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

相关文章:

  • 用IntelliJ远程打断点调试
  • Spring-Bean的生命周期概述
  • SENet 学习
  • 目前和未来的缓存构建
  • aws亚马逊云免费账号代充值!!!什么是 AWS Lambda?
  • 《从零开始大模型开发与微调 :基于PyTorch与ChatGLM》简介
  • 【LeetCode】102. 二叉树的层序遍历
  • golang连接池检查连接失败时如何重试
  • 从JavaScript到Rust的三年时间小结
  • 【Python机器学习】零基础掌握VotingRegressor集成学习
  • 云计算模式的区域LIS系统源码,基于ASP.NET+JQuery、EasyUI+MVC技术架构开发
  • 面向对象设计原则之接口隔离原则
  • haproxy 负载均衡
  • 在el-dialog中使用tinymce 点击工具栏下拉框被遮挡
  • CloudQuery + StarRocks:打造高效、安全的数据库管控新模式
  • 各类统计模型R语言的详细使用教程-R语言的线性回归使用教程
  • 点云从入门到精通技术详解100篇-基于尺度统一的三维激光点云与高清影像配准
  • <蓝桥杯软件赛>零基础备赛20周--第2周
  • CMake多文件构建初步
  • 游戏研发的解决方案有哪些?
  • Bayes决策:身高与体重特征进行性别分类
  • 【考研数学】数学“背诵”手册 | 需要记忆且容易遗忘的知识点
  • HJ3 明明的随机数
  • 如何恢复u盘删除文件?2023最新分享四种方法恢复文件
  • 8.稳定性专题
  • 基于51单片机的四种波形信号发生器仿真设计(仿真+程序源码+设计说明书+讲解视频)
  • 不同网段的IP怎么互通
  • C#序列化与反序列化详解
  • 如何在k8s的Java服务镜像(Linux)中设置中文字体
  • CT 扫描的 3D 图像分类-预测肺炎的存在