중첩을 계산하는 로직은 잘못된 것이다. 이것은 올바른 하나입니다
(rect1.right > rect2.left && rect2.right > rect1.left) &&
(rect1.bottom > rect2.top && rect2.bottom > rect1.top)
그리고 여기에 (그냥 실행하고 X/Y 값으로 재생) 을이를 찾는 예입니다 :
var interval1 = setInterval(function() {
\t var rect1 = document.querySelector('.rect1');
\t var rect2 = document.querySelector('.rect2');
\t
\t (function updatePosition() {
\t \t rect1.style.left = document.querySelector('.rect1inputLeft').value + "px"
\t \t rect1.style.top = document.querySelector('.rect1inputTop').value + "px";
\t \t rect2.style.left = document.querySelector('.rect2inputLeft').value + "px";
\t \t rect2.style.top = document.querySelector('.rect2inputTop').value + "px";
\t })();
\t
\t (function updateRectangleValues() {
\t \t var rect1rectangle = rect1.getBoundingClientRect();
\t \t var rect2rectangle = rect2.getBoundingClientRect();
\t \t
\t \t var newrect1 = '<span style="position: absolute; top: 2px; left: 50%; transform: translateX(-50%);">' + rect1rectangle.top + "</span>"
\t \t \t + '<span style="position: absolute; top: 50%; right: 2px; transform: translateY(-50%);">' + rect1rectangle.right + "</span>"
\t \t \t + '<span style="position: absolute; top: 50%; left: 2px; transform: translateY(-50%);">' + rect1rectangle.left + "</span>"
\t \t \t + '<span style="position: absolute; bottom: 2px; left: 50%; transform: translateX(-50%);">' + rect1rectangle.bottom + "</span>";
\t \t var newrect2 = '<span style="position: absolute; top: 2px; left: 50%; transform: translateX(-50%);">' + rect2rectangle.top + "</span>"
\t \t \t + '<span style="position: absolute; top: 50%; right: 2px; transform: translateY(-50%);">' + rect2rectangle.right + "</span>"
\t \t \t + '<span style="position: absolute; top: 50%; left: 2px; transform: translateY(-50%);">' + rect2rectangle.left + "</span>"
\t \t \t + '<span style="position: absolute; bottom: 2px; left: 50%; transform: translateX(-50%);">' + rect2rectangle.bottom + "</span>";
\t \t \t \t \t \t
\t \t if(rect1.innerHTML !== newrect1) {
\t \t \t rect1.innerHTML = newrect1;
\t \t }
\t \t if(rect2.innerHTML !== newrect2) {
\t \t \t rect2.innerHTML = newrect2;
\t \t }
\t \t (function checkIfOverlapping() {
\t \t \t document.querySelector('.overlapping').checked = ((rect1rectangle.right > rect2rectangle.left && rect2rectangle.right > rect1rectangle.left) && (rect1rectangle.bottom > rect2rectangle.top && rect2rectangle.bottom > rect1rectangle.top));
\t \t })();
\t })();
\t
}, 50);
body {
margin: 0;
padding: 0;
}
.rect {
width: 100px;
height: 80px;
position: absolute;
opacity: 0.7;
color: white;
}
.rect1 {
background: green;
top: 0;
left: 0;
}
.rect2 {
background: blue;
top: 0;
left: 0;
}
.rect1input {
background: green;
color: white;
border: 1px solid black;
}
.rect2input {
background: blue;
color: white;
border: 1px solid black;
}
.rect1input, .rect2input {
width: 50px;
height: 20px;
padding-left: 3px;
margin-top: 5px;
}
<div class="rect rect1"></div>
<div class="rect rect2"></div>
x <input type="number" class="rect1input rect1inputLeft" value="100">
y <input type="number" class="rect1input rect1inputTop" value="60">
x <input type="number" class="rect2input rect2inputLeft" value="100">
y <input type="number" class="rect2input rect2inputTop" value="132">
overlapping? <input type="checkbox" class="overlapping" disabled>
코드가 불완전하거나 실제 코드를 나타내지 않아서 어디에도 정의되어 있지 않은 londonClass를 사용하고 있습니다. 문제를 설명하기 위해 jsFiddle을 설정하는 것이 좋습니다. 또한 실제로 발생하는 것을 추적하기 위해 코드의 모든 관련 위치에'console.log' 문을 추가하는 것을 주저하지 마십시오 (또는 선호하는 브라우저의 Javascript 디버거 사용). – jcaron
중복 논리가 잘못되었습니다. 종이와 연필로 더 많은 시간을 보내십시오. –