小杨不掉发 2022-06-14 09:11 采纳率: 71.4%
浏览 910
已结题

vue两行文本溢出显示展开按钮

微信小程序
现在有一个需求,当文本超过两行溢出显示省略号,并在末尾展示展开按钮
我现在的做法是动态绑定class类名,当他的字符长度超过45的时候就

if (item.reply && item.reply.length < 45) item.replyFlag = true
 // reply字段就是返回的文字

这是正常显示

img

当输入的数字就会这样这里的长度都还没到45

img

当输入的英文长度==45的时候就会这样

img

    width: 94%;
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
// 这是长度不超过45的样式
// 超过45的时候
// -webkit-line-clamp:inherit

请问这种怎么解决或者还有别的更好的方式处理吗

  • 写回答

11条回答 默认 最新

  • czDonald 2022-06-16 10:00
    关注
    获得0.05元问题酬金

    小程序组件封装
    设置行高,2行多高换算成px,这里组件是5行换算成px是126,渲染后获取元素高度判断是否超出,超出增加对应样式和控制显示展开按钮
    wx.js

    import random from 'lodash.random';
    
    Component({
      options: { addGlobalClass: true },
      properties: {
        text: String,
      },
    
      data: {
        showExpande: false,
        ellipsis: false,
        id: '',
      },
    
      lifetimes: {
        ready() {
          if (this.data.text) {
            this.init();
          }
        },
      },
    
      methods: {
        onToggle() {
          this.setData({ ellipsis: !this.data.ellipsis });
        },
    
        init() {
          this.setData({ id: 'content' + Date.now() + random(100000, 999999).toString() }, () => {
            const query = wx.createSelectorQuery().in(this);
            query.select(`#${this.data.id}`).boundingClientRect((res) => {
              const showExpande = res?.height > 126;
              this.setData({ showExpande });
            }).exec();
          });
        },
      },
    });
    

    wx.wxml

    <view wx:if="{{text}}" class="act-detail_intro border-box pd-20 mt-40 radius-24 flex-column">
        <view class="font-28 {{!ellipsis ? 'ellipsis-5' : ''}}">
            <text id="{{id}}" decode space>{{text}}</text>
        </view>
        <view class="fold_btn width-full flex-end font-28" bindtap="onToggle" wx:if="{{showExpande}}">{{!ellipsis ? '展开' : '收起'}}</view>
    </view>
    

    wx.wxss

    .act-detail_intro {
      background: rgba(255, 255, 255, 0.2);
      font-weight: 400;
      color: #ffffff;
      line-height: 42rpx;
    }
    
    .act-detail_intro .fold_btn {
      font-weight: 400;
      line-height: 42rpx;
      color: rgba(255, 255, 255, 0.2);
    }
    .ellipsis-5 {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      text-overflow: -o-ellipsis-lastline;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 5;
      line-clamp: 5;
    }
    

    wx.json

    {
      "component": true,
      "usingComponents": {}
    }
    
    
    评论

报告相同问题?

问题事件

  • 系统已结题 6月22日
  • 修改了问题 6月14日
  • 赞助了问题酬金5元 6月14日
  • 创建了问题 6月14日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表