el-popover随点击位置变化

在使用 Element UI时,el-popover 组件默认是绑定到一个特定的触发元素上的,并且会在该触发元素的附近显示。然而,如果你希望 el-popover 能够根据用户的点击位置动态显示,(如表格中的每一项),需要进行一些自定义处理。

1. 创建一个 Vue 组件

在这个组件中,我们将处理点击事件并更新 el-popover 的位置。

html 复制代码
    <!-- 触发 Popover 的按钮(可选) -->
    <el-button @click="clickPop('button',$event)">点击</el-button>
    
    <!-- Popover 组件 -->
    <el-popover
        v-if="showPop"
        ref="pop"
        :reference="reference"
        placement="right"
        :offset="200"
        trigger="click"
    >
      <p>我是悬浮框</p>
    </el-popover>

2. 点击时,更新 Popover 的位置

根据点击事件的位置,更新 Popover 的显示位置。

javascript 复制代码
// data
data() {
    return {
      reference: {},
      // 控制渲染条件 如果不用v-if会报错 具体可自行尝试
      showPop: false,
      // 保存当前激活的refrence id
      activeItem: null,
    };
  },

点击事件处理

javascript 复制代码
// 点击显示Pop
    clickPop(item, event) {
      // 这个操作是为了避免与源码中的点击reference doToggle方法冲突
      if (this.activeItem === item && this.showPop) return;
      this.showPop = false;
      this.activeItem = item;
      // 设置reference 
      this.reference = event.target;

      this.$nextTick(() => {
        // 等待显示的popover销毁后再 重新渲染新的popover
        this.showPop = true;
        this.$nextTick(() => {
          // 此时才能获取refs引用
          this.$refs.pop.doShow();
        });
      });
    },
相关推荐
钱栈up4 分钟前
热榜上手|HyperFrames:写HTML出MP4
前端·html
Sailing15 分钟前
🚀🚀【封闭结束8】Agent RAG:从零搭建一个可用知识库
前端·后端·agent
Captain1314_李祖团22 分钟前
5. MLIR相关语法
java·前端·javascript
齐 飞41 分钟前
VUE2常用语法总结
javascript·vue.js
恋猫de小郭1 小时前
Flutter 可以更直接地调用原生 API 了,Dart Direct Native Interop
android·前端·flutter
Data analyse4561 小时前
web-vitals库接入:LCP、INP、CLS上报与阈值
前端·javascript·数据分析
北城笑笑1 小时前
Vue 106 ,上篇:前端桌面端应用实现方案( Electron 框架详细介绍,Vue + Electron )
前端·javascript·vue.js·python·electron
柳杉1 小时前
别再手抠特效帧了:一个能"看图改图"的像素特效库 + Claude Code Skill
前端·javascript
前端小D1 小时前
React Native项目开发(iOS侧为例)
javascript·react native·react.js