代码拉取完成,页面将自动刷新
Demo: npm run dev 本项目即可
npm i ja-contextmenu
import ContextMenu from 'ja-contextmenu';
const contextMenu = new ContextMenu();
const menuOption = {
item:[
{ label:'go', onclick(e, payload){...} }
]
};
const menu = contextMenu.create(menuOption);
window.addEventListener('contextmenu', e => { menu.show(e, payload) });
// async create menu
const menu2 = contextMenu.createAsync(menuOption);
window.addEventListener('click', e => { menu2().show(e, payload) });
安装后请把package.json 中ja-contextmenu 的版本号前的"^"删除,防止npm的预料之外的自动更新。(例: "ja-contextmenu":"^
1.3.0" => "ja-contextmenu":"1.3.0")
精力有限,不保证小版本更新时,不改动使用方式。
import ContextMenu, { h } from 'ja-contextmenu'; // types.d.ts supported
// import ContextMenu from 'ja-contextmenu/src/index.ts'
const contextMenu = new ContextMenu({
width: 200, // default: 200
fixMenuWhenScroll: false, // 滚动时菜单是否固定(position:fixed) default:false
hideMenuWhenScroll: true // 滚动时是否关闭菜单,default:true
});
const menuOption = {
items: [
{
label: 'menu1', // 选项名称
icon: './assets/images/ico.png', // icon url | HTMLElement
class: 'customClass', // 选项自定义class, default: ''
tip: 'tip1', // 选项右侧提示文字, default: ''
show: true, // 是否展示, default: true
disabled: false, // 是否禁用选项, default: false
onclick(e, payload) {
// payload 为调用menu.show方法传入的参数
console.log('menu1 click', payload);
// return true; // 点击不关闭菜单
},
},
{ type: '---' }, // 分割线
{
// 支持选项内容根据payload变动
label: payload => 'menu2',
icon: payload => 'icon href2',
class: payload => 'class2',
tip: payload => 'tip2',
show: payload => true,
disabled: payload => true
children: {
width: 120,// 不传则继承父菜单宽度
items: [
{
label: 'sub menu1',
onclick: (e, payload) => {
console.log('sub menu1 click', payload)
}
},{
class: 'li-class-name',
// 自定义选项内容
customItem: document.createElement('div')
},{
// 我封装了createElement的函数h
customItem: h('div',[
h('span', {
// {[element.key]:value}
textContent: 'hello', // element.textContent = 'hello'
style:{
fontWeight:'bolder'.// element.style.fontWeight = 'holder'
cssText: 'font-size:14px;' // element.style.cssText = 'font-size:14px;'
},
className:'class-name',
}),
h('span.class-name#id',' world')
])
}
]
}
},
],
}
let menu = contextMenu.create(menuOption);
document.body.oncontextmenu = (e) => {
let payload = 'payload data: callback when click items';
menu.show(e, payload);
};
// or
someButton.onclick = (e) => {
menu.show(e);
}
// menu.hide(); // 隐藏
// menu.destroy(); // 销毁
// menu = null;
new ContextMenu(option:
ContextMenuOption
);
key: type | default | desc |
---|---|---|
width: number | 200 | Menu width |
fixMenuWhenScroll: boolean | false | 滚动时菜单是否固定(hideMenuWhenScroll=false) |
hideMenuWhenScroll: boolean | true | 滚动时是否关闭菜单 |
MenuOption
): MenuWrapper
创建一个菜单,返回一个MenuWrapper对象
param: type | default | desc |
---|---|---|
width?: number | 200 | 菜单宽度,子菜单不配置,则继承父菜单宽度 |
class?: string|(payload)=>string | Menu ul class | |
items: MenuItemOption
|
列表配置项 |
param: type | default | desc |
---|---|---|
icon?: string|HTMLElement|(payload)=>string|HTMLElement | 图标icon url | |
class?: string|(payload)=>string | 菜单项li class | |
label?: string|(payload)=>string | 选项文字 | |
tip?: string|(payload)=>string | 选项右侧提示文字 | |
show?: boolean|(payload)=>boolean | true | 是否展示 |
disabled?: boolean|(payload)=>boolean | false | 是否禁用 |
type?: MenuItemType
|
取值 '---' | 'hr' => <hr> 分割线 | |
customItem?: HTMLElement
|
自定义菜单项 | |
onclick?: function(event, payload):boolean | 点击事件回调,参数payload为调用showMenu时传入的参数.return true 则点击后不关闭菜单. | |
children?: MenuOption
|
子菜单配置 |
const menu:MenuWrapper = contextMenu.create<Payload>(...)
展示菜单。
PointerEvent
, MouseEvent
, T extends { x: number, y: number }import ContextMenu from 'ja-contextmenu';
const contextMenu = new ContextMenu();
// 泛型 - PayloadType
const menu = contextMenu.create<number>({
width: 100,
items: [
{
label: 'label',
onclick(e, payload:number) { // type
console.log(payload);
},
},
],
});
menu.show({x: 100,y:100}, 1) // payload type :number
//menu.show({x: 100,y:100}, '2') // payload type error not number
/test
目录下。此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。