我之前尝试过,但没有找到一种通用的方法来使模态按我想要的方式运行。
于是我苦苦挣扎,用下面的全局scss实现了响应式模态和其他种类的模态:
ion-modal {
&.my-modal-inner {
display: flex;
align-items: center;
justify-content: center;
ion-backdrop {
visibility: visible;
}
.modal-wrapper {
display: flex;
align-items: flex-start;
justify-content: center;
overflow: auto;
width: auto;
height: auto;
left: auto;
top: auto;
contain: content;
max-width: 70%;
max-height: 70%;
border-radius: 2px;
box-shadow: 0 28px 48px rgba(0, 0, 0, 0.4);
> .ion-page {
position: relative;
display: block;
width: auto;
height: auto;
contain: content;
box-shadow: 0 28px 48px rgba(0, 0, 0, 0.4);
}
}
&.my-stretch {
.modal-wrapper {
overflow: hidden;
width: 100%;
height: 100%;
> .ion-page {
width: 100%;
height: 100%;
}
}
}
}
&.my-fullscreen {
.modal-wrapper {
position: absolute;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
}
您可以使用cssClass: 'my-modal-inner' 来创建具有内部内容大小的响应式模态:
模态框将占据最大宽度和高度的 70%(如上述 css 中定义的)当内容超过限制时:
如果模态的内容应该占据所有的容器元素(比如一个页面或者一个带有ion-content的组件),它就不能很好地与上面的情况一起工作,因为模态假设容器应该具有其子容器的大小,这可能导致不良行为(模态将非常小,超出其应有的范围):
相反,您可以使用cssClass: 'my-modal-inner my-stretch' 定义模式占据其最大尺寸:
如果您希望模态是全屏,即使在大型桌面浏览器中,您也可以使用cssClass: 'my-fullscreen':
注意事项:
- 您可以更改前缀
my- 使用您选择的任何其他前缀(或不使用前缀)。
- 您可以更改上述 CSS 中模态的最大宽度和高度,只要您觉得合适(我将两者都定义为 70%,在您的情况下为 50% 和宽度和高度分别为 80%)。
- 以上屏幕截图是在桌面浏览器中截取的,但模式更改也适用于使用 Ionic 的移动/原生应用程序(我在 Android 中测试过,它应该也适用于 iOS)。
更新(2018-07-30)
关于模态的 HTML 代码:
1) 与内部内容一样大或一样小的响应式模式(单个div 或divs、spans 和其他块和内联元素的层次结构应该做):
<div class="main">
<div class="img-container">
<img src="assets/img/main/icon.png" [alt]="APP_NAME">
</div>
<div class="info-container">
<div class="app-name">{{ APP_NAME }}</div>
<div class="app-version">Version {{ APP_VERSION }}</div>
<div class="app-year">@{{ INITIAL_YEAR }}-{{ CURRENT_YEAR }} {{ APP_NAME }}</div>
<div class="app-rights">All rights reserved</div>
</div>
</div>
2) 模态将占据其最大尺寸(使用类my-stretch)。在这种情况下,任何离子页面都可以:
<ion-header>
<ion-navbar>
<ion-title>My Title</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<p> My content </p>
</ion-content>