一、自定义鼠标右键的实现原理
TIM图片20180911164755.png自定义右键菜单功能,需清楚,所有浏览器都自带了右键功能,那么自定义右键菜单,需要先屏蔽系统自带的右键功能,如何实现呢?
$selector.on('contextmenu', '.box', function () {
return false;
})
注:$selector为选择器,.box为禁用鼠标右键的class
完成系统自带鼠标右键的禁用后,开始自定义鼠标右键。
获取鼠标事件的方法,使用mousedown,获取后需区分是鼠标左键还是右键,事件返回的参数event中,event.which为3表示右键,为1表示左键,为2表示中键。如下:
$("#box").mousedown(function(e){
if(3 === e.which){
alert("这 是右键单击事件");
}else if(1 === e.which){
alert("这 是左键单击事件");
} else if (2 === e.which) {
alert("这是鼠标中键");
}
})
二、自定义鼠标右键实例(表格行右键自定菜单)
html:
<div class="box">
<div class="table-list">
<div class="table-thead ">
<ul class="table-col">
<li class="save-item" title="名称">
<span>名称</span>
</li>
<li class="save-item" title="类型">
<span>类型</span>
</li>
<li class="save-item" title="数据">
<span>数据</span>
</li>
<li class="save-item">
<span>操作</span>
</li>
</ul>
</div>
<div class="table-tbody">
<ul class="table-col saveCol">
<li class="save-item" title="test1">
<span>test1</span>
</li>
<li class="save-item" title="test2">
<span>test2</span>
</li>
<li class="save-item" title="test3">
<span>test3</span>
</li>
<li class="save-item">
<i class="point-icon opratePointDrop" data-toggle="close"></i>
<ul class="oprate-dropdown opDropDown none">
<li class="op-item btn-mySaveRun">
<a href="javascript:;">
<span>运行</span>
</a>
</li>
<li class="op-item btn-mySaveRename">
<a href="javascript:;">
<span>编辑</span>
</a>
</li>
<li class="op-item btn-mySaveEdit">
<a href="javascript:;">
<span>刷新</span>
</a>
</li>
<li class="op-item btn-mySaveDelete">
<a href="javascript:;">
<span>导出</span>
</a>
</li>
</ul>
</li>
</ul>
<ul class="table-col saveCol">
<li class="save-item" title="test1">
<span>test1</span>
</li>
<li class="save-item" title="test2">
<span>test2</span>
</li>
<li class="save-item" title="test3">
<span>test3</span>
</li>
<li class="save-item">
<i class="point-icon opratePointDrop" data-toggle="close"></i>
<ul class="oprate-dropdown opDropDown none">
<li class="op-item btn-mySaveRun">
<a href="javascript:;">
<span>运行</span>
</a>
</li>
<li class="op-item btn-mySaveRename">
<a href="javascript:;">
<span>编辑</span>
</a>
</li>
<li class="op-item btn-mySaveEdit">
<a href="javascript:;">
<span>刷新</span>
</a>
</li>
<li class="op-item btn-mySaveDelete">
<a href="javascript:;">
<span>导出</span>
</a>
</li>
</ul>
</li>
</ul>
<ul class="table-col saveCol">
<li class="save-item" title="test1">
<span>test1</span>
</li>
<li class="save-item" title="test2">
<span>test2</span>
</li>
<li class="save-item" title="test3">
<span>test3</span>
</li>
<li class="save-item">
<i class="point-icon opratePointDrop" data-toggle="close"></i>
<ul class="oprate-dropdown opDropDown none">
<li class="op-item btn-mySaveRun">
<a href="javascript:;">
<span>运行</span>
</a>
</li>
<li class="op-item btn-mySaveRename">
<a href="javascript:;">
<span>编辑</span>
</a>
</li>
<li class="op-item btn-mySaveEdit">
<a href="javascript:;">
<span>刷新</span>
</a>
</li>
<li class="op-item btn-mySaveDelete">
<a href="javascript:;">
<span>导出</span>
</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
css:
* {
border: none;
margin: 0;
padding: 0;
}
ul, ul li, ol li, li {
list-style: none;
}
a {
text-decoration: none;
}
.none {
display: none;
}
.box {
width: 1000px;
margin: 20px 50px;
}
/* 自定义table */
.table-list {
width: 100%;
}
.table-list .table-tbody {
position: relative;
max-height: 350px;
min-height: 225px;
}
.table-list .table-thead ul {
background-color: #f5f5f5;
}
.table-thead > ul li {
font-weight: bold !important;
}
.table-tbody > ul {
margin-top: -1px;
position: relative;
cursor: pointer;
}
.table-tbody > ul:hover {
background-color: #f5f5f5;
}
.table-thead, .table-tbody {
width: 100%;
}
.table-thead > ul, .table-tbody > ul{
display: flex;
}
.table-thead > ul > li, .table-tbody > ul > li {
flex: 1;
height: 36px;
font: normal 12px/36px 'Microsoft YaHei', 'Arial';
border: 1px solid #d9d9d9;
border-left: none;
}
.table-thead > ul > li:first-child, .table-tbody > ul > li:first-child {
border-left: 1px solid #d9d9d9;
}
.table-thead > ul > li > span, .table-tbody > ul > li > span {
width: 90%;
display: inline-block;
margin: 0 10px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
/* 下拉 */
.oprate-dropdown {
position: absolute;
top: 28px;
right: 3px;
width: 90px;
border: 1px solid #d9d9d9;
box-shadow: 0 0 5px #ccc;
z-index: 100;
/* 禁用选择字体 */
-moz-user-select: none; /*火狐*/
-webkit-user-select: none; /*webkit浏览器*/
-ms-user-select: none; /*IE10*/
user-select: none;
}
.oprate-dropdown li.op-item {
width: 100%;
height: 32px;
background-color: #fff;
}
.oprate-dropdown li.op-item:hover {
background-color: #f2f3f6;
}
.oprate-dropdown li.op-item a {
display: inline-block;
width: 70px;
line-height: 32px;
border-bottom: 1px solid #d9d9d9;
color: #2a313f;
margin: 0 10px;
overflow: hidden;
}
.oprate-dropdown li.op-item .op-icon {
position: relative;
left: 0;
top: 5px;
display: inline-block;
width: 15px;
height: 20px;
}
.point-icon {
display: inline-block;
width: 20px;
height: 15px;
background: url(../image/point-icon.png) no-repeat;
background-position: 0 10px;
cursor: pointer;
margin-left: 10px;
}
js:
var _rightMenu = {
init: function () {
var _this = this;
_this.rightMenuOprate();
},
rightMenuOprate: function () {
var $searchPart = $('.box');
//操作点击下拉
$searchPart.on('click', '.opratePointDrop', function (e) {
e.stopPropagation();
var $this = $(this),
_isOpen = $this.attr('data-toggle'),
$dropDown = $this.next();
//清空所有显示下拉
$searchPart.find('.opDropDown').each(function () {
var $this = $(this);
$this.hide();
$this.prev().attr('data-toggle', 'close');
});
if (_isOpen === 'close') {
$dropDown.show().css('left', '');
$this.attr('data-toggle', 'open')
} else if (_isOpen === 'open') {
$dropDown.hide();
$this.attr('data-toggle', 'close')
}
});
//禁止鼠标右键事件
$searchPart.on('contextmenu', '.saveCol', function () {
return false;
});
//鼠标右键
$searchPart.on('mousedown', '.saveCol', function (e) {
e.stopPropagation();
var $this = $(this);
if (3 === e.which) {
//隐藏其他展开的下拉
$searchPart.find('.opDropDown').each(function () {
var $this = $(this);
$this.hide();
$this.prev().attr('data-toggle', 'close');
});
$this.find('.opDropDown').show().css({
'left': e.pageX - 50 + 'px'
});
}
});
//点击清除
$(document).click(function (e) {
//兼容火狐
if (1 === e.which) {
$searchPart.find('.opratePointDrop').attr('data-toggle', 'close');
$searchPart.find('.opDropDown').hide();
}
});
}
};
$(function () {
_rightMenu.init();
});
网友评论