简体   繁体   English

React-responsive-modal:打开模态时更改背景颜色

[英]React-responsive-modal: Change background-color when modal is open

I use react-responsive-modal to open some modals within my react app.我使用react-responsive-modal在我的反应应用程序中打开一些模态。 When i open the modal, there is an overlay effect that darkens the background behind the modal.当我打开模态时,有一个叠加效果使模态后面的背景变暗。 Is there any way to darken the background for 100% or set any color for the background so i cant see the stuff which was there before the modal was opened until i close the modal again?有没有办法使背景变暗 100% 或为背景设置任何颜色,这样我就看不到打开模态之前存在的东西,直到我再次关闭模态?

I created a new component for the modal ModalComponent within my MainComponent , which gets rendered when i click a button:我在我的MainComponent为模态ModalComponent创建了一个新组件,当我单击按钮时会呈现它:

ModalComponent : ModalComponent

render() {
 return (
  <div className="childDiv">
    <Modal
      open={open}
      onClose={this.onCloseModal}
      center
      classNames={{
        transitionEnter: styles.transitionEnter,
        transitionEnterActive: styles.transitionEnterActive,
        transitionExit: styles.transitionExitActive,
        transitionExitActive: styles.transitionExitActive
      }}
      animationDuration={1000}
    >
   ...

MainComponent:主要组件:

<div>
    <div className="outter" onClick={this.openModal.bind(this)}>
//Open Modal when clicking on this div
      <p className="arrival">Ankunft am Ziel: ~ {this.props.arrival} Uhr</p>
      <p className="price">max. {this.props.price} €</p>
      {this.state.open && (
        <BookingModalNew
          open={this.state.open}
          triggerCloseModal={this.closeModal.bind(this)}
          destination={this.props.destination}
          arrival={this.props.arrival}
          price={this.props.price}
        />
      )}
//Whole Stuff should not be visible while Modal is opened

Just assign an object with your styles for the overlay to a variable say, bg inside your render and then just use the styles prop to reference that object in your Modal like this:只需将一个带有overlay样式的对象分配给一个变量,例如渲染中的bg ,然后只需使用styles道具在您的模态中引用该对象,如下所示:

render() {

 const bg = {
   overlay: {
     background: "#FFFF00"
   }
 };

 return (
  <div className="childDiv">
    <Modal open={open} onClose={this.onCloseModal} center styles={bg} }}>
        <p>Your Modal Content</p>
    </Modal>
  </div>
 )

} }


But wait .但是等等 Why create an extra object when we can just write the styles directly instead like this:当我们可以像这样直接编写样式时,为什么还要创建一个额外的对象:

<Modal open={open} onClose={this.onCloseModal} center styles={background: "#FFFF00"}>
    <p>Your Modal Content</p>
</Modal>

The above approach won't work even though it looks like it's doing the same thing as my code and that is because you can't directly specify styles inline on react-responsive-modal .即使上面的方法看起来与我的代码做同样的事情,也不会起作用,这是因为您不能直接在react-responsive-modal上指定内联样式。 You need to place the styles in an object first and then reference the styles prop to that object.您需要先将样式放置在一个对象中,然后将styles属性引用到该对象。


You can however create the object within the styles prop itself by doing this:但是,您可以通过执行以下操作在styles道具本身中创建对象:

<Modal open={open} onClose={this.onCloseModal} center styles={{ overlay: { background: "#FFFF00" } }}>
    <p>Your Modal Content</p>
</Modal>

But it is recommended that you define objects outside and then reference it inside the styles prop as shown above.但是建议您在外部定义对象,然后在styles属性内部引用它,如上所示。

You can change the CSS of overlay by styles props of Modal你可以通过Modal的styles props改变overlay的CSS

<Modal animationDuration={1000} styles={{ modal: {}, overlay: { background: "#ccc" } }} closeIconSize={16} open={modalOpen} onClose={this.onCloseModal} center > 
    Your Code Here...
</Modal>

看

Please see the full documentation of react-responsive-modal here请在此处查看 react-responsive-modal 的完整文档

You need to target and overwrite the overlay class, eg您需要定位并覆盖overlay类,例如

<Modal style={{ overlay: { background: 'black' } }} />

Another way of doing it is shown in the docs , eg 文档中显示了另一种方法,例如

<Modal classNames={{ overlay: { background: 'black' } }} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM