PC文本超出变为省略号,同时鼠标移入显示文本详情

首先封装一个TextOverTooltip.vue,然后在需要的页面引入改页面

import TextOverTooltip from "@/components/TextOverTooltip.vue";

 components: {

    TextOverTooltip,

  },

本文章是今天看其他大佬写的,但是找不到他的文章了,他忘记写components。如果作者本人看到请联系我我加上转载链接。

<template>
  <div class="text-over-tooltip-components">
    <el-tooltip
      :effect="effect"
      :disabled="isDisabledTooltip"
      :content="content"
      :placement="placement"
    >
      <div
        class="ellipsis"
        :class="className"
        @mouseover="onMouseOver(refName)"
      >
        <span :ref="refName">{{ content }}</span>
      </div>
    </el-tooltip>
  </div>
</template>
  
  <script>
export default {
  name: "TextOverTooltip",
  props: {
    // 显示的文字内容
    content: String,
    // 设置父元素的样式:比如宽度字体等,需可以自己在组件内部配置样式比如字体大小20:fs20
    className: String,
    // 子元素标识(如在同一页面中调用多次组件,此参数不可重复)
    refName: String,
    // 默认提供的主题 dark/light
    effect: {
      type: String,
      default: () => {
        return "dark";
      },
    },
    // Tooltip 的出现位置top/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-end
    placement: {
      type: String,
      default: () => {
        return "bottom";
      },
    },
  },
  data() {
    return {
      isDisabledTooltip: true, // 是否需要禁止提示
    };
  },
  methods: {
    // 移入事件: 判断内容的宽度contentWidth是否大于父级的宽度
    onMouseOver(str) {
      let parentWidth = this.$refs[str].parentNode.offsetWidth;
      let contentWidth = this.$refs[str].offsetWidth;
      // 判断是否禁用tooltip功能
      this.isDisabledTooltip = contentWidth <= parentWidth;
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.text-over-tooltip-components {
  /* 文字超出宽度显示省略号 单行 */
  .ellipsis {
    width: 138px;
    height: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* 自定义样式 */
  .fs20 {
    font-weight: 400;
    font-size: 14px;
    line-height: 28px;
    color: #009a29;
    margin-left: 24px;
  }
}
</style>
  
  
                     <text-over-tooltip
                        refName="testName1"
                        className="fs20"
                        :content="val.wjxxpjContent"
                      ></text-over-tooltip>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值