P粉2264132562023-09-05 00:28:54
If you want to animate a square on a horizontal circle and have it face the viewer , you can use a 3D transform on the wrapper element and invert it on the square so that it stays For viewers.
The point is to rotate the element on the Y axis like "in real life".
Here is an example:
.wrap { width: 100px; height: 100px; margin: 50px auto 50px auto; animation: roll 2s linear infinite; transform-origin: 50% 50% -250px; transform-style: preserve-3d; } .roll { width: inherit; height: inherit; background: red; animation: roll 2s linear infinite reverse; transform-origin: 50% 50% 0; } @keyframes roll { from { transform: perspective(1200px) rotateY(0deg);} to { transform: perspective(1200px) rotateY(-360deg);} }
<div class="wrap"> <div class="roll"></div> </div>
Please note that you need to use transform-style:preserve-3d;
(More information about MDN)