flex布局控制展示几行

需求: 1、配置4-5个盒子,默认展示两行,第一行展示3个,剩余折行展示。 2、配置小于4个盒子,展示一行,平均分布。

后台返回数据
// 假数据
const assetList = [
    { alias: 'a', score:0 },
    { alias: 'a', score:0 },
    { alias: 'a', score:0 },
    { alias: 'a', score:0 },
  ]
盒子布局
// 盒子布局
<Div className="equal-division" couponLength={assetList.length}>
  {assetList.map((item: { score: string; alias:string      }, index: number) => (
   <div className={assetList.length <= 4 ?'coupon_points' : 'coupon_points_V4'}
         key={Math.random()}
         onClick={() => handlerCoupon(item, index)}
   >
    <div className="space-y-14">
      <div className="font_4 text_5">{item.score}</div>
      <div className="font_5">{item.alias}</div>
    </div>
  </div>
 )
)}
</Div>
样式
import styled from 'styled-components
父元素
const Div = styled.div<{
  couponLength: number 传入数组长度进行判断
}>`
  display: flex;  flex布局
  flex-wrap: wrap; 换行
  position: relative;
  flex: 1; 
  justify-content: ${(p) =>
    p.couponLength <= 4 ? ' space-around' : 'flex-start'}; 根据长度判断如何显示
   //子元素
  .coupon_points,
  .coupon_points_V4 {
    // 使用计算属性calc(1 / ${p.couponLength})
    flex: ${(p) => (p.couponLength <= 4 ? `calc(1 / ${p.couponLength})` : '')};
    width: ${(p) => (p.couponLength > 4 ? '117px' : '')};
    display: flex;
    justify-content: center;
    align-items: center;
    .space-y-14 {
      .font_4 {
        font-size: 17px;
        color: #333333;
        text-align: center;
        line-height: 25px;
        height: 25px;
        font-weight: 500;
        margin-bottom: 4px;
      }
      .font_5 {
        font-size: 12px;
        color: #999999;
        text-align: center;
        line-height: 16px;
        height: 16px;
        font-weight: 400;
      }
    }
  }
  .coupon_points {
    &:not(:last-child) {
      &::after {
        content: '';
        display: block;
        position: absolute;
        right: 0;
        top: 18px;
        width: 0.5px;
        height: 36px;
        background-color: #e8e8e8;
      }
    }
  }
  .coupon_points_V4 {
    &:nth-child(1) {
      margin-bottom: 1px;
    }
    &:not(:nth-child(3n)) {
      &::after {
        content: '';
        display: block;
        position: absolute;
        right: 0;
        top: 18px;
        width: 0.5px;
        height: 36px;
        background-color: #e8e8e8;
      }
    }
  }
`
export { Div }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值