文本溢出隐藏 显示省略号,鼠标悬浮展示 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

相关推荐
烟袅几秒前
小程序开发入门:从结构到事件,快速掌握核心要点
前端·微信小程序
caicai_lf_niuniu几秒前
🌳 ComboTreeV2:高性能虚拟树
前端·vue.js
BLOOM1 分钟前
新一代前端数据mock工具Data Faker
前端·javascript
UIUV2 分钟前
微信小程序开发学习笔记:从架构到实战
前端·javascript·前端框架
程序猿_极客4 分钟前
JavaScript的Web APIs 入门到实战(day2):事件监听与交互实现,轻松实现网页交互效果(附练习巩固)
开发语言·前端·javascript·学习笔记·web apis 入门到实战
Mintopia22 分钟前
🚀 一文看懂 “Next.js 全栈 + 微服务 + GraphQL” 的整体样貌
前端·javascript·全栈
Mintopia24 分钟前
🧬 医疗Web场景下,AIGC的辅助诊断技术边界与伦理
前端·javascript·aigc
半桶水专家29 分钟前
父子组件通信详解
开发语言·前端·javascript
Watermelo61731 分钟前
从vw/h到clamp(),前端响应式设计的痛点与进化
前端·javascript·css·算法·css3·用户界面·用户体验
寻星探路35 分钟前
测试开发话题10---自动化测试常用函数(2)
java·前端·python