文本溢出隐藏 显示省略号,鼠标悬浮展示 el-tooltip(TooltipIsShowMixin封装)

目录

  • [mixins 封装](#mixins 封装)
  • [使用 TooltipIsShowMixin](#使用 TooltipIsShowMixin)
  • 效果展示

mixins 封装

TooltipIsShowMixin.js

js 复制代码
export const TooltipIsShowMixin = {
  data() {
    return {
      tooltipIsShow: false
    }
  },
  methods: {
    tooltipIsDisHandler(className) {
      this.$nextTick(() => {
        const dom = document.querySelector(className)
        const domScrollWidth = dom && dom.scrollWidth
        const domClientWidth = dom && dom.clientWidth
        // console.log('domScrollWidth----', domScrollWidth)
        // console.log('domClientWidth----', domClientWidth)
        this.tooltipIsShow = domClientWidth >= domScrollWidth
      })
    }
  }
}

使用 TooltipIsShowMixin

html 复制代码
<div class="list" :style="{ '--height': tableMaxHeight + 'px' }">
  <div v-for="(item, index) in tableData" :key="index" class="list-card">
    <div class="list-card-pic">
      <div class="list-card-pic-type">{{ item.type }}</div>
      <div class="list-card-pic-time">{{ item.duration }} {{ item.createTime }}</div>
    </div>
    <div class="list-card-info">
      <div class="list-card-info-header">
        <div class="list-card-info-header-title font-bold text-lg">{{ item.title }}</div>
        <div class="list-card-info-header-opera">
          <el-button type="text" icon="el-icon-edit" />
          <el-button type="text" icon="el-icon-delete" />
        </div>
      </div>
      <div class="list-card-info-describe">
        <el-tooltip
          effect="dark"
          :content="item.description"
          placement="top-start"
          :disabled="tooltipIsShow"
        >
          <span
            :class="[`description${index}`]"
            @mouseenter="tooltipIsDisHandler(`.description${index}`)"
          >
            {{ item.description }}
          </span>
        </el-tooltip>
      </div>
    </div>
  </div>
</div>

<script>
import { TooltipIsShowMixin } from '@/mixins/TooltipIsShowMixin'

export default {
  // ...
  mixins: [TooltipIsShowMixin],
}
</script>
<style scoped lang="scss">
.list {
  height: var(--height);
  overflow: auto;
  &-card {
    display: inline-block;
    width: calc(25% - 12px);
    margin: 0 15px 15px 0;
    box-shadow: 0 0 5px #ccc;

    &:nth-child(4n) {
      margin-right: 0;
    }

    &-pic {
      height: 170px;
      background: url('~@/assets/image/default-course-cover.png') no-repeat;
      background-size: 100% 100%;
      position: relative;
      color: #fff;
      $width: 10px;
      &-type {
        position: absolute;
        top: $width;
        right: $width;
        background: rgba($color: #fff, $alpha: 0.3);
        border-radius: 10px;
        padding: 3px 8px;
      }
      &-time {
        position: absolute;
        bottom: $width;
        right: $width;
      }
    }
    &-info {
      padding: 10px 20px;
      background-color: #fff;
      &-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      &-describe {
        margin: 5px 0;
        color: #00000073;
        span {
          display: inline-block;
          width: 100%;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
    }
  }
}
</style>

效果展示

未溢出的鼠标悬浮不展示 el-tooltip

溢出展示省略号的鼠标悬浮展示 el-tooltip

相关推荐
子兮曰1 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰1 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万1 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝1 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋1 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁1 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄1 天前
JavaScript 隐式全局变量解析
javascript
汉堡大王95271 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大1 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师1 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端