I am attempting to calculate the distance between two dynamically moving HTML elements. Currently, I have them moving on hover, but the result does not change as they move.
What could be causing this issue?
Below is my code:
var lFirst = $("#x").offset().left;
var lSecond = $("#y").offset().left;
var ldist = parseInt(lFirst - lSecond);
var tFirst = $("#x").offset().top;
var tSecond = $("#y").offset().top;
var tdist = parseInt(tFirst - tSecond);
$('#result').append(parseInt(tdist + ldist));
html, body{
margin:0;
padding:0;
}
*{
transition:all 1s;
}
#x, #y{
width:50px;
height:50px;
margin-left:0;
margin-top:0;
background:black;
}
#container{
height:100vh;
width:100vw;
background:lightgrey;
}
#container:hover #x{
margin-left:50vw;
}
#container:hover #y{
margin-top:50vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="result"></div>
<div id="x"></div>
<div id="y"></div>
</div>