达拉~ 2022-04-21 17:40 采纳率: 64.7%
浏览 13
已结题

当鼠标设置在子节点内部时父节点 mouseout 鼠标移出事件失效

需求是鼠标移入盒子,遮罩层出现,移出消失,这都没有问题,但是当鼠标出现在遮罩层的中间时,鼠标就可以带着遮罩层出去大盒子了,不受mouseout这个事件的影响

    <script>
        var box = document.querySelector('.box')
        var mark = document.querySelector('.mark')

        box.addEventListener('mouseover', function () {
            mark.style.display = 'block'
        })

        box.addEventListener('mouseout', function () {
            mark.style.display = 'none'
        })
        box.addEventListener('mousemove', function (e) {
            // 计算出鼠标在盒子内的坐标
            x = e.pageX - this.offsetLeft
            y = e.pageY - this.offsetTop
            mark.style.left = x - mark.offsetWidth / 2 + 'px'
            mark.style.top = y - mark.offsetHeight / 2 + 'px'
            // console.log(x - mark.offsetWidth / 2);
        })
    </script>


```html
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            float: left;
            position: relative;
            width: 400px;
            height: 240px;
            border: 1px solid black;
        }

        .mark {
            width: 100px;
            height: 100px;
            z-index: 99;
            cursor: move;
            position: absolute;
            background-color: rgba(18, 56, 210, 0.5);
            display: none;
            
        }

        #img {
            position: absolute;
        }

        #yl {
            float: left;
            margin-left: 20px;
            width: 400px;
            height: 400px;
            overflow: hidden;
            position: relative;
        }

        #ylimg {
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="./20211213_170451.jpg" width="400px" id="img" alt="">
        <div class="mark"></div>
    </div>

```

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月29日
    • 创建了问题 4月21日

    悬赏问题

    • ¥50 如何将脑的图像投影到颅骨上
    • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
    • ¥15 netcore使用PuppeteerSharp截图
    • ¥20 这张图页脚具体代码该怎么写?
    • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
    • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
    • ¥15 数字信号处理考试111
    • ¥15 allegro17.2生成bom表是空白的
    • ¥15 请问一下怎么打通CAN通讯
    • ¥20 如何在 rocky9.4 部署 CDH6.3.2?