Ionic framework 带有web组件图像的图像网格布局
我的目标是为一系列图像实现这种布局: 如您所见,一个简单的2列正方形图像网格-这在引导环境中非常容易,其中每个指定2列或大小6,然后简单地将图像的CSSIonic framework 带有web组件图像的图像网格布局,ionic-framework,ionic4,web-component,shadow-dom,Ionic Framework,Ionic4,Web Component,Shadow Dom,我的目标是为一系列图像实现这种布局: 如您所见,一个简单的2列正方形图像网格-这在引导环境中非常容易,其中每个指定2列或大小6,然后简单地将图像的CSS对象匹配设置为覆盖,以避免拉伸并将高度设置为自动 问题是,我试图在Ionic 4中实现这一点,其中img元素是一个web组件,并且只有某些属性是可以自定义的。到目前为止,我可以以2列的方式显示图像,但纵横比是关闭的(我还必须使用元素,所以不能简单地使用HTML5 img元素) 以下是我到目前为止的情况: <ion-grid>
对象匹配设置为覆盖,以避免拉伸并将高度设置为自动
问题是,我试图在Ionic 4中实现这一点,其中img元素是一个web组件,并且只有某些属性是可以自定义的。到目前为止,我可以以2列的方式显示图像,但纵横比是关闭的(我还必须使用元素,所以不能简单地使用HTML5 img元素)
以下是我到目前为止的情况:
<ion-grid>
<ion-row>
<ion-col size="6" *ngFor="let image of selectedImageURIs">
<ion-img [src]="image"></ion-img>
</ion-col>
</ion-row>
</ion-grid>
注意:Ionic框架有自己的“引导”式框架,称为离子网格。我的结局是:
我知道需要使它们在高度和宽度上保持一致,并将对象设置为适合覆盖,但如何使用离子img实现这一点?我是一个web组件,因此影子Dom发挥作用。文档只提到了“组件在内部使用交叉点观察者”
。那能满足我的需要吗?我不熟悉web组件,不知道我缺少什么。page.html
<ion-row class="row-card">
<ion-col size="6" class="col-card">
<ion-card class="card-service">
<img src="../../assets/images/services/w-medical.png" class="img-service" />
</ion-card>
</ion-col>
<ion-col size="6" class="col-card">
<ion-card class="card-service">
<img src="../../assets/images/services/w-professional.png" class="img-service" />
</ion-card>
</ion-col>
</ion-row>
ion-grid>
这是我在Ionic 4应用程序中使用的解决方案。我使用css来完成你想要的外观。您的模板将与此类似,并使用ionimg
利用延迟加载功能
html
<ion-grid>
<ion-row>
<ion-col *ngFor="let category of categories" size="6">
<button #categoryButton type="button" (click)="selectCategory(categoryButton, category)" class="category-button">
<ion-card padding class="category-card">
<ion-card-content class="category-card-content">
<ion-img *ngIf="!imageNotLoaded; else loadingCategory" src="{{category.icon_image}}" class="category-icon" (ionError)="imageNotLoaded = true"></ion-img>
<ng-template #loadingCategory>
<ion-skeleton-text animated style="height: 70px;width: 70px"></ion-skeleton-text>
</ng-template>
</ion-card-content>
<ion-card-title class="category-title">{{category.name}}</ion-card-title>
</ion-card>
</button>
</ion-col>
</ion-row>
</ion-grid>
这将产生最终结果,如下图所示
注意:这是所有组件级代码,您需要使用:ng deep
绕过css代码的阴影dom
ion-grid >
<ion-row>
<ion-col col-6 col-md-4 col-xl-3 *ngFor="let image of [1,2,3,4,5,6,7,8,9,10,11]">
<img src=''/>
<!-- <div class="image-container" [style.background-image]="'url(assets/img/' + image + '.jpg)'"></div> -->
</ion-col>
</ion-row>
</ion-grid>
<ion-grid>
<ion-row>
<ion-col *ngFor="let category of categories" size="6">
<button #categoryButton type="button" (click)="selectCategory(categoryButton, category)" class="category-button">
<ion-card padding class="category-card">
<ion-card-content class="category-card-content">
<ion-img *ngIf="!imageNotLoaded; else loadingCategory" src="{{category.icon_image}}" class="category-icon" (ionError)="imageNotLoaded = true"></ion-img>
<ng-template #loadingCategory>
<ion-skeleton-text animated style="height: 70px;width: 70px"></ion-skeleton-text>
</ng-template>
</ion-card-content>
<ion-card-title class="category-title">{{category.name}}</ion-card-title>
</ion-card>
</button>
</ion-col>
</ion-row>
</ion-grid>
ion-col {
text-align: center;
}
.active {
.category-card {
border-color: var(--ion-color-primary)!important;
box-shadow: 0 4px 16px var(--ion-color-primary)!important;
}
}
.category-button {
padding: 4px;
background: transparent;
.category-card {
min-width: 100%;
margin: 0;
border: 2px solid;
.category-card-content {
text-align: center;
.category-icon {
width: available;
}
}
.category-title {
font-size: x-small;
font-weight: bolder;
}
}
}