我试图以一个'摇摆'运动的元素动画,就好像它是一条船在波浪上.

JSBIN这里:https://jsbin.com/bakugifulo/edit?html,css,output

我在JSBIN中有四个彩色的例子.

(1)Pink =这是我所追求的动议.非常平稳的摇摆与放松.动画关键帧:

@keyframes rocking1 {
    0% {
        transform: rotate(-5deg);
        animation-timing-function: ease-in-out;
    }
    50% {
        transform: rotate(5deg);
        animation-timing-function: ease-in-out;
    }
    100% {
        transform: rotate(-5deg);
        animation-timing-function: ease-in-out;
    }
}

(2)Blue = The catch:由于这个动画是如何被纳入到更大的图片,我需要它做同样的事情,但不是开始旋转.意思是,我希望它从'0'旋转开始.

从理论上讲,移动关键帧%的斑点和调整缓动应该会产生相同的结果:

@keyframes rocking2 {
    0% {
        transform: rotate(0deg);
    }
    25% {
        transform: rotate(-5deg);
        animation-timing-function: ease-out;
    }
    75% {
        transform: rotate(5deg);
        animation-timing-function: ease-in-out;
    }
    100% {
        transform: rotate(0deg);
        animation-timing-function: ease-in;
    }
}

请注意,当动画从100%循环回到0%时,会有非常轻微的停顿.这就是我正在努力防止的.

(3 4)Brown Green =上述两个的重复,但使用线性而不是 slack .&&注意,绿色的没有暂停.

所以,问题似乎是我在蓝色div中的宽松逻辑.但是,我想不明白.我试过各种洗牌和交换放松选项.完全go 掉0%和100%的标记.它们都会产生同样的问题...循环的时候会有一个非常轻微的停顿

有没有办法在不停顿的情况下实现我想要的东西?或者这只是使用缓动时不可避免的(我猜第一个例子is暂停,但你只是没有注意到,因为它在它移动的方向的末尾暂停).

但我希望我只是错过了一些显而易见的东西在我的放松逻辑.

推荐答案

它可以在单个动画中完成,从"0旋转"开始,没有堆叠,没有负延迟,你已经非常接近这一点.(欢迎光临,顺便说一句!)

您只是在一帧之后设置了缓动功能,但进度(ease-out-ease-in-out-ease-in)是正确的.

在POC演示中,我将"东西"改为类似于钟摆,因为我认为它更能说明这一点:

@keyframes swing {
 00% { /* bottom -> right cusp: start full speed, end slow: */
  animation-timing-function: ease-out;
  transform: rotate(-.0turn); color: red;
 }
 25% { /* right cusp -> left cusp: start slow, end slow: */
  animation-timing-function: ease-in-out;
  transform: rotate(-.2turn); color: blue;
 }
 75% { /* left cusp -> bottom: start slow, end full speed */
  animation-timing-function: ease-in;
  transform: rotate(+.2turn); color: green;
 }
 100% { /* bottom, timing function has no effect here */
  animation-timing-function: step-end;
  transform: rotate(+.0turn); color: yellow;
 }
}

div {
 animation: swing;
 animation-duration: 3s;
 animation-timing-function: ease-in-out;
 animation-iteration-count: infinite;
 animation-direction: normal;
 animation-play-state: running;
 transform-origin: center top;
 margin: auto;
 width: 100px;
 display: flex;
 flex-direction: column;
 align-items: center;
 pointer-events: none;
 &::before, &::after { content: ''; background-color: currentcolor}
 &::before {width: 1px; height: 100px; }
 &::after{width: 50px; height: 50px; }
}

#reset:checked ~ div { animation: none; }
#pause:checked ~ div { animation-play-state: paused; }
<meta name="color-scheme" content="dark light">

<input type="checkbox" id="pause"><label for="pause"> Pause animation.</label>
<input type="checkbox" id="reset"><label for="reset"> Remove animation.</label>

<div></div>

我必须承认,我从来没有想过我们可以 for each 关键帧设置不同的计时函数,所以这样看起来很自然的多步动画,并带有"绑定"的放松类型,实际上是可以实现的.对我来说,最大的收获也是最后一个(to/100%)关键帧的放松功能在逻辑上没有任何效果.

就我个人而言,我很可能会 Select 更简洁的动画,只有两个关键帧,..direction: alternate个开始暂停,并有负延迟将其初始状态切换到中间,类似于其他答案中提出的.

Css相关问答推荐

自定义kendo色彩 Select 器中的清晰按钮

如何为垫子制作圆角- Select 所有角点上的下拉列表

更改MAT-FORM-FIELD的CSS,使其在有Angular 的HTML中显示为普通文本框

圆形按钮边框长度动画

Django:Tailwind 样式不适用于具有小部件属性的模板变量

使用 React 和 Flexbox 文本溢出框外

css ::part 伪 Select 器 Chrome 兼容性?

Angular 以Angular 获取当前聚焦的元素

条件宽度和省略号不适用于样式化组件 - React.js

我怎样才能将我的按钮向上移动,因为文本是

如何应用适当样式的元素加消息框?

如何为柔和的配色方案提供易于使用的高对比度替代方案?

宽度为 x VW 的元素似乎对窗口调整大小没有react

如何在 CSS 中控制列表样式类型光盘的大小?

定位背景图片,添加内边距

悬停时如何在图像上显示文字?

表格溢出时水平滚动

我应该如何组织我的 CSS 文件的内容?

仅针对使用的类优化 Font Awesome

如何使用纯 CSS 创建工具提示尾部?