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();
        });
      });
    },
相关推荐
阿蒙Amon4 小时前
TypeScript学习-第7章:泛型(Generic)
javascript·学习·typescript
睡美人的小仙女1274 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
fanruitian4 小时前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo4 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk4 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
摘星编程5 小时前
React Native + OpenHarmony:Timeline垂直时间轴
javascript·react native·react.js
2501_944525546 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
jin1233226 小时前
React Native鸿蒙跨平台完成剧本杀组队详情页面,可以复用桌游、团建、赛事等各类组队详情页开发
javascript·react native·react.js·ecmascript·harmonyos
李白你好6 小时前
Burp Suite插件用于自动检测Web应用程序中的未授权访问漏洞
前端
经年未远7 小时前
vue3中实现耳机和扬声器切换方案
javascript·学习·vue