vue文字超长自动poptip

这个博客介绍了如何使用Boya组件库中的poptip组件来处理长文本溢出问题。通过监听鼠标enter和leave事件,动态调整显示和隐藏策略,以及设置自适应宽度,实现了一个智能提示框。当文字内容过长时,poptip会在鼠标悬停时显示完整内容,提供更好的用户体验。同时,文章强调了在调用该组件时需要指定合适的宽度。
摘要由CSDN通过智能技术生成

组件

poptip用的是boya组件,样式根据需要自己调整下

<template>
  <div class="text-poptip-component" @mouseenter="onTextMouseEnter" @mouseleave="onTextMouseLeave">
    <div v-show="isShow" ref="textnopop" class="text-no-poptip">{{ text }}</div>
    <Poptip
      v-show="!isShow"
      word-wrap
      :width="poptipWidth"
      trigger="hover"
      :placement="placement"
      :content="text"
    >
      <div class="text-poptip">{{ text }}</div>
    </Poptip>
  </div>
</template>
<script>
export default {
  props: {
    text: {
      type: String,
      default: '--'
    },
    placement: {
      type: String,
      default: 'top-start'
    }
  },
  data() {
    return {
      isShow: true,
      poptipWidth: 300
    }
  },
  methods: {
    onTextMouseEnter() {
      const obj = this.$refs.textnopop
      this.isShow = obj.offsetWidth >= obj.scrollWidth
      this.poptipWidth = obj.offsetWidth
    },
    onTextMouseLeave() {
      this.isShow = true
    }
  }
}
</script>
<style lang="less" scoped>
.by-poptip {
  width: 100%;
}
/deep/ .by-poptip-rel {
  width: 100%;
}
.text-poptip-component {
  display: inline-block;
  vertical-align: top;
  zoom: 1
}
.text-poptip {
  width: 100%;
  display: block;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  line-height: 20px;
}
.text-no-poptip {
  width: 100%;
  display: inline-block;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  line-height: 20px;
}
</style>

引用后调用

注意在调用时指定宽度

<TextPoptip text="很多很多的很长很长的文字-很多很多的很长很长的文字-很多很多的很长很长的文字-很多很多的很长很长的文字" placement="bottom-start" style="width: calc(100% - 120px)" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值