網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了React組件編寫溫度顯示器的具體代碼,供大家參考,具體內(nèi)容如下
這是模擬了一下溫度顯示器的效果,先看效果:
先在頁面中引入React等;
import React from "react";
import ReactDOM from "react-dom";
import "./index.css"; // 頁面的樣式文件
開發(fā)過程是這樣的:
首先定義一個BoillingVerdict組件,用來顯示溫度顯示器的(樣式先不寫,在后面一起上代碼)(溫度顯示器上的數(shù)字不是溫度;),
// 顯示溫度計(jì)算器文字的函數(shù)組件(最高300攝氏度)
function BoillingVerdict(props) {
? return (
? ? <div className="outer">
? ? ? <div className="inner" style={{ height: props.height + "%", background: props.bg, }} >
? ? ? ? {props.height}
? ? ? </div>
? ? </div>
? );
}
然后,創(chuàng)建一個名為 Calculator 的組件,用于渲染 ?'控制溫度的輸入框' 和 溫度顯示器組件,
class Calculator extends React.Component {
? // 構(gòu)造函數(shù),可以用于初始化state
? constructor(props) {
? ? super(props);
? ? this.state = {
? ? ? temperature: 0, // 溫度
? ? ? tempHeight: 0, // 溫度顯示器背景色高度
? ? ? bg: "#fff", // 溫度顯示器顏色
? ? };
? ? // 為tempChange方法綁定this,否則該方法中拿不到this
? ? this.tempChange = this.tempChange.bind(this);
? }
? tempChange(e) {?
? ?// 判斷溫度值大小來設(shè)置顏色
? ? var colors =
? ? ? Number(e.target.value) > 90 ? "#0F1CED" : Number(e.target.value) > 80 ? "#D5D70B" :
? ? ? Number(e.target.value) > 70 ? "#0BD737" : Number(e.target.value) > 60 ? "#0BD7CA" :
? ? ? Number(e.target.value) > 50 ? "#ED194B" : Number(e.target.value) > 40 ? "#AE1FD2" :
? ? ? Number(e.target.value) > 30 ? "skyblue" : Number(e.target.value) > 20 ? "blue" :
? ? ? Number(e.target.value) > 10 ? "orange" : "#671552";
? ? var height = (Number(e.target.value) / 3).toFixed(2);
? ? this.setState({
? ? ? temperature: e.target.value,
? ? ? tempHeight: height,
? ? ? bg: colors,
? ? });
? }
? render() {
? ? return (
? ? ? <fieldset>
? ? ? ? <legend> 溫度: </legend>
? ? ? ? <input value={this.state.temperature} onChange={this.tempChange} type="number" > </input>
? ? ? ? <BoillingVerdict height={this.state.tempHeight} bg={this.state.bg}></BoillingVerdict>
? ? ? </fieldset>
? ? );
? }
}
然后渲染:
ReactDOM.render(<Calculator></Calculator>,document.getElementById("root12"));
index.css
.outer{
? width:80px;
? height:300px;
? border:1px solid black;
? border-radius: 20px;
? overflow: hidden;
? background:#fff;
? margin-top:10px;
? position:relative;
? text-align: center;
}
.inner{
? position:absolute;
? bottom:0;height:200px;
? background:#fff;
? width:100%;
? background:blue;
? text-align: center;
? transition: all 1s;
}
原文鏈接:https://blog.csdn.net/yinge0508/article/details/114142078
相關(guān)推薦
- 2022-09-26 Redis?哈希Hash底層數(shù)據(jù)結(jié)構(gòu)詳解_Redis
- 2023-06-04 Pandas中MultiIndex選擇并提取任何行和列_python
- 2022-09-03 解決Python3錯誤:SyntaxError:?unexpected?EOF?while?pars
- 2022-06-29 python鏈表的基礎(chǔ)概念和基礎(chǔ)用法詳解_python
- 2022-06-20 python中的隨機(jī)數(shù)?Random介紹_python
- 2022-07-18 Redis服務(wù)器連接本地Linux所踩的坑
- 2023-05-16 Android?ActivityManagerService啟動流程詳解_Android
- 2022-11-27 網(wǎng)站https訪問是443端口還是433端口_服務(wù)器其它
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支