나는 게임을 만들고 있는데 canvas 대신 올바른 div를 사용하고 있습니다. 올바른 div는 한 div가 항상 다른 div의 안에 있어야합니다. 이런 식으로 테스트 스크립트를 만들 때이 문제가 발생했습니다. 한 사업부가 다른 사업부 외부에 있는지 어떻게 알 수 있습니까?
마우스가 큰 녹색 사업부 위에있을 때 코드는 마우스 X와 마우스 Y를 감지
var mousex, mousey;
function mousepos(e){
mousex = e.clientX;
mousey = e.clientY;
document.getElementById("xy").innerText = mousex + " , " + mousey;
}
function testdiv(){
document.getElementById("testdiv").style.left = mousex;
document.getElementById("testdiv").style.top = mousey;
setTimeout(testdiv, 30);
}
#city{
border: 1px dotted white;
background-color: lightgreen;
height: 500;
width: 500;
resize: both;
overflow: hidden;
max-height: 500px;
max-width: 500px;
min-height: 100px;
min-width: 100px;
}
#xy{
color: white;
}
#testdiv{
background-color: white;
position: absolute;
width: 100px;
height: 100px;
}
#body{
background-color: black;
}
<body id="body" onload="testdiv()">
<center>
<div id="city" onmousemove="mousepos(event);">
<div id="testdiv"></div>
</div>
<p id="xy"></p>
</center>
</body>
이 문제를 해결할 수있는 방법이 있습니까?
당신은 부모 요소에 Element.getBoundingClientRect()를 호출하고 mousex/쥐의 너비/높이 – jcubic
다음 왼쪽/상단보다 더 적은 경우에 확인하실 수 있습니다 https://codepen.io/piotrazsko/pen/VrrZBq –
재귀 사용을 권장하지 않습니다. 고성능을 위해 루프를 사용하십시오. –