vue使用flex布局做自适应宽度到5的倍数的div换行

12 篇文章 0 订阅
本文详细描述了如何使用HTML和CSS通过双层for循环,将数组数据每5个元素分为一组,并在页面上展示。作者展示了在Vue组件中创建考试题目列表的步骤,包括`splitItemEveryFive`和`afterSplitList`函数的应用,最终实现了按题目的合理布局。
摘要由CSDN通过智能技术生成

我的实现方式是通过html中双层for循环做的

首先我们把数组数据处理一下,因为我们是每5个元素分成一个div

看下数据格式:

 data() {
    return {
      examList: [
        {
          title: '正题',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题1',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
        },
        {
          title: '正题2',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题3',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题4',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题5',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        }
      ]

    };
  },
<div class="fl" v-for="start in splitItemEveryFive(exam)">
.....
</div>
    splitItemEveryFive(exam) {
      return Math.floor(exam.listData.length / 5) + 1;
    },

上面的代码我们先把他的父亲div写出来了, 例如我们有13个元素。那么我们就遍历4次父亲div,然后再子div中根据下标值循环5次

子div中的处理

  <topic-no-item
       v-for="item in afterSplitList(exam, start - 1)"
       :topicIndex="item"
       :hasChecked="false"
   />
    afterSplitList(exam, start) {
      return exam.listData.slice(start * SPLIT_NUM, start * SPLIT_NUM + SPLIT_NUM);
    },

完整代码:

<template>
  <main class="exam-no-main">
    <div class="exam-no-wrapper">
      <div class>
        <exam-header height="44px" title="题目列表" />
      </div>
      <div class="exam-no-body">
        <div v-for="exam in examList">
          <div class="exam-no-body-title">{{ exam.title }}</div>
          <div class="exam-no-body-list">
            <div class="display-flex" v-for="start in splitItemEveryFive(exam)">
              <topic-no-item
                v-for="item in afterSplitList(exam, start - 1)"
                :topicIndex="item"
                :hasChecked="false"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </main>
</template>
<script>
import TopicNoItem from './TopicNoItem';
import ExamHeader from '../components/ExamHeader';

const SPLIT_NUM = 5;
export default {
  name: 'TopicNoList',
  components: {
    TopicNoItem,
    ExamHeader,
  },
  data() {
    return {
      examList: [
        {
          title: '正题',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题1',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
        },
        {
          title: '正题2',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题3',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题4',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        },
        {
          title: '正题5',
          listData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        }
      ]

    };
  },
  created() { },
  watch: {
  },
  methods: {
    afterSplitList(exam, start) {
      return exam.listData.slice(start * SPLIT_NUM, start * SPLIT_NUM + SPLIT_NUM);
    },
    splitItemEveryFive(exam) {
      return Math.floor(exam.listData.length / 5) + 1;
    },
  },
};
</script>
<style lang='scss' scoped>
.exam-no-main {
  background-color: #f0f2f5;
  min-height: 100%;
  position: relative;
  width: 278px;
  box-sizing: border-box;
}
$padding-top-bottom: 20px;
$padding-left-right: 20px;
.exam-no-wrapper {
  background: #ffffff;
  height: calc(100% - $padding-top-bottom * 2);
  width: calc(100% - $padding-left-right * 2);
  top: $padding-top-bottom;
  left: $padding-left-right;
  border-radius: 8px;
  position: absolute;
  .exam-no-body {
    padding: 15px;
    box-sizing: border-box;
    // 45是头部题目列表的高度
    height: calc(100% - 45px);
    overflow-y: auto;
    &-title {
      font-size: 12px;
      width: 90%;
      text-align: left;
      margin: 12px auto;
      color: #909399;
    }
    .display-flex {
      display: flex;
      min-width: 160px;
    }
    &-list {
      display: flex;
      width: 90%;
      margin: 0 auto;
      flex-wrap: wrap;
      justify-content: flex-start;
    }
  }
}
</style>

实现效果如图:

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值