vue自定义鼠标右键组件

效果

2022_04_21_153505

全局屏蔽右键功能

  • App.vue
mounted(){
    
    window.addEventListener('contextmenu', (e)=>{
      e.preventDefault();
    })
  }

右键菜单组件

<template>
  <div
    class="showRightMenu"
    ref="showRightMenu"
  >
    <div
      v-for="item in menuList"
      :key="item.key"
      class="common dele"
      @click="clickSingle(item)"
    > {{item.className}}</div>
  </div>
</template>

<script>
import Vue from 'vue'
export default {
  data () {
    return {
      
    }
  },
  methods: {
    changeCSS (prop) {
      $(this.$el).css({ ...prop })
    },
    close () {
      this.changeCSS({ 'z-index': -1 })
    },
    show (e) {
      let blockWidth = this.$el.offsetWidth
      let blockHeight = this.$el.offsetWidth

      let windowWidth = $(window).width()
      let windowHeight = $(window).height()

      let posLeft = 0
      let posTop = 0

      if (e.pageX + blockWidth - windowWidth < 0) {
        // 正常情况
        posLeft = e.pageX
      } else {
        posLeft = e.pageX - (e.pageX + blockWidth - windowWidth) - 8
      }
      if (e.pageY + blockHeight - windowHeight < 0) {
        // 正常情况
        posTop = e.pageY
      } else {
        posTop = e.pageY - (e.pageY + blockHeight - windowHeight) - 8
      }

      this.changeCSS({ 'z-index': 3, left: posLeft, top: posTop })
    },
    clickSingle (item) {
      this.$emit('change', item)
    },
    regiter (component, props) {
      //component 参数为组件名
      var instance = new Vue({
        el: document.createElement("div"),
        render: (createElement) => {
          return createElement(component, {
            ...props
          })
        },
      });
      document.body.appendChild(instance.$el);
      return instance.$children[0]
    }
  },
  props: {
    menuList: {
      type: Array
    }
  },
  mounted () {
    window.addEventListener('click', (e) => {
      if (!this.$el.contains(e.target)) {
        this.close()
      }
    })
    window.addEventListener('resize', () => {
      this.changeCSS({ 'z-index': -1 })
    })
  },
  components: {

  }
}
</script>

<style scoped lang="less">
.showRightMenu {
  padding: 10px 0;
  min-width: 108px;
  cursor: pointer;
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
  box-sizing: border-box;
  background: linear-gradient(0deg, #343438, #343438), #ffffff;
  border: 1px solid #505050;
  border-radius: 4px;
  .common {
    width: 100%;
    color: #fff;
    font-family: "Helvetica Neue";
    font-style: normal;
    font-weight: 400;
    font-size: 12px;
    line-height: 20px;
    padding: 3px 19px;
    box-sizing: border-box;
    transition: all 0.3s;
    background-color: transparent;
    &:hover {
      background-color: #8372ff;
    }
  }
}
</style>

需要开启自定义右键的注册contextmenu事件,并将

<div
 @contextmenu="contextmenu($event)"
>
</div>


<script>
import showRightMenu from "./components/showRightMenu.vue"

data () {
    return {
     refShowRightMenu: null
    }
  },  
methods: {
  contextmenu (e, item) {
   e.preventDefault(); // 阻止默认事件
    this.refShowRightMenu.show(e) // 显示自定义组件
    }
}

mounted(){
// 注册组件并传递参数props和emit事件等
this.refShowRightMenu = showRightMenu.methods.regiter(showRightMenu, {
     props: {menuList: this.menuList},
     ref: 'showRightMenu',
     on: {
     change: this.rightSelectChange
   }
 })
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Vue3中的v-contextmenu指令来实现在arco-design表格组件内部自定义菜单,并且位置固定在点击范围。 首先,在arco-design表格组件上绑定v-contextmenu指令,并且传入一个方法名,该方法名用于在点击时触发显示自定义菜单的操作。例如: ```html <template> <div> <AcoTable v-bind="$props" v-contextmenu="showContextMenu"></AcoTable> </div> </template> <script> export default { props: { // arco-design表格组件的props }, methods: { showContextMenu(e) { // 显示自定义菜单的操作 } } } </script> ``` 然后,在该组件内部定义定义菜单的模板,例如: ```html <template id="my-context-menu"> <div class="my-context-menu"> <ul> <li>菜单项1</li> <li>菜单项2</li> <li>菜单项3</li> </ul> </div> </template> ``` 最后,在showContextMenu方法中,根据鼠标点击的位置动态计算出自定义菜单的位置,并且使用Vue3中的teleport组件将自定义菜单渲染到body元素下。例如: ```javascript showContextMenu(e) { e.preventDefault(); // 阻止原本的菜单弹出 const contextMenu = document.querySelector('#my-context-menu'); const { clientX, clientY } = e; contextMenu.style.left = `${clientX}px`; contextMenu.style.top = `${clientY}px`; const portal = document.createElement('div'); document.body.appendChild(portal); createVNode(Teleport, { to: 'body' }, [ createVNode(contextMenu.content.cloneNode(true)) ]).mount(portal); } ``` 在以上代码中,使用document.querySelector方法获取自定义菜单的模板,然后根据鼠标点击的位置动态计算出自定义菜单的位置,并且使用Vue3中的teleport组件将自定义菜单渲染到body元素下。 最后,您需要在CSS中定义定义菜单的样式,使其固定在点击范围。例如: ```css .my-context-menu { position: fixed; z-index: 9999; background-color: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); } ``` 通过以上步骤,您就可以在arco-design表格组件内部实现自定义菜单,并且位置固定在点击范围了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值