uni-app v-for 一行限制4个(占位)

<template>
  <view>
    <image class="respon-image" :src="isIcon" mode="aspectFit"></image>
    <view class="container">
      <view class="functional-entry" v-for="(entry, index) in Controllist" :key="entry.id">
        <image class="icon" :src="entry.img" mode="aspectFit"></image>
        <text class="text" :style="{ lineHeight: lineHeightStyle }">{{ entry.name }}</text>
      </view>
      <view v-for="n in getSpaces" :key="'space-${n}'" class="functional-entry space"></view>
    </view>
  </view>
</template>

<script setup>
const isIcon = ref("/static/control_back.png");

const Controllist = ref([
  { id: '1', name: '1', img: '/static/control1.png' },
  { id: '2', name: '2', img: '/static/control2.png' },
  { id: '3', name: '3', img: '/static/control3.png' },
  { id: '4', name: '4', img: '/static/control4.png' },
  { id: '5', name: '5', img: '/static/control1.png' },
  { id: '6', name: '6', img: '/static/control1.png' },
  { id: '7', name: '7', img: '/static/control1.png' }
]);

const lineHeightStyle = computed(() => '1.5em'); // 例如,设置为字体大小的1.5倍

const getSpaces = computed(() => {
  const spaces = [];
  const entriesCount = Controllist.value.length;
  const rows = Math.ceil(entriesCount / 4);
  for (let i = 0; i < rows; i++) {
    const spacesPerRow = 4 - (entriesCount - i * 4);
    for (let j = 0; j < spacesPerRow; j++) {
      spaces.push(null); // 添加占位符null
    }
  }
  return spaces;
});
</script>

<style lang="scss" scoped>
.respon-image {
  width: 100%;
  height: 210px;
}

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 10px;
}

.functional-entry {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex-basis: 24%;
  margin-bottom: 20px;
}

.functional-entry.space {
  flex-basis: 24%;
  height: 0;
  padding-bottom: 100%;
}

.icon {
  width: 50px;
  height: 50px;
  margin-bottom: 5px;
}

.text {
  font-size: 12px;
  text-align: center;
  margin: 0;
  padding: 0;
  width: 100%;
  line-height: 1.5em; /* 使用计算属性设置行高 */
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值