• 基于 element ui 之 ui-tooltip 组件


    在使用 el-select 时,有时 el-option 的内容过长,导致超长显示,例如:

    在这里插入图片描述

    为了更好展示,我们可以设置一个最大宽度,超出最大宽度时使用省略号代替,当鼠标移入时,使用 el-tooltip 展示完整内容。以下是封装的 ui-tooltip 组件。

    <template>
      <el-tooltip
        :content="content"
        :placement="placement"
        effect="light"
        :disabled="isDisabled"
      >
        <div class="contentnowrap" @mouseenter="isShowTooltip">
          {{ content }}
        </div>
      </el-tooltip>
    </template>
    
    <script>
    export default {
      name: "UiTooltip",
      props: {
        content: {
          type: String,
          default: "",
        },
        placement: {
          type: String,
          default: "top",
        },
      },
      data() {
        return {
          isDisabled: false,
        };
      },
      methods: {
        isShowTooltip(e) {
          let clientWidth = e.target.clientWidth,
            scrollWidth = e.target.scrollWidth;
          if (scrollWidth > clientWidth) {
            this.isDisabled = false;
          } else {
            this.isDisabled = true;
          }
        },
      },
    };
    </script>
    
    <style lang="scss">
    .contentnowrap {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    </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

    使用方式

    1、引入

    import UiTooltip from "@/components/ui-tooltip";
    
    • 1

    2、注册

    components: {
      UiTooltip,
    },
    
    • 1
    • 2
    • 3

    3、使用

    <el-select v-model="params.desc" placeholder="请选择" clearable>
      <el-option
        v-for="(item, index) in list"
        :key="index"
        :label="item.label"
        :value="item.value"
      >
        <span class="ui-tooltip-item">
          <ui-tooltip :content="item.label"></ui-tooltip>
        </span>
      </el-option>
    </el-select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    通过 max-width 设置最大宽度

    .ui-tooltip-item {
      display: inline-block;
      max-width: 150px;
      margin-left: 8px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4、效果

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    竞赛 基于情感分析的网络舆情热点分析系统
    小团队管理的艺术:实现1+1>2的协同效能
    OkHttpClient请求方式详解,及常用OkHttpUtils配置工具类
    工业自动化应用智能制造技术有哪些作用?
    Grid网格布局
    羽夏看Linux内核——段相关入门知识
    【uniapp】解决h5在ios safari浏览器tabBar抖动问题
    熊市下 DeFi 的未来趋势
    【C++】日期类的实现
    LINUX挂载共享盘
  • 原文地址:https://blog.csdn.net/qq_45268602/article/details/125534102