el-popover气泡宽度由内容撑起

前言

  • el-popover有个width属性,就算不填也有默认值150。想要让气泡内容撑起气泡宽度要特殊处理
  • 以下内容由Trae改造生成

方案

html 复制代码
<template>
  <el-popover
    placement="top"
    :width="0"
    effect="light"
    popper-class="auto-fit-popover"
    v-model:visible="popoverShow"
  >
    <div class="chat-popover-options">
      <div
        class="ctrl-item"
        @click="fuc1()"
      >
        功能1
      </div>
    </div>
    <template #reference>
      <button @click="popoverShow = true"></button>
    </template>
  </el-popover>
</template>

<style>
.auto-fit-popover {
  min-width: unset !important;
  width: auto !important;
  white-space: nowrap;
}
</style>

实现原理

  • :width="0" :通过设置宽度为 0,让 Element Plus 的 popover 组件不使用默认的固定宽度
  • !important :使用 !important 来覆盖 Element Plus 的默认样式,确保我们的样式能够生效
  • white-space: nowrap; :确保内容不会换行,这样气泡框的宽度就会根据内容的实际宽度来调整
相关推荐
猫腻前端2 小时前
深度图d3绘制交互逻辑
前端
angerdream2 小时前
最新版vue3+TypeScript开发入门到实战教程之Pinia详解
前端·javascript·vue.js
没想好d2 小时前
通用管理后台组件库-14-图表和富文本组件
前端
siger2 小时前
前端部署缓存策略实践
前端·nginx
Mh2 小时前
react 设计哲学 | 严格模式
前端·react.js·preact
怜悯2 小时前
uniapp 如何实现google登录-安卓端
前端·javascript
TT_哲哲2 小时前
小程序解析字符串拼接多图 点击放大展示
前端·javascript
一颗奇趣蛋2 小时前
Cursor 多项目搜索指南
前端
Jolyne_2 小时前
Taro小程序接入微信客服过程记录
前端