zoukankan      html  css  js  c++  java
  • Vue + element控制鼠标右键菜单

    1、在页面元素绑定contextmenu事件

    元素中使用@contextmenu.prevent.native="openMenu($event)"绑定事件

    <template>
      <span size="medium" @contextmenu.prevent.native="openMenu($event)" />
    </template>

    2、在页面编写右键菜单内容

    <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
          <li>历史记录</li>
     </ul>

    3、在data()中定义需要的变量属性

    data() { 
        return {
                visible: false,
                top: 0,
                left: 0
        }
    }

    4、监控visible的变化,来触发关闭右键菜单,调用关闭菜单的方法

    watch: {
        visible(value) {
          if (value) {
            document.body.addEventListener('click', this.closeMenu)
          } else {
            document.body.removeEventListener('click', this.closeMenu)
          }
        }
      },

    5、在method中定义openMenu、closeMenu的两个方法

    openMenu(e) {
          const menuMinWidth = 105
          const offsetLeft = this.$el.getBoundingClientRect().left // container margin left
          const offsetWidth = this.$el.offsetWidth // container width
          const maxLeft = offsetWidth - menuMinWidth // left boundary
          const left = e.clientX - offsetLeft // 15: margin right
    
          if (left > maxLeft) {
            this.left = maxLeft
          } else {
            this.left = left
          }
    
          this.top = e.clientY - 60 // fix 位置bug
          this.visible = true
        },
        closeMenu() {
          this.visible = false
        }
  • 相关阅读:
    .net core ELK
    mongodb基础
    .net core FluentValidation
    使用Identity Server 4建立Authorization Server
    .net core JWT应用
    .net core action过滤器的普通应用
    matplotlib
    python-13:文件操作 之二
    python-13:文件操作 open
    python-12:内置 函数之一 filter map sorted reduce
  • 原文地址:https://www.cnblogs.com/gaofz/p/11995001.html
Copyright © 2011-2022 走看看