对于超出一行的文本进行(展开/收起)操作的vue组件,实测好用

完整功能复用

1、对于超出一行的文本进行(展开/收起)操作,实测好用

<template>
  <!-- 多出一行出现展开、收起按钮 -->
  <div class="top-tips">
    <div :class="isOpen ? '' : 'tipsBox'">
      <span class="f14 potips" :class="isOpen ? '' : 'van-ellipsis opentext'">
        <span class="remekTitle">{{ title }}</span><span>{{ text }} </span>
      </span>
      <span v-if="showopen" class="f14 inTips" @click="inTips">
        {{ isOpen ? "收起" : "展开" }}
      </span>
    </div>
  </div>
</template>

<script>
export default {
  name: "textTips",
  data() {
    return {
      isOpen: false, //默认展开|收起
      showopen: true, //是否显示展开|收起按钮(文字)
    };
  },
  props: {
    // 显示的文字
    text: {
      type: String,
      default: "none",
      required: true,
    },
    // 标题
    title: {
      type: String,
      default: "none",
      required: true,
    },
  },
  created() {
    this.showopen = this.isLineWrap(); // 是否超出一行(超出则显示展开/收起)
  },
  methods: {
    inTips() {
      this.isOpen = !this.isOpen;
    },
      // 判断是否产出一行的函数
    isLineWrap(text = this.text) {
      const getBLen = function (str) {
        if (str == null) return 0;
        if (typeof str != "string") {
          str += "";
        }
        return str.replace(/[^\x00-\xff]/g, "01").length;
      };
      if (this.platform === "pc") {
        const lineWidth = 334;
        const textLine = getBLen(text) * 9.5;
        return textLine > lineWidth;
      } else {
        const fontSizeSty = document.documentElement.style.fontSize;
        const fontSize = Number(fontSizeSty.slice(0, fontSizeSty.length - 2));
        const textLine = fontSize * getBLen(text) * 0.16;
        const lineWidth = 4.56 * fontSize;
        return textLine > lineWidth;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.top-tips {
  display: flex;
  align-items: center;
  // padding: 5px 0;
  position: relative;
  .potips {
    padding: 2px 0;
    color: #9ea4a9;
    min-height: 16px;
  }
  .inTips {
    color: rgb(91, 169, 245);
  }
  .tipsBox {
    // 是否在一行显示,或者自认换行
    display: flex;
    align-items: baseline;
    width: 100%;
  }
}
.opentext {
  min-width: 90%;
  max-width: 38%;
  display: inline-block;
}
.van-ellipsis {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.asdasd {
  display: inline-block;
  width: 40px;
  height: 20px;
}
.flex {
  display: flex;
}
.remekTitle {
  display: inline-block;
  padding-bottom: 3px;
}
</style>

2、 组件的调用-也十分简单,一般来说使用场景在备注信息比较常见

<TextTips
          :title="'备注'"
          :text="需要展示的数据需要展示的数据需要展示的数据需要展示的数据需要展示的数据需要展示的数据"
          >
</TextTips>

3、效果展示

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值