【问题标题】:React-modal hides behind elementsReact-modal 隐藏在元素后面
【发布时间】:2018-01-16 17:09:46
【问题描述】:

我第一次尝试使用 react-modal。当我点击登录按钮时,react-modal 组件被调用,但似乎隐藏在作为视频登录页面的封面页后面。

React 开发工具在单击登录按钮之前显示适当的状态 before the sign-in button is clicked

现在单击登录按钮时,react devtool 现在显示 ModalPortal 组件已呈现并显示适当的状态 when the sign-in button is clicked

SignInModal.scss

.ReactModalPortal>div {
    opacity: 0;
}

.ReactModalPortal .ReactModal__Overlay {
    align-items: center;
    display: flex;
    justify-content: center;
    transition: opacity 200ms ease-in-out;
}

.ReactModalPortal .ReactModal__Overlay--after-open {
    opacity: 1;
}

.ReactModalPortal .ReactModal__Overlay--before-close {
    opacity: 0;
}

.modal {
    position: relative;
    background: #464b5e;
    color: white;
    max-width: 90rem;
    outline: none;
    padding: 3.2rem;
    text-align: center;
}

.modal__title {
    margin: 0 0 1.6rem 0;
}

.modal__body {
    font-size: 2rem;
    font-weight: 300;
    margin: 0 0 3.2rem 0;
    word-break: break-all;
}

CoverPage.js 组件

import Header from './Header';
import HeaderVideo from './HeaderVideo';
import SignInModal from './SignInModal';

import React, { Component } from 'react';

class CoverPage extends Component {
  state = {
    modalIsOpen: false
  };

  onOpenModal = () => {
    this.setState(() => ({
      modalIsOpen: true
    }));
  };

  onCloseModal = () => {
    this.setState(() => ({
      modalIsOpen: false
    }));
  };
  render() {
    return (
      <div>
        <Header />
        <HeaderVideo onOpenModal={this.onOpenModal} />
        <SignInModal
          modalIsOpen={this.state.modalIsOpen}
          onOpenModal={this.onOpenModal}
          onCloseModal={this.onCloseModal}
        />
      </div>
    );
  }
}

export default CoverPage;

HeaderVideo.js 组件

import React from 'react';
import Signup from './Signup';
import CoverInfo from './CoverInfo';

const HeaderVideo = props => {
  return (
    <div className="video-container">
      <video preload="true" autoPlay loop volume="0" postoer="/images/1.jpg">
        <source src="images/vine.mp4" type="video/mp4" />
        <source src="images/vine1.webm" type="video/webm" />
      </video>
      <div className="video-content">
        <div className="container content">
          <div className="row">
            <div className="col-md-9">
              <CoverInfo onOpenModal={props.onOpenModal} />
            </div>
            <div className="col-md-3">
              <Signup />
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default HeaderVideo;
CoverInfo.js 组件

import React from 'react';

const CoverInfo = props => {
  return (
    <div className="info">
      <div>
        <h1>Welcome to EventCity!</h1>
      </div>
      <div>
        <p>
          At EventCity! we pride ourselves on the unrivalled personal {`event`} services,we provide
          to our clientele. We guide you from the stressful decision making {`process`},ensuring you
          are comfortable,whether it is a wedding, corporate {`function `}or even a kiddies party,we
          create a buzz around you, taking you to the next level.
        </p>
      </div>
      <div>
        <h3>Innovation, {`Performance`} and Delivery</h3>
      </div>
      <button type="button" className="btn btn-success btn-lg" onClick={props.onOpenModal}>
        Sign In here
      </button>
    </div>
  );
};

export default CoverInfo;

video-cover.scss

video {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    z-index: 1;
}

.video-content {
    z-index: 2;
    position: absolute;
    background: rgba(0, 0, 0, 0.6);
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

.content {
    padding-top: 120px;
}

【问题讨论】:

  • 这不只是一个css问题吗?你不能修改你的模态的z-index吗?
  • @Axnyff 你是对的,但我仍然不知道该怎么做。我试过改变模态的z-index,但还是不行。

标签: reactjs modal-dialog


【解决方案1】:

您需要在 Modal 的叠加层上设置 z-index 属性,通常 z-index 为 0。CSS 类是 .ReactModal__Overlay

这是纯 React 的做法:

const customStyles = {
  content : {
    ...
  },
  overlay: {zIndex: 1000}
};

<Modal style={customStyles}>
  ...
</Modal>

【讨论】:

  • 哇,我用那个挣扎了太久了,谢谢!
  • 谢谢!我有ayudado mucho!
  • 不错的一个!谢谢
【解决方案2】:
.modal {
    position: fixed;
    z-index:9999;
    top :0;
    left:0;
    right:0;
    bottom:0;
    background: #464b5e;
    color: white;
    outline: none;
    padding: 3.2rem;
    text-align: center;
}

【讨论】:

  • 考虑 editing 你的答案来解释为什么这段代码有效。
  • 我会记住这一点的。顺便感谢@Tom Aranda 的地址。
  • 之所以可行,是因为覆盖层需要更高的z-index,而且还因为通过模态框的overlayClassName 属性向覆盖层添加类名会覆盖默认样式,而这些属性中的大部分都是模态工作。
【解决方案3】:

Example of react-modal inline styles 在 react-modal 内联样式中设置样式。 z-index 为 100 但如下所示

style={{
  overlay: {
  zIndex: 100,
  backgroundColor: 'rgba(70, 70, 70, 0.5)',
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多