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

相关推荐
2501_920931703 小时前
React Native鸿蒙跨平台采用ScrollView的horizontal属性实现横向滚动实现特色游戏轮播和分类导航
javascript·react native·react.js·游戏·ecmascript·harmonyos
0思必得05 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
东东5165 小时前
智能社区管理系统的设计与实现ssm+vue
前端·javascript·vue.js·毕业设计·毕设
catino5 小时前
图片、文件的预览
前端·javascript
2501_920931707 小时前
React Native鸿蒙跨平台实现推箱子游戏,完成玩家移动与箱子推动,当所有箱子都被推到目标位置时,玩家获胜
javascript·react native·react.js·游戏·ecmascript·harmonyos
layman05287 小时前
webpack5 css-loader:从基础到原理
前端·css·webpack
半桔7 小时前
【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典
前端·css·html
AI老李7 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·postcss
_OP_CHEN7 小时前
【前端开发之CSS】(一)初识 CSS:网页化妆术的终极指南,新手也能轻松拿捏页面美化!
前端·css·html·网页开发·样式表·界面美化
啊哈一半醒7 小时前
CSS 主流布局
前端·css·css布局·标准流 浮动 定位·flex grid 响应式布局