移动端内容超出一行省略并自适应出现--展开收起功能

3 篇文章 0 订阅
2 篇文章 0 订阅

1、自定义一个组件textTips 里面使用的icon 是VantUI的 自己可以随意替换

<template>
  <!-- 多出一行出现展开、收起按钮 -->
  <div class="top-tips">
    <div class="tipsBox" :class="isOpen ? 'end' : 'bas'">
      <span class="f14 potips" ref="isOverflow" :class="isOpen ? '' : 'van-ellipsis opentext'">
        <span class="remekTitle">{{ title }}</span><span>{{ text }} </span>
      </span>
      <span v-if="showOpen" class="f14 inTips" @click="inTips">
        <span>{{ isOpen ? "收起" : "展开" }}</span>
        <van-icon :name="isOpen ? 'arrow-up' : 'arrow-down'" />
      </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,
    },
  },
  mounted () {
    // 是否超出一行(超出则显示展开/收起)
    // 获取标签内容的宽度和屏幕可视宽度作对比
    this.showOpen = this.$refs.isOverflow.clientWidth < this.$refs.isOverflow.scrollWidth;
  },
  methods: {
    inTips () {
      this.isOpen = !this.isOpen;
    },
  }
};
</script>

<style lang="less" scoped>
.top-tips {
  display: flex;
  align-items: center;
  position: relative;
  .potips {
    padding: 2px 0;
    color: #9ea4a9;
    min-height: 16px;
    flex: 1;
  }
  .inTips {
    color: rgb(91, 169, 245);
    display: flex;
    align-items: center;
  }
  .tipsBox {
    // 是否在一行显示,或者自认换行
    display: flex;
    justify-content: space-between;
    width: 100%;
  }
  .tipsBox.bas {
    align-items: baseline;
  }
  .tipsBox.end {
    align-items: flex-end;
  }
}
.opentext {
  flex: 1;
  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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值