当前位置:   article > 正文

el-tooltip 超出宽度显示提示内容,支持自定义提示内容_tooltip 长按提示 提示内容

tooltip 长按提示 提示内容

功能需求

  1. 超出内容区域宽度显示tooltip
  2. 自定义tooltip内容
  3. tooltip位置跟随鼠标位置显示

组件源码

<template>
  <el-tooltip
    v-bind="$attrs"
    :popper-class="['ts-ellipsis-tooltip', popperClass]"
    :placement="tooltipPlacement"
    :disabled="disabled"
    :offset="tooltipOffset"
  >
    <template #content>
      <slot v-if="$slots.content" name="content" />
      <template v-else>
        {{ content || text }}
      </template>
    </template>
    <div :class="['ts-ellipsis', { 'cursor-default': !disabled && placement === 'mouse' }, textClass]" @mouseover="handleMouseover">
      <span :ref="uniqueRefName">{{ text }}</span>
    </div>
  </el-tooltip>
</template>

<script>
import { uniqueId } from 'lodash';

export default {
  props: {
    text: { // 默认显示的文本
      type: String,
      default: ''
    },
    content: { // tooltip显示内容
      type: String,
      default: ''
    },
    offset: { // 偏移量
      type: String,
      default: ''
    },
    placement: { // tooltip位置,支持mouse值,跟随鼠标位置显示
      type: String,
      default: ''
    },
    popperClass: { // tooltip class
      type: String,
      default: ''
    },
    textClass: { // text class
      type: String,
      default: ''
    },
    refName: { // 为页面文字标识(如在同一页面中调用多次组件,此参数不可重复)
      type: String,
      default: ''
    }
  },
  data() {
    return {
      uniqueRefName: '',
      disabled: true,
      tooltipPlacement: this.placement,
      tooltipOffset: this.offset
    };
  },
  created() {
    this.uniqueRefName = this.refName || uniqueId('ellipsis-');
  },
  methods: {
    handleMouseover({ offsetX }) {
      const contentRef = this.$refs[this.uniqueRefName];
      const parentWidth = contentRef.parentNode.offsetWidth;
      const contentWidth = contentRef.offsetWidth;
      if (this.placement === 'mouse') { // tooltip跟随鼠标位置显示
        this.tooltipPlacement = 'bottom-start';
        this.tooltipOffset = offsetX + 5;
      }
      this.disabled = contentWidth <= parentWidth;
    },
  }
};
</script>

<style lang="scss" scoped>
.ts-ellipsis {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;

  &.cursor-default {
    cursor: default;
  }
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

组件调用

// placement跟随鼠标移动,其他情况和el-tooltip位置一致
<ts-ellipsis-tooltip
  popper-class="my-ellipsis-tooltip"
  text="这是显示文本"
  effect="light"
  placement="mouse"
  :open-delay="1"
>
  <template #content>
    这是tooltip内容
  </template>
</ts-ellipsis-tooltip>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/91885
推荐阅读
相关标签
  

闽ICP备14008679号