I have some problems with my code. Basically, I have a red ball and a div box for coordinates.
When I click on a document div.ball goes where I want to, but the box stays at its position. After 2nd click, ball goes where it should and the box moves to the previous position of the ball. The box goes where it should after double click on the same position.
I want the box be like a tooltip, appearing every time next to the ball.
Can you guys help me?
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>