![](/img/trans.png)
[英]Put background image behind ionic ion-content and ion-header
[英]How to put image in both <ion-header> and <ion-content> in Ionic
有一種更簡單的方法可以做到這一點,那就是使用ion-content
組件 ( docs ) 中的fullscreen
屬性。 所以訣竅是讓內容全屏,然后將 header 的背景設置為透明,這樣它就不會覆蓋背景。
<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) {}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.