I want to move the three dots consecutively
but they don't move vertically, I managed to move them horizontally
I found what I want but with css https://codepen.io/mattonit/pen/vLoddq
can it with javascript?
let rootElements = document.querySelectorAll("div.dot");
let i = 0;
setInterval(() => {
i++;
if (i % 10 > 2) {
i = 0;
}
rootElements[i % 10].style.margin = "0px";
for (let j = 0; j < rootElements.length; j++) {
if (j !== i) {
rootElements[j].style.margin = "-1px";
}
}
}, 500);
.dot {
display: inline-block;
margin: -1px;
}
<div>
<div class="dot">⚫</div>
<div class="dot">⚫</div>
<div class="dot">⚫</div>
</div>
It would only be enough to remove the property of margin -1 px and set it as auto margin so that both vertical and horizontal elements and the display are accommodated instead of behaving in line as in block
looking like this:
Remaining the following result
I don't think they move, rather they change color.
I have made some changes to your code:
i
is calculated from the number of elements ofrootElements
(in the original code we are doing modulo 10 and then comparing with 2, when in this particular case a modulus of 3 or the number of elements ofrootElements
would be more convenient).top
. That way when one of the circles moves, the other circles don't move (or not an unexpected move).transition
, so with JavaScript, taking into account that we know which element is active (i
), all we have to do is remove the styles (so that it returns to the initial state), advance the value dei
and give the styles we want to the next element to move. Eliminating the loopfor
in the code that doesn't seem to be really necessary.Here you can see a demo:
The animation is not exactly like the one on Facebook (which looks like the one you are trying to replicate) because there is no pause at the end or the dots are never all 3 moving (both of which could be achieved with a second function and using
setTimeout
different times in place ofsetInterval
).Personally, if the animation you're after is always the same, I'd use an animated gif. You save code and problems.
If you fancy doing it with JS, put each point in a different object with the CSS position:relative property and play around with its top property.
Something like this: