css - 如何将图像放在两者中和在离子
问题描述
我目前正在开发 Ionic 应用程序并坚持在 ion-header 和 ion-content 中实现图像。
这是我如何实现的屏幕截图。正如你从截图中看到的那样,离子头和离子内容内容被隐藏了,因为我将图像设置z-index
为高数字;
谁能建议如何实现这一目标?谢谢。
解决方案
有一种更简单的方法可以做到这一点,它是使用组件中的fullscreen
属性ion-content
(docs)。所以诀窍是让内容全屏,然后将标题的背景设置为透明,这样它就不会覆盖背景。
模板:
<ion-header class="ion-no-border">
<ion-toolbar>
<ion-title>Home</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding" fullscreen>
...
...
</ion-content>
款式:
ion-toolbar {
--ion-toolbar-background: transparent;
}
ion-content {
--background: url('/assets/your_image.jpg') no-repeat center center / cover;
}
重要提示:目前存在一个问题,导致背景图像在某些特定场景下闪烁(Github 问题)。如果此问题影响您的应用,建议的解决方法是在组件中设置背景,而不是像这样使用 css:
模板:
<ion-header class="ion-no-border">
<ion-toolbar>
<ion-title>Home</ion-title>
</ion-toolbar>
</ion-header>
<!-- The only change is the id added to the ion-content -->
<ion-content id="my-page-content" class="ion-padding" fullscreen>
...
...
</ion-content>
款式:
ion-toolbar {
--ion-toolbar-background: transparent;
}
ion-content {
// Do not set the background here!
}
零件:
import { DomController } from '@ionic/angular';
// ...
@Component({...})
export class MyPage {
constructor(private domController: DomController) {}
// ...
ionViewWillEnter() {
this.initializeBackground();
}
// ...
private initializeBackground(): void {
try {
const content = document.querySelector('#my-page-content');
const innerScroll = content.shadowRoot.querySelector('.inner-scroll');
this.domController.write(() => {
innerScroll.setAttribute(
'style',
'background: url("/assets/img/your_image.jpg") no-repeat center center / cover',
);
});
} catch (e) {}
}
}
推荐阅读
- nginx - centos 7 nginx phpmyadmin 未找到
- python - 检查一个线段是否与一组线段相交
- php - JSON 数组:PHP 警告:array_count_values():只能计算 STRING 和 INTEGER 值
- ethereum - SSH 和以太坊节点
- git - 为什么git不能合并不同行不同变化的文本文件?
- c# - 将对象从 API 转换为字符串 C#
- android - 如何从 MapBox 的 LocationEngineProvider 创建 Observable?
- c# - 防止用户界面层依赖于数据访问层
- oauth-2.0 - WSO2 IS 5.6.0 和 OpenID Connect 声明
- sql - 从 ntext 中提取值