下拉菜单

代码实现

''''html
<!DOCTYPE html>
<html lang="zh">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定义网页右键菜单</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

    html,
    body {
        width: 100%;
        height: 100%;
    }

    body {
        position: relative;
    }

    ul {
        position: absolute;
        left: 0;
        top: 0;
        width: 200px;
        height: 300px;
        background-color: #d43d3d;
        border-radius: 8px;
        overflow: hidden;
        list-style-type: none;
        display: none;
    }

    ul li {
        height: 50px;
        width: 100%;
        cursor: pointer;
        border-bottom: solid 1px #6898f1;
        color: white;
        text-align: center;
        line-height: 50px;
        font-size: 18px;
    }

    ul li:hover {
        background-color: #da1ae8;
        color: #333;
    }
</style>

</head>

<body>
<ul class="menu" id="menu">
<li>复制</li>
<li>粘贴</li>
<li>会员充值</li>
<li>联系我们</li>
</ul>

<script>
    let menu = document.getElementById("menu")
    document.oncontextmenu = function (e) {
        e.preventDefault()
    }
    document.onmousedown = function (e) {
        if (e.button == "2") {
            menu.style.display = "block"
            menu.style.left = e.clientX + "px"
            menu.style.top = e.clientY + "px"
        }
    }
    document.onclick = function () {
        menu.style.display = "none"
    }
</script>

</body>

</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容