首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反应模式动态调整大小

反应模式动态调整大小
EN

Stack Overflow用户
提问于 2018-10-21 17:46:50
回答 4查看 27K关注 0票数 8

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

  1. 对于大屏幕,模式只占屏幕的一小部分(假设最大宽度200px;
  2. 对于中等屏幕,模式占据大部分屏幕(假设80%的屏幕宽度和高度
  3. 对于移动设备,它占据100%的宽度和高度。
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-10-25 22:39:15

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

代码语言:javascript
复制
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

代码语言:javascript
复制
Yellow color = large screen
Green color = medium screen
Gray color = Mobile

不要忘记这个meta:

代码语言:javascript
复制
"<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">"

祝你编码愉快。

票数 2
EN

Stack Overflow用户

发布于 2018-10-23 21:04:09

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

代码语言:javascript
复制
.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;
}

为了获得理想的结果,您需要调整绝对位置,因此:

代码语言:javascript
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2018-10-23 22:12:12

最好不要使用calc,因为它支持IE

代码语言:javascript
复制
..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%;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52913976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档