display a overlay when input is clicked in react

前端 未结 2 1509
[愿得一人]
[愿得一人] 2020-12-31 21:37

I\'m trying to display a overlay when a certain Input field is clicked. I\'m doing this in react. How can I do this?

This is my code



        
2条回答
  •  春和景丽
    2020-12-31 22:08

    I have created a sample react component. I hope this will help you in somewhat way to achieve what you want.

    class Test extends React.Component {
    
        constructor(props) {
            super(props);
            this.state = {
                style : {
                    width : 350
                }
            };
            this.openNav = this.openNav.bind(this);
            this.closeNav = this.closeNav.bind(this);
        }
    
        componentDidMount() {
            document.addEventListener("click", this.closeNav);
        }
    
        componentWillUnmount() {
            document.removeEventListener("click", this.closeNav);
        }
    
        openNav() {
            const style = { width : 350 };
            this.setState({ style });
            document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
            document.addEventListener("click", this.closeNav);
        }
    
        closeNav() {
            document.removeEventListener("click", this.closeNav);
            const style = { width : 0 };
            this.setState({ style });
            document.body.style.backgroundColor = "#F3F3F3";
        }
    
        render() {
            return (
              

    Fullscreen Overlay Nav Example

    Click on the element below to open the fullscreen overlay navigation menu.

    In this example, the navigation menu will slide in, from left to right:

    ☰ open

    Form

    This is a sample input form

    ×
    {/*your form component goes here */} {this.props.children}
    ); } } ReactDOM.render( , document.getElementById('test') );
    .overlay {
        height: 100%;
        width: 0;
        position: fixed;
        z-index: 1;
        top: 0;
        left: 0;
        background-color: rgb(0,0,0);
        background-color: rgba(0,0,0, 0.9);
        overflow-x: hidden;
        transition: 0.5s;
    }
    
    .overlay-content {
        position: relative;
        top: 25%;
        width: 100%;
        text-align: center;
        margin-top: 30px;
    }
    
    .overlay a {
        padding: 8px;
        text-decoration: none;
        font-size: 36px;
        color: #818181;
        display: block;
        transition: 0.3s;
    }
    
    .overlay a:hover, .overlay a:focus {
        color: #f1f1f1;
    }
    
    .overlay .closebtn {
        position: absolute;
        top: 20px;
        right: 45px;
        font-size: 60px;
    }
    
    @media screen and (max-height: 450px) {
      .overlay a {font-size: 20px}
      .overlay .closebtn {
        font-size: 40px;
        top: 15px;
        right: 35px;
      }
    }
    
    .overlay h2, .overlay p {
      color:white;
      }
    
    
    
    
    

提交回复
热议问题