【页面上标签过多,极致版处理】

页面上标签过多,极致版处理

我们常常会遇到页面上标签太多,但是设计希望不去换行显示,直接截断的话样式又不美观,能做的处理办法就是根据当前整个标签组一整行的总宽度,去计算每一个标签加上padding+margin+文字本身的宽度值,看最终能放下几个,放不下的标签在循环的时候将是否显示设置为false就可以了。
接下来上代码!!

          let containerWidth = 240
          let sumWidth = 0 // tag-item元素宽度的累加和
          let tagWidth = 0
          all_tags.forEach((item, index) => {
            if (item.type == 'icon') {
              tagWidth = 24
            } else {
              let word = item.name
              let wordWidth = _this.getLenPx(word, 11)
              tagWidth = wordWidth + 4 * 3 + 2
            }
            if (sumWidth + tagWidth > containerWidth) {
              all_tags[index].isShow = false // 将多出的tag-item元素截断
            } else {
              all_tags[index].isShow = true
              sumWidth += tagWidth
            }
          })
          //将元素换算计算大小
            getLenPx: function (str, font_size) {
               var str_leng = str.replace(/[^\x00-\xff]/gi, 'aa').length
               return (str_leng * font_size) / 2
           },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值