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();
        });
      });
    },
相关推荐
niucloud-admin5 小时前
web 端前端
前端
摘星编程8 小时前
React Native for OpenHarmony 实战:Linking 链接处理详解
javascript·react native·react.js
胖者是谁8 小时前
EasyPlayerPro的使用方法
前端·javascript·css
EndingCoder8 小时前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux35288 小时前
Web集群管理实战指南:从架构到运维
运维·前端·架构
沛沛老爹9 小时前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发
摘星编程9 小时前
React Native for OpenHarmony 实战:ImageBackground 背景图片详解
javascript·react native·react.js
小光学长9 小时前
基于Web的长江游轮公共服务系统j225o57w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库
摘星编程10 小时前
React Native for OpenHarmony 实战:Alert 警告提示详解
javascript·react native·react.js
Joe55610 小时前
vue2 + antDesign 下拉框限制只能选择2个
服务器·前端·javascript