React實現(xiàn)pc端的彈出框效果
本文實例為大家分享了React實現(xiàn)pc端彈出框效果的具體代碼,供大家參考,具體內(nèi)容如下
最近學習react碰見了一個小坑 不知道為什么 我在做一個彈出框的小demo

很簡單的一個小demo 就是桌面上一個按鈕點擊 出現(xiàn)一個彈出框 彈出框下面有一個遮罩層
1.我們現(xiàn)在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中 子類調(diào)用父類的方法 是父類在子組件上面 綁定 方法然后在子組件中調(diào)用
<Dialog display={this.state.display} hide={this.hide} /> ?// 父類 通過props傳遞過去
?<button onClick={this.props.hide}>×</button> ? // 子類調(diào)用以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用React hook實現(xiàn)remember me功能
相信大家在使用 React 寫頁面的時候都遇到過完成 Remember me 的需求吧!本文就將這個需求封裝在一個 React hook 中以供后續(xù)的使用,覺得有用的同學可以收藏起來以備不時之需,感興趣的小伙伴跟著小編一起來看看吧2024-04-04
React實現(xiàn)組件之間狀態(tài)共享的幾種方法
在開發(fā)現(xiàn)代Web應用時,管理組件之間的狀態(tài)共享是一個重要的課題,特別是在使用React這個流行的前端庫時,如何有效地在不同組件之間傳遞狀態(tài),確保應用的響應性和可維護性,是我們需要掌握的關鍵技能,在本文中,我們將探討幾種有效的狀態(tài)共享策略,需要的朋友可以參考下2025-02-02
react項目中使用react-dnd實現(xiàn)列表的拖拽排序功能
這篇文章主要介紹了react項目中使用react-dnd實現(xiàn)列表的拖拽排序,本文結(jié)合實例代碼講解react-dnd是如何實現(xiàn),代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02

