<div id="app" class="container"></div>
 @import 'https://fonts.googleapis.com/css?family=Share+Tech+Mono';

@import 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css';

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
    width: 100vw;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/1376484/jess-harding-lqT6NAmTaiY-unsplash.jpg'),radial-gradient(#28a3dd, #0d7751);
    background-size: cover;
    background-position: center;
    font-family: "Share Tech Mono", monospace;
    font-size: 2rem;
    background-blend-mode: multiply,screen, overlay;
}


#app {
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  // height: 100vh;
  background: #fff;
  border-radius: 5px;
  padding: 2vh;
  flex-direction: column;
}

.house {
  position: relative;
  height: 380px;
  width: 400px;
  margin: 3vh;
}

img {
  position: absolute;
}

.roof {
  left: 0;
  top: 0;
  border-width: 0;
}

.chimney {
  left: 255px;
}

.wall {
  left: 5px;
  top: 48px;
}

.window {
  left: 200px;
  top: 182px;
}

.door {
  left: -25px;
  top: 182px;
  cursor: pointer;
}

p {
  font-size: 2vh;
  color: #ccc;
}
// 创建房顶组件

const Roof = ({color, roof}) => {
  return <img src={roof} className="roof" style={{background: `${color}`}} />
}

// 创建墙
const Wall = ({wall}) => {
  return <img src={wall} className='wall' />;
}

// 创建窗户
const Window = ({window}) => {
  return <img src={window} className='window' />;
}

// 创建门
const Door = ({doorOpen, doorClose, isOpen, onClick}) => {
  return <>
    { isOpen ? (<img src={ doorOpen } onClick={onClick} className='door' />) : (<img src={ doorClose } className='door' onClick={onClick} />)}
    </>;
}

// 创建房子

class House extends React.Component{
  static defaultProps = {
    color: 'blue',
    roof: 'https://cdn.glitch.com/1fb3273a-81cb-45bc-acbd-555546cb098f%2Froof.png?1515785259159',
    wall: 'https://cdn.glitch.com/1fb3273a-81cb-45bc-acbd-555546cb098f%2Fwall.png?1501113882297',
    window: 'https://cdn.glitch.com/1fb3273a-81cb-45bc-acbd-555546cb098f%2Fwindow.png?1501113882112',
    doorClose: 'https://cdn.glitch.com/1fb3273a-81cb-45bc-acbd-555546cb098f%2Fdoor_close.png?1501113881433',
    doorOpen: 'https://cdn.glitch.com/1fb3273a-81cb-45bc-acbd-555546cb098f%2Fdoor_open.png?1501113882121'
  }
                                    
  constructor (props) {
    super(props)
    this.state = {
      isOpen: false
    }
    this.handleClick = this.handleClick.bind(this)
  }
  
  handleClick = () =>{
    console.log(this.state.isOpen)
    this.setState({
      isOpen: !this.state.isOpen
    })
    console.log(this.state.isOpen)
  }
  
  render() {
    return (
      <>
        <div className='house'>      
          <Roof color={this.props.color} roof={this.props.roof } />
          <Wall wall={this.props.wall} />
          <Window window={this.props.window} />
          <Door doorOpen={this.props.doorOpen} doorClose={this.props.doorClose} isOpen={this.state.isOpen} onClick={this.handleClick} />
        </div>
        <p>请用鼠标点击房门,开门和关门之间会切换</p>
      </>  
    )
  } 
}

ReactDOM.render(<House />, document.getElementById('app'));
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.10.2/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.10.2/umd/react-dom.production.min.js