div
은 onmousemove
이고 커서를 따라 객체를 만드는 데 사용합니다. 이 내에서 div
div
은 onmouseleave
입니다. 문제는 onmouseleave
이 매우 빠르게 개체에서 커서를 이동하는 경우에만 작동한다는 것입니다. 그러나 onmousemove
을 제거하면 onmouseleave
이 올바르게 작동하기 시작합니다. onmousemove
과 onmouseleave
을 동시에 작동 시키려면 어떻게해야합니까?onmouseleave는 onmousemove 내에서 작동하지 않습니다.
html로 :
<body>
<div onmousemove="cursorMove(event)" id="main">
<p id="title">...</p>
<div onmouseleave="gameOver()" id="light"></div>
<div id="cursor"></div>
</div>
<button onclick="moveLight()">move</button>
<button onclick="startGame()">start</button>
<script src="js/javascript.js"></script>
</body>
자바 스크립트
function cursorMove(event) {
document.getElementById("cursor").style.top = event.clientY - 14;
document.getElementById("cursor").style.left = event.clientX - 14;
document.getElementById("cursor").style.opacity = '1';
}
function gameOver() {
console.log("Game Over");
document.getElementById("light").style.top = 245 + 'px';
document.getElementById("light").style.left = 238 + 'px';
document.getElementById("title").style.opacity = '1';
document.getElementById("title").innerHTML = 'Enter the light';
gameActive = false;
}
인라인 이벤트를 addEventListner로 바꾸려고했지만 불행히도 아무런 차이가 없었습니다. –
예를 수정했습니다 : https://jsfiddle.net/4hkkm7k5/1/ –