自定义右键菜单

在这里插入图片描述

<template>
  <div class="tree">
    <span
      class="tree-item"
      v-for="(item, index) in tree"
      :key="index"
      @contextmenu.prevent="openMenu(item, $event)">
      {{ item.name }}
    </span>
    <div class="popover" :style="style" v-if="isShowPopover">
      <div v-for="(item, index) in popoverList" :key="index" class="popover-item" @click="menuClick(item)">
        <i :class="'el-icon-' + item.type"></i>
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'NotFound',
  computed: {
    // 计算出距离
    style () {
      return {
        left: this.popoverLeft + 'px',
        top: this.popoverTop + 'px'
      }
    }
  },
  data () {
    return {
      tree: [
        { name: '一级1' },
        { name: '一级2' }
      ],
      popoverLeft: 0, // 距离左边的距离
      popoverTop: 0, // 距离顶部的距离
      isShowPopover: true, // 是否展示右键内容
      popoverList: [
        { name: '新增', type: 'circle-plus-outline' },
        { name: '编辑', type: 'edit' },
        { name: '删除', type: 'delete' }
      ]
    }
  },
  methods: {
    // 展开菜单
    openMenu (item, event, index) {
      this.curItem = item
      this.curIndex = index
      // 判断窗口大小
      const clientWidth = document.body.clientWidth
      const clientHeight = document.body.clientHeight
      // 鼠标右键边界值判断(100,178是右键菜单的宽高)
      if (event.clientX + 100 >= clientWidth) {
        this.popoverLeft = clientWidth - 100
      } else {
        this.popoverLeft = event.clientX
      }
      if (event.clientY + 178 >= clientHeight) {
        this.popoverTop = clientHeight - 178
      } else {
        this.popoverTop = event.clientY
      }
      this.isShowPopover = true // 显示自定义菜单栏
    },
    // 关闭菜单
    closeMenu () {
      this.isShowPopover = false
    },
    // 菜单某一项被点击
    menuClick (row) {
      console.log(row)
    }
  },
  watch: {
    // 目的: 为了在鼠标右键内容展示,点击其他地方隐藏鼠标右键内容
    isShowPopover (val) {
      if (val) {
        document.body.addEventListener('click', this.closeMenu)
      } else {
        document.body.removeEventListener('click', this.closeMenu)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tree {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .tree-item {
      width: 200px;
      line-height: 40px;
      border: 1px solid red;
      text-align: center;
      margin-top: 10px;
      cursor: pointer;
    }
    .popover {
      width: 100px;
      height: auto;
      position: fixed;
      background: #fff;
      border: 1px solid #ddd;
      padding: 0 10px;
      box-shadow: 0 0 2px 0 #ccc;
      .popover-item {
        border-bottom: 1px solid #efefef;
        line-height: 35px;
        text-align: center;
        cursor: pointer;
      }
      .popover-item:last-child{
        border-bottom: none;
      }
    }
  }
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值