当前位置:网站首页>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;
}
})
边栏推荐
- 二舅的外甥和他的学生们
- 美团到餐“祖传数仓”标准化治理笔记
- 笔试缺考者入围教师招聘面试?河南祥符:个别考生成绩统计错误
- 信通院陈屹力:降本增效是云原生应用最大价值
- 交换机和路由器技术-02-以太网交换机工作原理
- Design details of hidden iframe and form elements used by SAP ui5 fileuploader
- Help, boost and take responsibility, the new value and significance of the 6th Tuba rabbit 718 national home decoration Festival
- 数据库超话(一)
- 一个端到端的基于 form 表单的文件上传程序,包含客户端和服务器端
- 成本高、落地难、见效慢,开源安全怎么办?
猜你喜欢

Gods at dusk, "cat trembles" bid farewell to the big V Era

High cost, difficult to implement, slow to take effect, what about open source security?

.net core with microservices - what is a microservice

Neural network implementation of handwritten numeral classification matlab

快解析结合华途文档加密软件

详解分布式系统的幂等

JSP自定义标签(下)

【单片机】2.1 AT89S52单片机的硬件组成

Coca Cola's primary challenge is not vitality forest

The whole process of neural network construction is finally completed! Can't you come to me~
随机推荐
Uncle's nephew and his students
Neural network implementation of handwritten numeral classification matlab
ES6 array method and pseudo array to array method
Why is domestic Xinguan oral medicine a drug for the treatment of AIDS
#夏日挑战赛#【FFH】实时聊天室之WebSocket实战
【单片机】2.1 AT89S52单片机的硬件组成
立创EDA——原理图的布局与检查(三)
Microsoft silently donated $10000 to curl, which was not notified until half a year later
likeshop外卖点餐系统「100%开源无加密」
Chery omenda is also too similar to Chang'an uni-t, but does it look like it? Is the product power like it?
(2)融合cbam的two-stream项目搭建----数据准备
微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists
公网域名如何解析到内网IP服务器——快解析域名映射外网访问
【obs】x264_ encoder_ Encode encoding output PTS DTS and framesize
数据库超话(四)
帮扶、提振、担当,第六届土巴兔718全民家装节的新价值和意义
C语言怎么学?这篇文章给你完整答案
Lichuang EDA - PCB layout (IV)
【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)
Database hyperphone (II)