Skip to content

Instantly share code, notes, and snippets.

@rslahmed
Last active March 15, 2020 08:37
Show Gist options
  • Save rslahmed/8a2d1d88e84a7420f245d0a5b32b9935 to your computer and use it in GitHub Desktop.
Save rslahmed/8a2d1d88e84a7420f245d0a5b32b9935 to your computer and use it in GitHub Desktop.
Custom right click menu
//html
<ul class='custom-menu'>
<li>First thing</li>
<li>Second thing</li>
<li>Third thing</li>
</ul>
//css
/* The whole thing */
.custom-menu {
display: none;
z-index: 1000;
position: absolute;
overflow: hidden;
border: 1px solid #CCC;
white-space: nowrap;
font-family: sans-serif;
background: #FFF;
color: #333;
border-radius: 5px;
padding: 0;
}
/* Each of the items in the list */
.custom-menu li {
padding: 8px 12px;
cursor: pointer;
list-style-type: none;
transition: all .3s ease;
user-select: none;
}
.custom-menu li:hover {
background-color: #DEF;
}
//js
// Trigger action when the contexmenu is about to be shown
$('.card-body').on("contextmenu", function (event) {
// Avoid the real one
event.preventDefault();
// Show contextmenu
$(".custom-menu").finish().toggle(100).
// In the right position (the mouse)
css({
top: event.pageY + "px",
left: event.pageX + "px"
});
});
// If the document is clicked somewhere
$(document).bind("mousedown", function (e) {
// If the clicked element is not the menu
if (!$(e.target).parents(".custom-menu").length > 0) {
// Hide it
$(".custom-menu").hide(100);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment