This is as much a math question as it is a coding question.
Picture this scenario:
___________________
| |
(#1) | Center Object | (#2)
|_________________|
I want the two circular objects to move away from the center object at the same speed but different distances, #1 moving to the left and #2 moving to the right.
The end product should look like this:
___________________
| |
(#1) | Center Object | (#2)
|_________________|
If I simply use animate({"marginLeft:-200px"},1000) and animate({"marginLeft:1000px"},1000), #2 will move much faster because the animation times are the same (1000).
Here's where a little calculus comes in. I'm not very good at math so I don't know where exactly to start with the calculation.
How would I vary the animation time based on the distance so that they move at the same speed?
speed = 0.04;
distanceTop = 150;
distanceRight = 500;
$('#top').animate({'marginTop':0},distanceTop/speed);
$('#right').animate({'marginLeft':'500px'},distanceRight/speed);
.object {
background:black;
height:50px;
margin-top:150px;
position:absolute;
width:50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div style="background:red;height:200px;position:absolute;width:200px">
<div class="object" id="top"></div>
<div class="object" id="right"></div>
</div>
As you can see in this JSFiddle, the black boxes are reaching the corners of the red box at different speeds. Excuse my bad math, but how do I make them move at equal speeds?