案例:自定义一个右键菜单

(另一种笔记里找)

自定义右键菜单

    - contextmenu 右键菜单事件

    - e.preventDefault()/return false 阻止默认事件

    - e.clientX/e.clientY 鼠标位置获取

 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>自定义右键</title>

    <style>

        body {

            width: 100vw;

            height: 100vh;

            background: url(./imgs/bg.png) no-repeat;

            background-size: 100% 100%;

            position: relative;

        }

        #list {

            margin: 0;

            padding: 0;

            list-style: none;

            width: 180px;

            height: 200px;

            position: fixed;

            background-color: #fff;

            display: none;

        }

        #logo {

            width: 200px;

            height: 48px;

            background: url(./imgs/logo.png) no-repeat;

            margin: 50px auto;

            position: absolute;

            left: 50%;

            margin-left: -100px;

            top: 50px;

        }

        #list li {

            width: 100%;

            height: 50px;

            line-height: 50px;

            text-indent: 56px;

            color: #f5726c;

        }

        #list li:nth-child(1) {

            background: url(./imgs/download.png) no-repeat 20px 16px;

        }

        #list li:nth-child(2) {

            background: url(./imgs/remove-icon.png) no-repeat 20px 16px;

        }

        #list li:nth-child(3) {

            background: url(./imgs/move-icon.png) no-repeat 20px 15px;

        }

        #list li:nth-child(4) {

            background: url(./imgs/rename-icon.png) no-repeat 20px 14px;

        }

        #list li:hover {

            background-color: #fde9e9;

            color: #e6423c;

        }

    </style>

</head>

<body>

<div id="logo"></div>

<ul id="list">

    <li>下载</li>

    <li>删除</li>

    <li>移动到</li>

    <li>重命名</li>

</ul>

<script src="./fns.js"></script>

<script>

{

    let list = document.querySelector("#list");

    // contextmenu 右键菜单事件

    document.addEventListener("contextmenu",(e)=>{

        /*

            e.clientX/e.clientY 相对于可视区的鼠标位置

            e.pageX/e.pageY 相对于页面的鼠标位置

        */

        css(list,"left",e.clientX);

        css(list,"top",e.clientY);

        css(list,"display","block");

        e.preventDefault();

    });

    document.addEventListener("click",()=>{

        css(list,"display","none");

    });

}    

</script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值