鼠标跟随案例

该代码示例展示了如何使用HTML、CSS和JavaScript创建一个动态效果,当鼠标悬停在头像上时,会显示一个介绍框,且介绍框随着鼠标的移动而移动。CSS用于设置样式和定位,JavaScript处理鼠标移入、移出和移动事件,动态改变介绍框的位置。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 100px;
            background-color: greenyellow;
            position: relative;
            margin: 50px;
        }
        #box p{
            width: 300px;
            height: 200px;
            background-color: aquamarine;
            position: absolute;
            left: 100px;
            top: 100px;
            display: none;

            /* 鼠标来到这个P标签上也不会触发事件---穿透 */
            pointer-events: none;

            /* 设置层级 */
            z-index: 100;
        }
    </style>
</head>
<body>
    <div id="box">
        你的头像
        <p>
            你的介绍
        </p>
    </div>

    <div>
        111111
    </div>

    <script>
        box.onmouseover = function(){
            // console.log(this.firstElementChild)
            this.firstElementChild.style.display = "block"
        }
        box.onmouseout = function(){
            // console.log(this.firstElementChild)
            this.firstElementChild.style.display = "none"
        }
        box.onmousemove = function(event){
            // console.log(event.offsetX,event.offsetY)
            // this.firstElementChild.style.left = event.offsetX+50+"px"
            // this.firstElementChild.style.top = event.offsetY+50+"px"
            this.firstElementChild.style.left = event.offsetX+"px"
            this.firstElementChild.style.top = event.offsetY+"px"
        }

    </script>
</body>
</html>

效果显示:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值