跳至主要內容

跳动的点

望间代码HTML&CSS动画小于 1 分钟

CSS 世界之跳动的点

效果展示

<div class="container">
  <div class="dot"></div>
</div>
.container {
  position: relative;
  height: 80px;
  width: 100%;
  display: flex;
  justify-content: center;
}
.dot:before {
  content: "";
  width: 20px;
  height: 20px;
  position: absolute;
  border-radius: 50%;
  background-color: #333;
  animation: dot 0.5s ease infinite alternate;
}
@keyframes dot {
  0% {
    top: 60px;
    height: 5px;
    border-radius: 50px 50px 25px 25px;
    transform: scaleX(1.5);
  }
  50% {
    height: 20px;
    border-radius: 50%;
    transform: scaleX(1);
  }
  100% {
    top: 0;
  }
}
.dot:after {
  content: "";
  width: 20px;
  height: 4px;
  border-radius: 50%;
  position: absolute;
  top: 62px;
  animation: shadow 0.5s ease infinite alternate;
}
@keyframes shadow {
  0% {
    filter: blur(1px);
    transform: scaleX(1.5);
    background-color: rgba(0, 0, 0, 0.9);
  }
  50% {
    transform: scaleX(1);
    background-color: rgba(0, 0, 0, 0.6);
  }
  100% {
    filter: blur(2px);
    transform: scaleX(0.5);
    background-color: rgba(0, 0, 0, 0.3);
  }
}

实现思路

主要是利用伪元素 ::before::after ,以及 CSS 动画 animation 实现的效果

使用 ::before 做跳动的原点,::after 作为原点的阴影,分别设置各自的动画

这里涉及的动画都使用了 animation-direction: alternate;,使动画实现正反交替的效果

animation-direction 属性的具体使用方法请参考 MDN-animation-directionopen in new window

上次编辑于:
贡献者: ViewRoom