[el-tag]使用多个el-tag,自动判断内容是否超出

项目中有需求显示el-tag标签 内容超出需要显示省略号并且点击省略显示剩余标签

复制代码
<template>
  <div ref="tagContainer" class="tag-list">
    <!-- 显示的标签 -->
    <el-tag
      v-for="(tag, index) in visibleTags"
      :key="index"
      class="tag-item"
      @click.stop
    >
      {{ tag }}
    </el-tag>

    <!-- 如果有更多标签,则显示省略按钮和 Popover -->
    <el-popover
      v-if="showPopover"
      placement="top"
      trigger="click"
      width="300px"
    >
      <template #reference>
        <el-tag class="more-tag"><moreFilled /></el-tag>
      </template>
      <div class="flex flex-wrap gap-[4px] w-full">
        <el-tag
          v-for="(tag, index) in hiddenTags"
          :key="index"
          class="popover-tag"
        >
          {{ tag }}
        </el-tag>
      </div>
    </el-popover>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, onUnmounted } from "vue";
import moreFilled from "~icons/ep/more-filled";

// 定义 Props
const props = defineProps({
  tags: {
    type: Array<any>,
    default: () => [] as any[]
  }
});

// DOM 引用
const tagContainer = ref<HTMLDivElement | null>(null);

// 是否显示 Popover
const showPopover = ref(false);

// 可见标签和隐藏标签
const visibleTags = ref<string[]>([]);
const hiddenTags = ref<string[]>([]);

// 计算可见标签和隐藏标签
const calculateTags = () => {
  if (!tagContainer.value) return;

  const containerWidth = tagContainer.value.offsetWidth;

  let totalWidth = 0;

  visibleTags.value = [];
  hiddenTags.value = [];

  props.tags.forEach(tag => {
    const tempSpan = document.createElement("span");
    tempSpan.style.position = "absolute";
    tempSpan.style.visibility = "hidden";
    tempSpan.style.whiteSpace = "nowrap";
    tempSpan.innerText = tag;
    document.body.appendChild(tempSpan);
    const tagWidth = tempSpan.offsetWidth + 22; // 加上一些 padding  gap-4 和tag内置的padding9 9 + 9 + 4
    document.body.removeChild(tempSpan);
    if (totalWidth + tagWidth <= containerWidth) {
      visibleTags.value.push(tag);
      totalWidth += tagWidth;
    } else {
      hiddenTags.value.push(tag);
    }
  });

  showPopover.value = hiddenTags.value.length > 0;
};

// 监听窗口大小变化重新计算
onMounted(() => {
  calculateTags();
  window.addEventListener("resize", calculateTags);
});

// 组件卸载时移除事件监听
onUnmounted(() => {
  window.removeEventListener("resize", calculateTags);
});
</script>

<style lang="scss" scoped>
.tag-list {
  display: flex;
  gap: 4px;
  align-items: center;
  overflow: hidden;
}

.tag-item {
  white-space: nowrap;
}

.more-tag {
  cursor: pointer;
}

.popover-tag {
  max-width: 100%; // 设置最大宽度,防止无限延伸
  height: auto;
  line-height: 24px;
  word-break: break-all; // 强制单词内换行
  white-space: normal; // 允许换行
}
</style>
相关推荐
弓弧名家_玄真君几秒前
在ubuntu中安装redis
前端·bootstrap·mybatis
RFCEO2 分钟前
学习前端编程:DOM 树、CSSOM 树、渲染树详解
前端·学习·渲染树·dom 树·cssom 树·浏览器的渲染流程·回流/重绘
笨蛋不要掉眼泪4 分钟前
Redis主从复制:原理、配置与实战演示
前端·redis·bootstrap·html
bigdata-rookie8 分钟前
Starrocks 数据模型
java·前端·javascript
白帽子凯哥哥10 分钟前
网络安全Web基础完全指南:从小白到入门安全测试
前端·sql·web安全·信息安全·渗透测试·漏洞
RFCEO16 分钟前
前端编程 课程十四、:CSS核心基础2:选择器优先级 + 伪类选择器(解决冲突+交互效果)
前端·css·交互·css选择器优先级判断规则详解·css important使用·css链接伪类lvha顺序·实现悬浮交互效果
web打印社区18 分钟前
前端实现浏览器预览打印:从原生方案到专业工具
前端·javascript·vue.js·electron
yuezhilangniao21 分钟前
# 告别乱码:用FastAPI特性与Next.js打造类型安全的API通信
javascript·安全·fastapi
jiayong2330 分钟前
Vue2 与 Vue3 生态系统及工程化对比 - 面试宝典
vue.js·面试·职场和发展
徐同保31 分钟前
vue.config.ts配置代理解决跨域,配置开发环境开启source-map
前端·javascript·vue.js