任意位置生成右键自定义菜单插件context.js之API与调用

6 篇文章 0 订阅
5 篇文章 0 订阅
jQuery轻量级右键菜单插件context.js,支持Bootstrap,可以选择使用或不使用Bootstrap,支持递归子菜单以及
横线间隔菜单,支持动态添加/删除菜单,支持菜单链接,样子还不错,推荐使用。
jQuery轻量级右键菜单插件context.js

使用方法:
1.加载插件和jQuery
1 <script src="js/jquery.min.js"></script>
2 <script src="js/context.js"></script>  
3 <link rel="stylesheet" type="text/css" href="css/context.standalone.css">

2.初始化
01 <script>
02 $(document).ready(function(){  
03     context.init({preventDoubleContext: false});
04     context.settings({compress: true});
05     context.attach('html', [//attach为绑定的dom对象,可以使用类名或id,例如'.classname'
06         {header: 'Compressed Menu'},
07         {text: 'Back', href: '#'},
08         {text: 'Reload', href: '#'},
09         {divider: true},
10         {text: 'Save As', href: '#'},
11         {text: 'Print', href: '#'},
12         {text: 'View Page Source', href: '#'},
13         {text: 'View Page Info', href: '#'},
14         {divider: true},
15         {text: 'Inspect Element', href: '#'},
16         {divider: true},
17         {text: 'Disable This Menu', action: function(e){
18             e.preventDefault();
19             context.destroy('html');
20             alert('html contextual menu destroyed!');
21         }},
22         {text: 'Donate?', action: function(e){
23             e.preventDefault();
24             $('#donate').submit();
25         }}
26     ]);
27 });
28 </script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以帮你写一个用Vue 3编写的自定义右键菜单组件。以下是一个简单的示例: 1. 首先,创建一个名为`ContextMenu.vue`的组件文件。 ```vue <template> <div class="context-menu" :style="{ top: `${top}px`, left: `${left}px` }"> <ul> <li v-for="(item, index) in items" :key="index" @click="handleItemClick(item.action)"> {{ item.label }} </li> </ul> </div> </template> <script> export default { props: { items: { type: Array, required: true }, top: { type: Number, required: true }, left: { type: Number, required: true } }, methods: { handleItemClick(action) { // 执行相应的操作 if (action) { this[action](); } }, // 示例操作函数 exampleAction() { console.log("执行了示例操作"); } } }; </script> <style scoped> .context-menu { position: fixed; background-color: #fff; border: 1px solid #ccc; padding: 8px; } </style> ``` 2. 在需要使用右键菜单的地方,引入并使用`ContextMenu`组件。 ```vue <template> <div class="container" @contextmenu.prevent="showContextMenu($event)"> 右键点击此处显示菜单 <ContextMenu v-if="showMenu" :items="menuItems" :top="menuTop" :left="menuLeft" /> </div> </template> <script> import ContextMenu from "@/components/ContextMenu.vue"; export default { components: { ContextMenu }, data() { return { showMenu: false, menuItems: [ { label: "菜单项1", action: "exampleAction" }, { label: "菜单项2", action: "exampleAction" }, { label: "菜单项3", action: "exampleAction" } ], menuTop: 0, menuLeft: 0 }; }, methods: { showContextMenu(event) { event.preventDefault(); this.showMenu = true; this.menuTop = event.clientY; this.menuLeft = event.clientX; }, exampleAction() { console.log("执行了示例操作"); } } }; </script> <style scoped> .container { width: 200px; height: 200px; background-color: lightgray; } </style> ``` 这个示例中,我们创建了一个`ContextMenu`组件,它接收一个菜单项数组和菜单显示的位置作为props。当右键点击触发`@contextmenu.prevent`事件时,显示右键菜单,并根据鼠标位置设置菜单的top和left样式。 请注意,这只是一个简单的示例,你可以根据自己的需求对菜单样式和行为进行进一步的定制。希望对你有所帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值