js右键菜单

父级页面

<!--
 * @LastEditTime: 2024-08-05 22:30:42
-->
<template>
  <div class="content">
    <span
      class="content-menu"
      :style="{
        background:item.check?'yellow':'',
        color:item.check?'black':'',

      }"
      :id="index"
      v-for="(item,index) in listone"
      :key="item.label"
    >{{item.label}}</span>
    <ContextMenu
      v-if="showMenu"
      class="block"
      :x="x"
      :y="y"
      :menu="list"
      @select="choose3 = $event.label"
    ></ContextMenu>
  </div>
</template>
<script>
import $ from "jquery"
import ContextMenu from './components/ContextMenu.vue'
export default {
  components: {
    ContextMenu: ContextMenu,
  },
  data() {
    return {
      listone: [
        {
          label: '数据一',
          check: false,
        },
        {
          label: '数据二',
          check: false,

        },
        {
          label: '数据三',
          check: false,

        }
      ],
      list: [
        {
          label: '选择'
        },
        {
          label: '删除'
        },
        {
          label: '移动'
        }
      ],
      showMenu: false,
      shiftKey: false,
      x: '',
      y: '',
    }
  },
  mounted() {
    document.addEventListener('keydown', (event) => {
      if (event.key === 'Shift') {
        console.log('Shift 键被按下了!');
        this.shiftKey = true
      }
    });

    document.addEventListener('keyup', (event) => {
      if (event.key === 'Shift') {
        this.shiftKey = false
        console.log('Shift 键被松开了!');
        // 执行其他逻辑
      }
    });
    // keyboardJS.bind('shift', (e) => {
    //   this.shiftKey = true
    //   console.log('Shift 键被按下了!');
    // }, (e) => {
    //   this.shiftKey = false
    //   console.log('Shift 键被松开了!');
    // });


    // 添加右击事件监听器到整个文档
    document.addEventListener('contextmenu', (event) => {
      event.preventDefault();
      event.stopPropagation(); // 阻止冒泡
      var targetElement = event.target;
      var classNames = targetElement.className;
      if (classNames == "content-menu") {
        this.x = event.clientX;
        this.y = event.clientY;
        this.showMenu = true;
      } else {
        if (this.showMenu) {
          this.showMenu = false;
        }
      }
    });

    // 添加点击事件监听器到整个文档,用于隐藏菜单
    document.addEventListener('click', (event) => {
      event.preventDefault();
      event.stopPropagation(); // 阻止冒泡
      var targetElement = event.target;
      var classNames = targetElement.className;
      if (this.showMenu) {
        this.showMenu = false;
      }
      if (classNames == "content-menu") {
        if (!this.shiftKey) {  //判断shift状态
          this.listone.forEach(v => {
            if (v.check) {
              v.check = false
            }
          })
        }
        this.listone[$(targetElement).attr('id')].check = true
        this.$forceUpdate()
      } else {
        this.listone.forEach(v => {
          if (v.check) {
            v.check = false
          }
        })
      }

    });
  }
}
</script>
<style lang="less">
.content {
  width: 100%;
  height: 100%;
  // border: 1px solid red;
  background: blue;
  span {
    display: block;
    width: 80px;
    height: 50px;
    line-height: 50px;
    background: red;
    border-radius: 5px;
    margin: 5px;
    text-align: center;
    color: white;
    font-size: 19px;
  }
}
</style>

二级页面

<!--
 * @LastEditTime: 2024-08-05 21:56:55
-->
<template>
  <div
    class="context-menu"
    ref="containerRef"
    :style="{
        left: x + 'px',
        top: y + 'px',
      }"
  >
    <div class="menu-list">
      <div
        @click="handleClick(item)"
        class="menu-item"
        v-for="(item, i) in menu"
        :key="i"
      >{{ item.label }}</div>
    </div>
  </div>
</template>

<script >
export default {
  props: ["menu", "x", "y"],
  methods: {
    handleClick() {
      this.$emit("select")
    }
  },
}
</script>
<style lang="less" scoped>
.option {
  width: 150px;
}
.context-menu {
  position: fixed;
  min-width: 150px;
  box-sizing: border-box;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-radius: 8px;
  background: white;
  .menu-item {
    cursor: pointer;
    border-radius: 4px;
    padding: 5px;
  }
  .menu-item:hover {
    cursor: pointer;
    background: rgb(52, 119, 216);
    color: white;
  }
}
</style>

根目录 菜单菜单二 go 修改 删除 function showMenu(id){ menuForm.id.value = id; if("" == id){ } else{ popMenu(itemMenu,100,"111"); } event.returnValue=false; event.cancelBubble=true; return false; } /** *显示弹出菜单 *menuDiv:右键菜单的内容 *width:行显示的宽度 *rowControlString:行控制字符串,0表示不显示,1表示显示,如“101”,则表示第1、3行显示,第2行不显示 */ function popMenu(menuDiv,width,rowControlString){ //创建弹出菜单 var pop=window.createPopup(); //设置弹出菜单的内容 pop.document.body.innerHTML=menuDiv.innerHTML; var rowObjs=pop.document.body.all[0].rows; //获得弹出菜单的行数 var rowCount=rowObjs.length; //循环设置每行的属性 for(var i=0;i<rowObjs.length;i++) { //如果设置该行不显示,则行数减一 var hide=rowControlString.charAt(i)!='1'; if(hide){ rowCount--; } //设置是否显示该行 rowObjs[i].style.display=(hide)?"none":""; //设置鼠标滑入该行时的效果 rowObjs[i].cells[0].onmouseover=function(){ this.style.background="#818181"; this.style.color="white"; } //设置鼠标滑出该行时的效果 rowObjs[i].cells[0].onmouseout=function(){ this.style.background="#cccccc"; this.style.color="black"; } } //屏蔽菜单菜单 pop.document.oncontextmenu=function(){ return false; } //选择右键菜单的一项后,菜单隐藏 pop.document.onclick=function(){ pop.hide(); } //显示菜单 pop.show(event.clientX-1,event.clientY,width,rowCount*25,document.body); return true; } function create(){ alert("create" + menuForm.id.value + "!"); } function update(){ alert("update" + menuForm.id.value + "!"); } function del(){ alert("delete" + menuForm.id.value + "!"); } function clickMenu(){ alert("you click a menu!"); }
JS右键菜单插件是一种可以在网页中实现自定义的右键菜单功能的插件。它可以让用户在右键单击鼠标时,弹出一个自定义的菜单,提供一些操作选项供用户选择。 这种插件通常基于JavaScript语言编写,并且需要通过引用相关的插件文件来实现。一般来说,使用这种插件的步骤包括以下几个方面: 首先,需要在网页中引用相关的插件文件。这通常是一个带有.js后缀的JavaScript文件,可以通过在网页的头部或者尾部通过`<script>`标签引用。 然后,在网页的相应位置编写JavaScript代码来实现右键菜单插件的功能。这通常包括定义菜单项的内容、设置菜单的样式、绑定右键单击事件等。 在编写JavaScript代码时,可以使用插件提供的API来实现一些常见的功能,比如添加菜单项、响应菜单项的点击事件等。 最后,经过以上步骤的设置后,当用户在网页中右键单击鼠标时,插件会生效,弹出自定义的右键菜单供用户选择。 需要注意的是,不同的插件可能有不同的使用方式和设置方法,具体的使用说明可以参考插件的文档或者使用示例。此外,还需要注意插件是否与当前网页的其他功能或者代码有冲突或者兼容性问题,以确保插件的正常运行。 总之,JS右键菜单插件是一种可以在网页中实现自定义右键菜单功能的插件,通过引用插件文件并编写相关的JavaScript代码,可以实现弹出自定义菜单并响应菜单项点击事件的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值