CSS 关键帧手机振动

标签 css css-animations keyframe

我正在尝试制作类似手机振动的效果。这是我目前拥有的:https://codepen.io/anon/pen/jwWwzx

我只是想弄清楚如何添加中断,例如振动一秒钟然后暂停一秒钟然后重复。

<div class="phone"><img src="https://i.imgpile.com/nucPMx.png"></div>


.phone {
  -webkit-animation: vibrate 0.32s cubic-bezier(.36, .07, .19, .97) infinite;
  animation: vibrate 0.32s cubic-bezier(.36, .07, .19, .97) infinite;
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-perspective: 300px;
  perspective: 300px;
}

@keyframes vibrate {
  0.50%, 90% {
   -webkit-transform: translate3d(-0.5px, 0, 0);
   transform: translate3d(-0.5px, 0, 0);
 }

0.50%, 80% {
  -webkit-transform: translate3d(0.5px, 0, 0);
  transform: translate3d(0.5px, 0, 0);
}

30%, 50%, 70% {
  -webkit-transform: translate3d(-0.5px, 0, 0);
  transform: translate3d(-0.5px, 0, 0);
}

0.50%, 60% {
  -webkit-transform: translate3d(0.5px, 0, 0);
  transform: translate3d(0.5px, 0, 0);
}
}

最佳答案

我更新了您的代码,它暂停了大约 20% 的动画。这样,您就可以同时保留最后的停顿和快速振动效果 ( DEMO ):

.phone {
  -webkit-animation: vibrate 2s cubic-bezier(.36, .07, .19, .97) infinite;
  animation: vibrate 2s cubic-bezier(.36, .07, .19, .97) infinite;
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-perspective: 300px;
  perspective: 300px;
}


@keyframes vibrate {
  0%, 2%, 4%, 6%, 8%, 10%, 12%, 14%, 16%, 18% {
    -webkit-transform: translate3d(-1px, 0, 0);
            transform: translate3d(-1px, 0, 0);
  }
  1%, 3%, 5%, 7%, 9%, 11%, 13%, 15%, 17%, 19% {
    -webkit-transform: translate3d(1px, 0, 0);
            transform: translate3d(1px, 0, 0);
  }
  20%, 100% {
    -webkit-transform: translate3d(0, 0, 0);
            transform: translate3d(0, 0, 0);
  }
}

关于CSS 关键帧手机振动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44455538/

相关文章:

css - CSS 关键帧动画中的平滑旋转

html - 两列之间的 CSS 垂直分隔线将与较长列的高度相匹配

css - 菜单显示中的溢出样式

javascript - 获取 CSSKeyframesRule 长度

css - 显示带有淡出效果的 W

css - 使用 CSS3 动画和 Sass @for 循环触发列表

html - 静态div内容改变位置

Html 正斜杠显示不正确(Chrome 和 Edge)

css - 动画边框效果不适用于 IE

css - 用过渡替换 css 中的@keyframes