<!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>
.box {
display: flex;
}
.box1 {
width: 20px;
height: 20px;
background: mediumaquamarine;
}
.box2 {
width: 100px;
height: 100px;
background: mediumorchid;
margin-left: 10px;
display: none;
}
</style>
</head>
<body>
<div class="box">
<div class="box1"></div>
<div class="box2"></div>
</div>
<script>
const box1 = document.getElementsByClassName("box1")[0]
const box2 = document.getElementsByClassName("box2")[0]
let timer = null
box1.onmouseenter = function () {
timer && clearTimeout(timer)
box2.style.display = "block"
}
box1.onmouseleave = function () {
box2.onmousemove = () => {
clearTimeout(timer)
}
box2.onmouseleave = function () {
box2.style.display = "none"
}
const timer = setTimeout(() => {
box2.style.display = "none"
}, 500);
}
</script>
</body>
</html>