实现类似于鼠标右键相同的右键事件


实现类似于鼠标右键相同的右键事件

实现原理:禁止默认事件的鼠标右键功能,自定义一个右键事件,新的右键事件弹出框的位置跟随鼠标的位置即可。并且给鼠标左键点击事件增加一个消除自定义右键事件即可。

<style type="text/css">
    .rightBox{
        position: absolute;
        display: none;
        width: 100px;
        background-color: red;
        text-align: center;
    }
</style>
<div class="rightBox" id="rightBox">
    <div>aaa</div>
    <div>bbb</div>
    <div>ccc</div>
</div>
<script type="text/javascript">
var rightBox = document.getElementById('rightBox');
document.oncontextmenu = function(ev){
    var oEvent = ev || window.event;

    rightBox.style.display = "block";

    rightBox.style.left = oEvent.clientX + 'px';
    rightBox.style.top = oEvent.clientY + 'px';
    return false;
}

document.onclick = function(ev){
    var oEvent = ev || window.event;
    rightBox.style.display = "none";
}
</script>

转载自:https://blog.csdn.net/u012832088/article/details/79804609

You may also like...