React實現pc端的彈出框效果
更新時間:2022年08月25日 15:53:05 作者:yunchong_zhao
這篇文章主要為大家詳細介紹了React實現pc端的彈出框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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> ? // 子類調用以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
react項目中使用react-dnd實現列表的拖拽排序功能
這篇文章主要介紹了react項目中使用react-dnd實現列表的拖拽排序,本文結合實例代碼講解react-dnd是如何實現,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02

