日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

React實現pc端的彈出框效果_React

作者:yunchong_zhao ? 更新時間: 2022-10-22 編程語言

本文實例為大家分享了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}>&times;</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}>&times;</button> ? // 子類調用

原文鏈接:https://blog.csdn.net/yunchong_zhao/article/details/104462911

欄目分類
最近更新