我想要一个从div的中心开始的扩展半径,而不是从div的左上角开始。
想象一下按钮有一个向外跳动的轮廓。脉冲的轮廓应该从div的中间开始,然后消失。
请参见此处的示例:https://jsbin.com/dinehoqaro/edit?html,css,output
您可以看到扩展是从左上角开始的。

.circle {
  background-color: white;
  border: 1px solid red;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  animation: pulse 1s infinte;
  -webkit-animation: pulse 1.2s infinite;
}
button {
  background-color: green;
  border: none;
  border-radius: 50%;
  width: 50px;
  height: 50px;
}
@-webkit-keyframes pulse {
  from {
    width: 50px;
    height: 50px;
  }
  to {
    width: 100px height: 100px;
  }
}
@keyframes pulse {
  from {
    width: 50px;
    height: 50px;
  }
  to {
    width: 100px;
    height: 100px;
  }
}

<div class="circle"><button>click here</button></div>

最佳答案

这里有一个使用cssflexboxtransformpseudo-elements的通用解决方案。

body {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: lightyellow;
  height: 100vh;
  margin: 0;
}
#container {
  display: flex;
  align-items: center;
  justify-content: center;
}
.sphere {
  display: flex;
  background: lightblue;
  border-radius: 300px;
  height: 100px;
  width: 100px;
}
#container::after {
  display: flex;
  background: lightpink;
  border-radius: 300px;
  height: 250px;
  width: 250px;
  animation: pulsate 2.5s ease-out;
  animation-iteration-count: infinite;
  opacity: 0.0;
  content: "";
  z-index: -1;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
@keyframes pulsate {
  0% {
    transform: scale(0.1, 0.1);
    opacity: 0.0;
  }
  50% {
    opacity: 1.0;
  }
  100% {
    transform: scale(1.2, 1.2);
    opacity: 0.0;
  }
}

<div id="container">
  <div class="sphere"></div>
</div>

jsFiddle
也可以通过@harry查看这个很棒的解决方案:How to create a pulsing glow ring animation in CSS?

08-25 10:45