当前位置:   article > 正文

对el-tooltip二次封装,封装后可自动显示提示_el-tooltip重新注册条件判断显示

el-tooltip重新注册条件判断显示

文字过长时对文字使用...省略,并显示文字的完整提示

子组件

在模板中通过插槽来展示用户传入的标签内容

  1. <template>
  2. <el-tooltip class="item" effect="dark" :disabled="isShowTooltip" :content="content" placement="top">
  3. <div ref="aotoTooltip" class="ellipsis" :style="`width:${width}`" @mouseover="mouseHover">
  4. <slot></slot>
  5. </div>
  6. </el-tooltip>
  7. </template>

鼠标悬浮时对元素宽度进行比对,如果超出则开启el-tooltip,如果未超出则禁用显示提示 

  1. export default {
  2. name: 'AutoTooltip',
  3. props: {
  4. // 提示内容
  5. content: {
  6. type: String,
  7. default: () => {
  8. return ''
  9. }
  10. },
  11. // 超出多少宽度后开始文字提示
  12. width: {
  13. type: String,
  14. default: () => {
  15. return ''
  16. }
  17. },
  18. },
  19. data() {
  20. return {
  21. isShowTooltip: true
  22. }
  23. },
  24. methods: {
  25. /**
  26. * 鼠标悬浮是获取内外侧的宽度
  27. */
  28. mouseHover() {
  29. let outer = this.$refs.aotoTooltip.offsetWidth;
  30. let inner = this.$refs.aotoTooltip.childNodes[0].offsetWidth;
  31. // 判断内存宽度是否大于外层宽度如果是则开启提示如果否则关闭提示
  32. if (inner>outer) {
  33. this.isShowTooltip = false;
  34. } else {
  35. this.isShowTooltip = true;
  36. }
  37. }
  38. }
  39. }

超长显示...

  1. .ellipsis {
  2. overflow: hidden;
  3. white-space: nowrap;
  4. text-overflow: ellipsis;
  5. }

父组件调用

  1. <template>
  2. <auto-tooltip :content="tipText"
  3. width="200px">
  4. <span>{{tipText}}</span>
  5. </auto-tooltip>
  6. </template>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号