vue-context-menu-实现鼠标右键事件

第一步:安装vue-context-menu

npm install vue-contextmenu --save

第二步:在main.js中引用

import VueContextMenu from 'vue-contextmenu'
Vue.use(VueContextMenu)

第三步:给需要点击的元素绑定事件

<div @contextmenu.native="contextmenu" >右键点击我弹出菜单</div>

这是点击过后显示的菜单

<vue-context-menu
      :contextMenuData="contextMenuData"<!--菜单绑定的-->
      @delRight="delRight"
      @delLeft="delLeft"
      @delOhter="delOhter"
      @closeAll="closeAll"
    ></vue-context-menu>

第四步:用事件做功能

 contextmenu(e) {//右键单击事件
      e.preventDefault();
      var x = e.clientX;
      var y = e.clientY;
      // Get the current location
      this.contextMenuData.axis = {
        x,
        y
      };
    },

第五步:菜单数据初始化,事件调用

data:{
contextMenuData: {
        // the contextmenu name(@1.4.1 updated)
        menuName: "demo",
        // The coordinates of the display(菜单显示的位置)
        axis: {
          x: null,
          y: null
        },
        // Menu options (菜单选项)
        menulists: [
          {
            fnHandler: "delRight", // Binding events(绑定事件)
            icoName: "fa fa-home fa-fw", // icon (icon图标 )
            btnName: "关闭右侧" // The name of the menu option (菜单名称)
          },
          {
            fnHandler: "delLeft",
            icoName: "fa fa-home fa-fw",
            btnName: "关闭左侧"
          },
          {
            fnHandler: "delOhter",
            icoName: "fa fa-home fa-fw",
            btnName: "关闭其他"
          },
          {
            fnHandler: "closeAll",
            icoName: "fa fa-home fa-fw",
            btnName: "关闭全部"
          }
        ]
      }
}
methods:{
delRight() {
      //关闭右侧
     alert('关闭右侧')
    },
    delLeft() {
      //关闭左侧
     alert('关闭左侧')
    },
    closeAll() {
      //关闭所有
     alert('关闭所有')
    },
    delOhter() {
      //关闭其他
     alert('关闭其他')
    }```
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值