내 코드에 몇 가지 문제점이 있습니다. 기본적으로 좌표계에는 빨간 공과 div 상자가 있습니다.트리거 된 이벤트의 요소를 툴팁으로 따라 가려면 어떻게해야합니까?
문서를 클릭하면 div.ball이 원하는 위치로 이동하지만 상자는 해당 위치에 있습니다. 두 번째 클릭 후, 공은해야만하는 위치로 이동하고 상자는 공의 이전 위치로 이동합니다. 상자는 동일한 위치를 두 번 클릭해야하는 위치로 이동합니다.
상자가 공 옆에 나타날 때마다 툴팁처럼 나타나길 원합니다.
나를 도와 줄 수 있습니까?
function menuRoll(){
var hidden_collapse = document.getElementById("hidden_collapse");
hidden_collapse.classList.toggle("myStyle");
}
function readPosition(ball){
var rect = ball.getBoundingClientRect();
var x = Math.round(rect.left) + "px";
var y = Math.round(rect.top) + "px";
coordinat.innerHTML = "Coordinates:"+ "<br>" + x + ", " + y;
coordinat.style.top = Math.round(rect.top) - 30 + "px";
coordinat.style.left = Math.round(rect.left) + 37 + "px";
}
window.onload = function(){
var ball = document.getElementById('foo');
var ball_space = document.getElementById('ball_space');
var coordinat = document.getElementById("coordinat");
document.addEventListener('click', function(e){
ball.style.background = "#B40520";
var ball_H = ball.offsetHeight;
var ball_W = ball.offsetWidth;
readPosition(ball);
ball.style.transform = 'translateY('+(e.clientY-(0.5*ball_H))+'px)';
ball.style.transform += 'translateX('+(e.clientX-(0.5*ball_W))+'px)';
}, true);
};
#coordinat {
transition: all 1s;
border-radius: 5px;
position: absolute;
text-align: center;
right: 5px;
top: 5px;
width: 150px;
height: 30px;
padding: 5px 5px;
background-color: black;
color: white;
font-size: 0.8em;
}
#foo{
margin: 2px;
border-radius: 50px;
width: 30px;
height: 30px;
/*background: #B40520;*/
background: white;
position: absolute;
top: 0px;
left: 0px;
-moz-transition: transform 1s, background 1s;
-webkit-transition: transform 1s, background 1s;
-ms-transition: transform 1s, background 1s;
-o-transition: transform 1s, background 1s;
transition: transform 1s, background 1s;
}
<div id="coordinat">Coordinates you will see!</div>
<div id="ball_space">
<div id="foo"></div>
</div>
. 빨간 공 바로 위에 툴팁을 항상 표시하고 싶습니까? 그렇다면 툴팁이 아닌 볼의 클릭 시간에 툴팁의 좌표를 얻는 이유는 무엇입니까? –
빨간 공이 항상 상자에 들어가기를 바란다. 따라서 클릭 기능에서 코드의 일부를 꺼내야합니까? – Shoux
솔루션을 찾을 수 있습니다. https://jsfiddle.net/kris_IV/e8fugw9w/2/ 괜찮 으면 대답으로 답합니다. –