flex布局遇到多行单个列表最后一行对不齐的解决办法,解决写了justify-content:space-between之后还对不齐的办法

在这里插入图片描述
用ResizeObserver来解决,只不过不兼容ie,由于做的是混合app,所以不考虑ie,ie的话需要用到媒体查询或者其他办法,有更好的办法欢迎指教。
代码源码

// goods组件是要展示的数据
<div
 class="flex flex-wrap justify-between match-goods-com goods-com"
  ref="goodsCom"
>
  <goods
    v-for="(item, index) in productList"
    :key="index.id"
    type="ability"
    :goods="item"
    class="mb-20"
    :style="{
      marginRight: productMRIndexArr.includes(index)
        ? `${productMR}px`
        : 'auto',
    }"
  ></goods>
</div>

// js中
productList: [],
productMR: '', // 最后一行能力之间的间距
productMRIndexArr: [], // 哪些数据需要手动设置由边距(能力)
// css中需要设置一下伪元素
.match-goods-com {
  &::after {
    content: '';
    flex: auto;
  }
}
mounted() {
   // 监听goods组件最后一行的间距
   this.resizeObserver = new ResizeObserver(this.resizeChangeWH);
   this.resizeObserver.observe(document.querySelector('.goods-com'));
},
methods: {
   resizeChangeWH(entries) {
     this.productMRIndexArr = [];
     for (let entry of entries) {
       const childNodes = entry.target.childNodes;
       const groupArr = groupingData(childNodes, 'offsetTop');
       const lastGroupData =
         groupArr.length > 0 ? groupArr[groupArr.length - 1].list : [];
       const lastSecondGroupData =
         groupArr.length > 1 ? groupArr[groupArr.length - 2].list : [];
       // 如果最后一组数据长度小于第一组数据
       if (
         groupArr.length >= 2 &&
         lastGroupData.length < lastSecondGroupData.length
       ) {
         for (
           let i = childNodes.length - lastGroupData.length;
           i < childNodes.length - 1;
           i++
         ) {
           // 获取到第一个与第二个之间的间距
           this.productMR =
             lastSecondGroupData[1].offsetLeft -
             lastSecondGroupData[0].offsetLeft -
             lastSecondGroupData[0].offsetWidth;
           this.productMRIndexArr.push(i);
         }
       }
     }
   },
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值