当前位置:网站首页>JS to realize the right-click menu bar function
JS to realize the right-click menu bar function
2022-07-27 17:48:00 【Can stars count human beings】
1. design sketch :

Mouse hover :
2. Code
(1)html
<div class="leftDiv">
<!-- Right click div-->
<div id="rightClickDiv">
<ul id="rightClickUl">
<li class="createLi rightClickLi liHover"><span class="liContent createSpan"> New folder </span></li>
<li class="addProduct rightClickLi liHover"><span class="liContent addProductSpan"> Add product </span></li>
<li class="addWp rightClickLi liHover"><span class="liContent addWpSpan"> Add type </span></li>
<li class="addPg rightClickLi liHover"><span class="liContent addPgSpan"> Add code </span></li>
<hr class="splitLine" />
<li class="deleteLi rightClickLi liHover"><span class="liContent deleteSpan"> Delete </span></li>
<li class="renameLi rightClickLi liHover"><span class="liContent renameSpan"> rename </span></li>
</ul>
</div>
</div>
(2)css
#rightClickDiv{
display: none;
position: absolute;
float: left;
width: 9.5vw;
height: 25vh;
border: 1px solid #d8d8d8;
box-shadow:3px 3px 5px #dadada;
z-index: 10001;
background-color: white;
}
ul#rightClickUl {
position: relative;
width: 100%;
height: 96%;
top: 2%;
font-size: 0.77vw;
color: #525252;
cursor: default;
}
#rightClickUl> li{
position: relative;
margin-top: 2%;
height: 3vh;
font-size: 0.77vw;
line-height: 1.5vw;
}
.liHover:hover{
/* Mouse hover style */
background-color: #e1e1e1;
}
.dLi{
/* The file color changes when access is prohibited */
color: #bababa;
}
.liContent{
position: relative;
left: 21%;
}
(3)js
/** * Disable right mouse button */
$('.leftDiv').on('contextmenu', function () {
return false;
});
/** * Target area click event */
$('.leftDiv').on('mousedown', function (event) {
var code = event.which;
switch (code) {
case 1:
// left-click
console.log(event);
// Judge sub elements
if (event.target.className.indexOf('addProductSpan') >-1 || event.target.className.indexOf('addProduct')>-1) {
// Add product
console.log(" Add product ");
} else if (event.target.className.indexOf('addWpSpan') >-1 || event.target.className.indexOf('addWp')>-1){
// Add part
console.log(" Add part ");
} else if (event.target.className.indexOf('addPgSpan') >-1 || event.target.className.indexOf('addPg')>-1){
// Add program
console.log(" Add program ");
} else if (event.target.className.indexOf('createLi') >-1 || event.target.className.indexOf('createSpan')>-1) {
// Add folder
console.log(" Add folder ");
} else if (event.target.className.indexOf('deleteLi') >-1 || event.target.className.indexOf('deleteSpan')>-1){
// Delete
if (!$(".deleteLi").hasClass('dLi')){
console.log(" Delete ");
}
} else if (event.target.className.indexOf('renameLi') >-1 || event.target.className.indexOf('renameSpan')>-1){
// rename
if (!$(".renameLi").hasClass('dLi')){
console.log(" rename ");
}
} else if (event.target.className.indexOf('ztree') > -1){
$('#rightClickDiv').hide();
}
break;
case 2: mouse = " In the key ( Roller )";
break;
case 3: mouse = " Right click ";
// Click on the blank space
console.log(event);
if (event.target.className.indexOf("ztree")>-1){
// Change content Click on the blank page
$("#rightClickUl>li:not(.createLi)").removeClass("liHover").addClass("dLi");
} else if (event.target.className.indexOf("ico_docu")>-1 || event.target.className.indexOf("node_name") > -1){
// Change content Click product package
$(".addProduct").addClass("liHover").removeClass("dLi");
$("#rightClickUl>li:not(.addProduct)").removeClass("liHover").addClass("dLi");
}
var x = event.offsetX;
var y = event.offsetY;
// If the click position is div On the right side , Adjust the display position
if (event.offsetX + $("#rightClickDiv").width() > $(this)[0].clientWidth) {
x = $(this)[0].clientWidth - $("#rightClickDiv").width();
}
if (event.offsetY + $("#rightClickDiv").height() > $(this)[0].clientHeight) {
y = $(this)[0].clientHeight - $("#rightClickDiv").height();
}
// Change the position of the menu to the position of the event
$('#rightClickDiv').css({
'top': y,'left': x,'display': 'block'});
break;
default: break;
}
})
边栏推荐
- 公网域名如何解析到内网IP服务器——快解析域名映射外网访问
- Branch loop statement of C language
- Taishan Office Technology Lecture: word strange paragraph borders
- 腾讯云上传使用
- Switch and router technology-02-working principle of Ethernet switch
- Chery omenda is also too similar to Chang'an uni-t, but does it look like it? Is the product power like it?
- Switch and router technology-03-basic configuration of switch
- Dense optical flow extraction dense_ Flow understanding
- 记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析
- [MCU] 2.2 pin function of AT89S52
猜你喜欢

诸神黄昏,“猫抖快”告别大V时代

Lichuang EDA - PCB layout (IV)

立创EDA——原理图的布局与检查(三)

Today's sleep quality record 82 points

一个端到端的基于 form 表单的文件上传程序,包含客户端和服务器端

An end-to-end file upload program based on form, including client and server

Mlx90640 infrared thermal imager temperature sensor module development notes (VII)

Kubernetes Part 7: using kubernetes to deploy prometheus+grafana monitoring system (kubernetes work practice class)

kubernetes 1.24高可用集群二进制部署

信号量保护之位带操作
随机推荐
小于n的最大数
Subject 3: straight driving
大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?
奇瑞欧萌达也太像长安UNI-T了,但长得像,产品力就像吗?
Oracle-Linux-7.9是否能支持Oracle-19c的ACFS文件系统?
密集光流提取dense_flow理解
阿里巴巴鹰眼系统简介
[MCU] 2.2 pin function of AT89S52
Database hyperphone (I)
Kubernetes Part 7: using kubernetes to deploy prometheus+grafana monitoring system (kubernetes work practice class)
公网域名如何解析到内网IP服务器——快解析域名映射外网访问
微软默默给 curl 捐赠一万美元,半年后才通知
Chen Yili of ICT Institute: reducing cost and increasing efficiency is the greatest value of cloud native applications
神经网络实现手写数字分类matlab
C语言怎么学?这篇文章给你完整答案
Functions of C language
如何限制root远程登入,使普通用户拥有root权限
Zhengzhou University database course resource description
【Codeforces】 A. Computer Game
Establishing SSL connection without server‘s identity verification is not recommended