当前位置: 首页 > news >正文

web-app uniapp监测屏幕大小的变化对数组一行展示数据作相应处理

web-app uniapp监测屏幕大小的变化对数组一行展示数据作相应处理
在这里插入图片描述
1.uni.getSystemInfoSync().screenWidth; 获取屏幕宽度
2.uni.onWindowResize() 实时监测屏幕宽度变化
3.根据宽度的大小拿到每行要展示的数量itemsPerRow
4.为了确保样式能够根据 itemsPerRow 动态调整,可以使用 CSS 变量或动态类。width: calc((100% - 40rpx * (itemsPerRow - 1)) / itemsPerRow);

<template><view class="index"><!-- list表单 --><view class="activityList"><view class="innerContent"><!-- conent-list --><view class="content-list"><view class="list-row" v-for="(row, rowIndex) in groupedCollectionList" :key="rowIndex"><view class="list-item" v-for="(item, index) in row" :key="index" @click="goDetail(item)"><view class="item-left" v-if="item.picture"><image class="img" :src="item.picture" /></view><view class="item-right"><view class="title space" v-if="item.name.length > 10">{{ item.name.slice(0, 10) }}...</view><view class="title space" v-else>{{ item.name }}</view><view class="title space">{{ item.createdTime }}</view></view></view></view></view></view></view></view></template><script>
export default {data() {return {list:[],itemsPerRow:1,// 默认每行显示1个};},computed: {// 在 computed 中添加 groupedCollectionList 以根据 itemsPerRow 分组数据。groupedCollectionList() {const rows = [];for (let i = 0; i < this.list.length; i += this.itemsPerRow) {rows.push(this.list.slice(i, i + this.itemsPerRow));}return rows;},},beforeMount() {this.updateScreenSize(); //初始化屏幕宽度uni.onWindowResize(this.updateScreenSize);  // 监听屏幕尺寸变化},beforeDestroy() {uni.offWindowResize(this.updateScreenSize);  // 移除监听器},methods: {// 获取当前屏幕宽度getScreenWidth() {return uni.getSystemInfoSync().screenWidth;},updateScreenSize(){const width = this.getScreenWidth()console.log(width,'width');// 562<width&&width<687if (width > 640) {this.itemsPerRow = 3;} else if (562<width&&width < 640) {this.itemsPerRow = 2;} else {this.itemsPerRow = 1;}},getList() {// this.$modal.loading("加载中..");this.list = [{picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",name:"测试1",createdTime:"2025-1-1"},{picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",name:"测试2",createdTime:"2025-1-1"},{picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",name:"测试3",createdTime:"2025-1-1"},{picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",name:"测试4",createdTime:"2025-1-1"}]},},async onLoad(e) {const { id }  = ethis.id = id},onShow() {this.pageNum = 1;this.getList();},};
</script><style lang="scss" scoped>
.index {width: 100%;min-height: 100vh;background: #f7f8fc;box-sizing: border-box;padding-bottom: calc(110rpx + env(safe-area-inset-bottom));.activityList {width: 100%;padding: 0 20rpx;padding-top: 24rpx;.innerContent {width: 100%;background: #ffffff;border-radius: 20rpx;padding: 20rpx;.content-list {padding: 20rpx;padding-right: 0rpx;.list-row {display: flex;justify-content: space-between;margin-bottom: 20rpx;}.list-item {// 确保样式能够适应不同数量的每行显示。width: calc((100% - 40rpx * (itemsPerRow - 1)) / itemsPerRow);height: 152rpx;display: flex;margin-bottom: 20rpx;.item-left {width: 270rpx;height: 152rpx;border-radius: 10rpx;position: relative;.img {width: 270rpx;height: 152rpx;border-radius: 10rpx;}}.item-right {flex: 1;padding: 10rpx 0rpx;display: flex;flex-direction: column;justify-content: space-between;width: 200rpx;padding-left: 20rpx;.title {font-size: 30rpx;font-family: PingFang SC, PingFang SC-Regular;font-weight: Regular;text-align: left;color: #333333;line-height: 41rpx;}.title.space {white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}}}}}}
}
</style>
http://www.lryc.cn/news/518960.html

相关文章:

  • 2025年VGC大众汽车科技社招入职测评综合能力英语口语SHL历年真题汇总、考情分析
  • Linux中配置Java环境变量
  • 完全自定义Qt翻译功能,不使用Qt Linguist的.ts 和 .qm类型翻译
  • 551 灌溉
  • php函数性能优化中应注意哪些问题
  • 安科瑞 Acrel-1000DP 分布式光伏监控系统在工业厂房分布式光伏发电项目中的应用
  • 鼠标自动移动防止锁屏的办公神器 —— 定时执行专家
  • 各种特种无人机快速发展,无人机反制技术面临挑战
  • 深入学习RabbitMQ的Direct Exchange(直连交换机)
  • HTML实战课堂之启动动画弹窗
  • 将本地的 Git 仓库上传到 GitHub 上(github没有该仓库)
  • 【Linux】模拟Shell命令行解释器
  • G-Star Landscape 2.0 重磅发布,助力开源生态再升级
  • Lianwei 安全周报|2024.1.7
  • ASP.NET Core 实现微服务 - Consul 配置中心
  • 使用redis的5种常用场景
  • 微信小程序防止重复点击事件
  • PySpark用sort-merge join解决数据倾斜的完整案例
  • sklearn-逻辑回归-制作评分卡
  • scrapy爬取图片
  • 在 Vue 项目中使用地区级联选
  • 【简博士统计学习方法】第1章:1. 统计学习的定义与分类
  • 利用 Python 脚本批量创建空白 Markdown 笔记
  • 【Qt】C++11 Lambda表达式
  • 怎样提高服务器中的数据传输速度?
  • Vue 封装公告滚动
  • JVM实战—12.OOM的定位和解决
  • 【python翻译软件V1.0】
  • Spring Boot中的依赖注入是如何工作
  • ubuntu22.04 编译安装libvirt 10.x