gpt4 book ai didi

css - 如何基于媒体查询动态传值

转载 作者:太空狗 更新时间:2023-10-29 18:27:47 26 4
gpt4 key购买 nike

在我的产品部分,大约有 300 种产品,因此对于分页,我使用了 ngx-pagination 插件。所以产品应该基于媒体查询来展示。例如,我必须将值(在媒体查询中)传递给 1600px 到 1200px 应该有 itemsPerPage:30,1199 到 768 itemsPerPage:24

如何实现?

<ecom-section-box>
<ecom-card-wc-5 *ngFor="let data of dataArray4 | paginate: { itemsPerPage: 30, currentPage: p } " [data]="data"></ecom-card-wc-5>
</ecom-section-box>
<div class="card card2">
<span class="pagination-alignment">
<pagination-controls (pageChange)="p = $event" ></pagination-controls>
</span>
</div>

最佳答案

init 上获取窗口宽度

public innerWidth: any;
ngOnInit() {
this.innerWidth = window.innerWidth;
}

如果你想在调整大小时保持更新:

@HostListener('window:resize', ['$event'])
onResize(event) {
this.innerWidth = window.innerWidth;
}

您可以使用此代码检查窗口大小是 1600px 到 1200px 还是 1199 到 768 并相应地设置 itemsPerPage

如何使用此代码:

@HostListener('window:resize', ['$event'])
onResize(event) {
this.innerWidth = window.innerWidth;
if (this.innerWidth <= 1600 && this.innerWidth >= 1200) {
itemsPerPage = 30;
} else if (this.innerWidth <= 1199 && this.innerWidth >= 768) {
itemsPerPage = 24;
}
}

关于css - 如何基于媒体查询动态传值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55923463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com