私のコードにいくつか問題があります。基本的には、赤い球と座標のdivボックスがあります。トリガイベントの要素にツールチップを従う方法
文書をクリックすると、div.ballは私がしたいところに行きますが、ボックスはその位置にとどまります。 2回目のクリック後、ボールはどこに移動し、ボックスはボールの前の位置に移動します。ボックスは、同じ位置をダブルクリックした後に移動します。
ボールの横のたびに表示される、ツールチップのような形になります。
あなたは私を助けることができますか?
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/ 私は答えとしてそれを入れます。 –