【问题标题】:react-modal Dynamic Sizingreact-modal 动态调整大小
【发布时间】:2019-03-25 14:54:45
【问题描述】:

我正在使用react-modal,这非常棒。是否可以动态调整它的大小(可能使用 css 媒体标签)。例如,

  1. 对于大屏幕,模态仅占屏幕的一小部分(假设最大宽度为 200 像素;
  2. 对于中等屏幕,模态占据了大部分屏幕(假设是屏幕宽度和高度的 80%
  3. 对于移动设备,它占据了 100% 的宽度和高度。

【问题讨论】:

    标签: javascript css reactjs react-modal


    【解决方案1】:

    看看这个为你准备的代码。

    ReactModal.setAppElement('#main');
    
    class ExampleApp extends React.Component {
      constructor () {
        super();
        this.state = {
          showModal: false
        };
    
        this.handleOpenModal = this.handleOpenModal.bind(this);
        this.handleCloseModal = this.handleCloseModal.bind(this);
      }
    
      handleOpenModal () {
        this.setState({ showModal: true });
      }
    
      handleCloseModal () {
        this.setState({ showModal: false });
      }
    
      render () {
        return (
          <div>
            <button onClick={this.handleOpenModal}>Trigger Modal</button>
            <ReactModal 
               isOpen={this.state.showModal}
               contentLabel="onRequestClose Example"
               onRequestClose={this.handleCloseModal}
               className="Modal"
               overlayClassName="Overlay"
            >
              <p>Modal text!</p>
              <button onClick={this.handleCloseModal}>Close Modal</button>
            </ReactModal>
          </div>
        );
      }
    }
    
    const props = {};
    
    ReactDOM.render(<ExampleApp {...props} />, document.getElementById('main'))
    

    在响应式视图中结帐:

    https://codepen.io/ene_salinas/full/yRGMpG/

    查看完整代码:

    https://codepen.io/ene_salinas/pen/yRGMpG

    Yellow color = large screen
    Green color = medium screen
    Gray color = Mobile
    

    不要忘记这个元数据:

    "<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">"
    

    编码愉快。

    【讨论】:

    • @user172902 我有什么可以帮助你的吗?,你看我的回答了吗?请给我一个反馈..
    • 你能把你的代码笔的相关部分放在答案中吗?当 codepen 停止工作时,这个答案不会给出解决方案。
    【解决方案2】:

    我还在我的一个项目中使用该库,我为模态提出了以下样式规则:

    .ReactModal__Overlay {
        z-index: 99;
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        background-color: rgba(255,255,255,0.75);
    }
    
    .ReactModal__Content {
        position: absolute;
        left: 2.5rem;
        right: 2.5rem;
        top: 2.5rem;
        bottom: 2.5rem;
        background-color: #fff;
        box-shadow: 0 0 10px 0 rgba(0,0,97,0.5);
        overflow: auto;
        border-radius: 4px;
        outline: none;
    }
    

    为了获得理想的结果,您需要调整绝对定位,所以:

    1. large screen, modal max-width 200px
    @media screen and (min-width: 900px) {
        max-width: 200px;
        left: calc(50% - 100px);
    }
    
    2. medium screen, modal about 80% screen width
    @media screen and (min-width: 475px) and (max-width: 900px) {
        left: 10%;
        right: 10%;
        top: 10%;
        bottom: 10%;
    }
    
    3. mobile screen, modal full width
    @media screen and (max-width: 475px) {
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
    }
    

    【讨论】:

    • 看来我所要做的就是添加您的三个媒体 css 样式并将它们包装在 .ReactModal__Content 类中。但是,我发现自己需要为每个元素添加 !important 才能生效。你知道为什么吗?
    • 那是因为有另一个选择器比您的样式规则更具体。也许你可以通过添加多个类名(也属于父类)或指定元素类型(div.ReactModal__Overlay div.ReactModal__Content)来使你的更具体。但是,如果其他样式是内联样式,则必须将它们移至 CSS 或在每个 CSS 规则上使用 !important,因为内联样式具有最高优先级。
    【解决方案3】:

    由于IE 支持,最好不要使用calc

    ..ReactModal__Overlay {
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        background-color: rgba(0,0,0,0.7);
        z-index: 999;
    }
    
    // this is better to use transform for center the modal and width 
    .ReactModal__Content {
        position: absolute; // if it's inside your ReactModal__Overlay if not use Fixed and a higher z-index.
        width: 100%;
        height: 100%;
        left: 50%;
        top: 50%;
        overflow: auto;
        background-color: #fff;
        -webkit-transform: translate(-50,-50%);
        transform: translate(-50,-50%);
    }
    
    @media screen and (min-width: 992px) { // desktop
        .modal.modal-content {
            max-width: 200px;
            max-height: 80%;
        }
    }
    
    @media screen and (min-width: 426px) and (max-width: 991) { // tablet size
        .modal.modal-content {
            max-width: 80%;
            max-height: 80%;
        }
    }
    
    @media screen and (max-width: 425px) { // mobile size
        .modal.modal-content {
            max-width: 100%;
            max-height: 100%;
        }
    }
    

    【讨论】:

    • IE11 支持 calc,其他的都已经微软不维护了。
    【解决方案4】:

    class Modal extends React.Component {
    
      state = {
        showModal: false
      };
    
      handleOpenModal = () => {
        this.setState({ showModal: true });
      };
    
      handleCloseModal = () => {
        this.setState({ showModal: false });
      };
      
      componentDidMount() {
        ReactModal.setAppElement('#root');
      }
    
      render() {
        return (
          <div>
            <button onClick={this.handleOpenModal}>Trigger Modal</button>
            <ReactModal
              isOpen={this.state.showModal}
              className="Modal"
              overlayClassName="Overlay"
            >
              <p>Here goes your content!</p>
              <button onClick={this.handleCloseModal}>Close Modal</button>
            </ReactModal>
          </div>
        );
      }
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<Modal />, rootElement);
    .Modal {
      height: 100%;
      width: 100%;
    
      background-color: #ffffff;
      overflow: auto;
    }
    
    .Overlay {
      position: fixed;
    
      top: 0;
      bottom: 0;
      right: 0;
      left: 0;
    
      display: flex;
      justify-content: center;
      align-items: center;
    
      background-color: rgba(0, 0, 0, 0.4);
    }
    
    @media (min-width: 768px) and (max-width: 1279px) {
      .Modal {
        width: 80%;
        height: 80%;
      }
    }
    
    @media (min-width: 1280px) {
      .Modal {
        height: auto;
        width: auto;
    
        max-width: 500px;
        max-height: 80%;
      }
    }
    <div id="root" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-modal/3.6.1/react-modal.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 2022-01-25
      • 1970-01-01
      • 2020-07-09
      • 2012-10-24
      • 2014-03-19
      • 2015-11-02
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多