IT评测·应用市场-qidao123.com

标题: web-app uniapp监测屏幕大小的变革对数组一行展示数据作相应处理 [打印本页]

作者: 张裕    时间: 2025-1-12 01:47
标题: 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);
  1. <template>
  2.     <view class="index">
  3.       <!-- list表单 -->
  4.       <view class="activityList">
  5.         <view class="innerContent">
  6.           <!-- conent-list -->
  7.           <view class="content-list">
  8.             <view class="list-row" v-for="(row, rowIndex) in groupedCollectionList" :key="rowIndex">
  9.               <view class="list-item" v-for="(item, index) in row" :key="index" @click="goDetail(item)">
  10.                 <view class="item-left" v-if="item.picture">
  11.                   <image class="img" :src="item.picture" />
  12.                 </view>
  13.                 <view class="item-right">
  14.                   <view class="title space" v-if="item.name.length > 10">{{ item.name.slice(0, 10) }}...</view>
  15.                   <view class="title space" v-else>{{ item.name }}</view>
  16.                   <view class="title space">{{ item.createdTime }}</view>
  17.                 </view>
  18.               </view>
  19.             </view>
  20.           </view>
  21.         </view>
  22.       </view>
  23.     </view>
  24.   </template>
  25. <script>
  26. export default {
  27.     data() {
  28.         return {
  29.             list:[],
  30.             itemsPerRow:1,// 默认每行显示1个
  31.         };
  32.     },
  33.     computed: {
  34.         // 在 computed 中添加 groupedCollectionList 以根据 itemsPerRow 分组数据。
  35.         groupedCollectionList() {
  36.             const rows = [];
  37.             for (let i = 0; i < this.list.length; i += this.itemsPerRow) {
  38.             rows.push(this.list.slice(i, i + this.itemsPerRow));
  39.             }
  40.             return rows;
  41.         },
  42.     },
  43.     beforeMount() {
  44.         this.updateScreenSize(); //初始化屏幕宽度
  45.         uni.onWindowResize(this.updateScreenSize);  // 监听屏幕尺寸变化
  46.     },
  47.     beforeDestroy() {
  48.        uni.offWindowResize(this.updateScreenSize);  // 移除监听器
  49.     },
  50.     methods: {
  51.         // 获取当前屏幕宽度
  52.         getScreenWidth() {
  53.         return uni.getSystemInfoSync().screenWidth;
  54.         },
  55.         updateScreenSize(){
  56.             const width = this.getScreenWidth()
  57.             console.log(width,'width');
  58.             // 562<width&&width<687
  59.             if (width > 640) {
  60.                 this.itemsPerRow = 3;
  61.             } else if (562<width&&width < 640) {
  62.                 this.itemsPerRow = 2;
  63.             } else {
  64.                 this.itemsPerRow = 1;
  65.             }
  66.         },
  67.         getList() {
  68.             // this.$modal.loading("加载中..");
  69.             this.list = [{
  70.                 picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",
  71.                 name:"测试1",
  72.                 createdTime:"2025-1-1"
  73.             },
  74.             {
  75.                 picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",
  76.                 name:"测试2",
  77.                 createdTime:"2025-1-1"
  78.             },
  79.             {
  80.                 picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",
  81.                 name:"测试3",
  82.                 createdTime:"2025-1-1"
  83.             },
  84.             {
  85.                 picture:"https://jis-eclass.oss-accelerate.aliyuncs.com/images/Course/20250108/20250108141759a888cbd5-8dec-4b98-a423-30d1f2081023.jpg",
  86.                 name:"测试4",
  87.                 createdTime:"2025-1-1"
  88.             }
  89.         ]
  90.         },
  91.     },
  92.     async onLoad(e) {
  93.         const { id }  = e
  94.         this.id = id
  95.     },
  96.     onShow() {
  97.         this.pageNum = 1;
  98.         this.getList();
  99.     },
  100. };
  101. </script>
  102. <style lang="scss" scoped>
  103. .index {
  104.   width: 100%;
  105.   min-height: 100vh;
  106.   background: #f7f8fc;
  107.   box-sizing: border-box;
  108.   padding-bottom: calc(110rpx + env(safe-area-inset-bottom));
  109.   .activityList {
  110.     width: 100%;
  111.     padding: 0 20rpx;
  112.     padding-top: 24rpx;
  113.     .innerContent {
  114.       width: 100%;
  115.       background: #ffffff;
  116.       border-radius: 20rpx;
  117.       padding: 20rpx;
  118.       .content-list {
  119.         padding: 20rpx;
  120.         padding-right: 0rpx;
  121.         .list-row {
  122.           display: flex;
  123.           justify-content: space-between;
  124.           margin-bottom: 20rpx;
  125.         }
  126.         .list-item {
  127.           // 确保样式能够适应不同数量的每行显示。
  128.           width: calc((100% - 40rpx * (itemsPerRow - 1)) / itemsPerRow);
  129.           height: 152rpx;
  130.           display: flex;
  131.           margin-bottom: 20rpx;
  132.           .item-left {
  133.             width: 270rpx;
  134.             height: 152rpx;
  135.             border-radius: 10rpx;
  136.             position: relative;
  137.             .img {
  138.               width: 270rpx;
  139.               height: 152rpx;
  140.               border-radius: 10rpx;
  141.             }
  142.           }
  143.           .item-right {
  144.             flex: 1;
  145.             padding: 10rpx 0rpx;
  146.             display: flex;
  147.             flex-direction: column;
  148.             justify-content: space-between;
  149.             width: 200rpx;
  150.             padding-left: 20rpx;
  151.             .title {
  152.               font-size: 30rpx;
  153.               font-family: PingFang SC, PingFang SC-Regular;
  154.               font-weight: Regular;
  155.               text-align: left;
  156.               color: #333333;
  157.               line-height: 41rpx;
  158.             }
  159.             .title.space {
  160.               white-space: nowrap;
  161.               overflow: hidden;
  162.               text-overflow: ellipsis;
  163.             }
  164.           }
  165.         }
  166.       }
  167.     }
  168.   }
  169. }
  170. </style>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4