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

很簡單的一個小demo 就是桌面上一個按鈕點(diǎn)擊 出現(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用 React 和 Threejs 創(chuàng)建一個VR全景項(xiàng)目的過程詳解
這篇文章主要介紹了使用 React 和 Threejs 創(chuàng)建一個VR全景項(xiàng)目的過程詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
JS中使用react-tooltip插件實(shí)現(xiàn)鼠標(biāo)懸浮顯示框
前段時間遇到的一個需求,要求鼠標(biāo)懸停顯示使用描述, 用到了react-tooltip插件,今天寫一個總結(jié),感興趣的朋友跟隨小編一起看看吧2019-05-05
淺談React-router v6 實(shí)現(xiàn)登錄驗(yàn)證流程
本文主要介紹了React-router v6 實(shí)現(xiàn)登錄驗(yàn)證流程,主要介紹了公共頁面、受保護(hù)頁面和登錄頁面,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
再次談?wù)揜eact.js實(shí)現(xiàn)原生js拖拽效果引起的一系列問題
React 起源于 Facebook 的內(nèi)部項(xiàng)目,因?yàn)樵摴緦κ袌錾纤?JavaScript MVC 框架,都不滿意,就決定自己寫一套,用來架設(shè) Instagram 的網(wǎng)站.本文給大家介紹React.js實(shí)現(xiàn)原生js拖拽效果,需要的朋友一起學(xué)習(xí)吧2016-04-04

