A contextmenu module in Javascript Vanilla. DEMO
showContextMenu(event, menu, [, options]);
element.oncontextmenu = function(event) {
showContextMenu(event, [
{
type: 'button',
text: 'click me!',
sub: [
{
type: 'button',
text: 'submenu 1',
sub: [
{
type: 'infos',
text: 'sub submenu 1'
}, {
type: 'infos',
text: 'sub submenu 2'
}
]
}
],
onclick: function(event) {
alert(event);
}
}, {
type: 'infos',
text: 'another one'
}, {
type: 'html',
text: '<div>and the last</div>'
}
], {
arrow: true,
align: 'center',
element: element,
toggleClass: element,
width: '100%',
margin: {
top: 0,
right: 0,
bottom: 0,
left: 0
}
});
};
Objects array
string or HTML code when type: 'html'
'button', 'infos' or 'html'. 'button' by default
when 'html', you can put HTML code on the text
type: 'html',
text: '<div>Hello</div>'
add a function on click
onclick: function(event) {
alert(event);
}
submenu, same syntax than menu
show an arrow, or not: true or false, false by default.
arrow: true
align the context menu horizontally: 'center', 'left', 'right' or false, false by default.
align: 'center'
align the context menu vertically relatively to an element and add 'contextmenu-active' class to him when the menu is active:
element: document.getElementById('id')
add 'contextmenu-active' class to element when the menu is active
toggleClass: document.getElementById('id')
define a width in pixel or percent for the menu
width: '55%'
width: '234px' // (width: '234' is the same)
add margin in pixel
margin: {
top: 5,
right: 0,
bottom: 5,
left: 0
}
hide all context menu
hideContextMenu()
hide submenus after a level
hideContextMenuChild(0) //hide all submenu
add multiple events with addEventListener or attachEvent
addEvents(
window, //element
'keydown resize dragover click contextmenu DOMMouseScroll wheel mousewheel touch', //events
hideContextMenu //handler
);
remove multiple events with removeEventListener or detachEvent
removeEvents(
window, //element
'keydown resize dragover click contextmenu DOMMouseScroll wheel mousewheel touch', //events
hideContextMenu //handler
);