前端 鼠标事件

本文主要探讨JavaScript中的鼠标事件,包括点击、移动、拖放等重要事件类型,以及如何在前端开发中有效利用这些事件来增强用户体验。

回顾

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 200px;
            height: 200px;
            border: 2px solid black;
        }
    </style>
</head>
<body>
    <div class="box">
        一个盒子
    </div>
    <script>
        // onclick : 事件处理函数, click:事件名称
        // 鼠标事件:
        // click 单击
        // dblclick 双击
        // mouseover 鼠标经过(当前的那个对象)
        // mouseout 鼠标离开(当前的那个对象)
        // mousedown 鼠标按下
        // mouseup 鼠标释放
        // mousemove 鼠标移动
        // oncontextmenu 鼠标右键单击

        // mouseenter 鼠标经过(范围)
        // mouseleave 鼠标离开(范围)

        var mybox = document.querySelector(".box")
        // mybox.onclick = function(){
        //     this.style.backgroundColor = "red"
        // }
        // mybox.ondblclick = function(){
        //     this.style.backgroundColor = "red"
        // }
        mybox.onmouseover = function(){
            this.style.backgroundColor = "red"
        }
        mybox.onmouseout = function(){
            this.style.backgroundColor = "blue"
        }
        mybox.onmousedown = function(){
            this.style.backgroundColor = "yellow"
            this.style.border = "5px dashed orange"
        }
        mybox.onmouseup = function(){
            this.style.border = "5px dotted pink"
        }
        // 抚摸
        mybox.onmousemove = function(){
            console.log('ok');
        }

        document.oncontextmenu = function(){
            alert("你鼠标右键单击了")
        }


    </script>
</body>
</html>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值