網站首頁 編程語言 正文
本文實例為大家分享了React實現pc端彈出框效果的具體代碼,供大家參考,具體內容如下
最近學習react碰見了一個小坑 不知道為什么 我在做一個彈出框的小demo
很簡單的一個小demo 就是桌面上一個按鈕點擊 出現一個彈出框 彈出框下面有一個遮罩層
1.我們現在src文件夾 下建立一個 Dialog 組件
import React,{Component} from 'react'?
import '../dialog.css'
export default class Dialog extends Component {
? ? constructor(props){
? ? ? ?super(props);
? ? ? ?this.state={}
? ? }
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <div className="mask" style={{display:this.props.display}}>
? ? ? ? ? ? ? ? <div className="content">
? ? ? ? ? ? ? ? ? ? <button onClick={this.props.hide}>×</button>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </div>
? ? ? ? );
? ? }
}
2.然后就是css樣式
.mask{
? ? width: 100%;
? ? height: 100%;
? ? position: fixed;
? ? left: 0;
? ? right: 0;
? ? background-color: #000;
? ? opacity: 0.4;
? ? color:#f00;
}
.content{
? ? position: fixed;
? ? height: 300px;
? ? width: 300px;
? ? left: 50%;
? ? top:50%;
? ? background-color: #fff;
? ? transform: translate(-50%,-50%);
}
3.再然后就是index.js的入口文件
import ?React,{Component } from 'react'
import ReactDOM from 'react-dom'
import Dialog from './components/Dailog';
import './index.css'
class Parent extends Component {
? ? constructor(props){
? ? ? ? super(props);
? ? ? ? this.state={display:'block'};
? ? ? ? this.tan=this.tan.bind(this);
? ? ? ? this.hide=this.hide.bind(this);
? ? }
? ? tan(){
? ? ? ? console.log(this);
? ? ? ? this.setState({display:'block'})
? ? }
? ? hide(){
? ? ? ? this.setState({display:'none'})
? ? }
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <div>
? ? ? ? ? ? ? ?// 就是這里 不知道為什么我一把組件放到按鈕下面 ?遮罩層 就不會覆蓋掉按鈕 很奇怪
? ? ? ? ? ? ? ? <Dialog display={this.state.display} hide={this.hide} />
? ? ? ? ? ? ? ? <button onClick={this.tan}>彈出</button>
? ? ? ? ? ? </div>
? ? ? ? );
? ? }
}
ReactDOM.render(<div><Parent /></div>,document.getElementById('root'))
在react中 子類調用父類的方法 是父類在子組件上面 綁定 方法然后在子組件中調用
<Dialog display={this.state.display} hide={this.hide} /> ?// 父類 通過props傳遞過去
?<button onClick={this.props.hide}>×</button> ? // 子類調用
原文鏈接:https://blog.csdn.net/yunchong_zhao/article/details/104462911
相關推薦
- 2023-09-12 git常用指令
- 2023-03-01 pyinstaller打包后偶爾出現黑窗口一閃而過的問題及解決_python
- 2022-07-15 go學習筆記讀取consul配置文件詳解_Golang
- 2022-10-01 終于搞懂了Python中super(XXXX,?self).__init__()的作用了_pytho
- 2022-03-24 一篇文章教你如何用C語言實現strcpy和strlen_C 語言
- 2023-05-22 Redis數據結構原理淺析_Redis
- 2022-08-23 React?中的列表渲染要加?key的原因分析_React
- 2022-06-08 nacos項目啟動報錯:Connection refused: no further informa
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支