javascript - css 同步关键帧动画

标签 javascript jquery html css keyframe

我希望同步三个不同元素的关键帧动画。

它们基本上是三颗心,我希望它们在被单击时遵循心跳动画。

当单击两个或多个时,我希望它们同步“跳动”。

可以查看JSbin here

到目前为止我所拥有的是:

.rating {
  position: relative;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 140px;
  height: 50px;
  padding: 5px 10px;
  margin: auto;
  border-radius: 30px;
  background: #FFF;
  display: block;
  overflow: hidden;
  unicode-bidi: bidi-override;
  direction: rtl;
}

.rating:not(:checked)>input {
  display: none;
}


/* - - - - - LIKE */

#like {
  bottom: -65px;
}

#like:not(:checked)>label {
  cursor: pointer;
  float: right;
  width: 30px;
  height: 30px;
  display: block;
  margin-right: 7.5px;
  color: rgba(233, 54, 40, .4);
  line-height: 42px;
  text-align: center;
  transition: color 0.2s;
}

#like:not(:checked)>label:hover,
#like:not(:checked)>label:hover~label {
  color: rgba(233, 54, 40, .6);
}

#like>input:checked+label:hover,
#like>input:checked+label:hover~label,
#like>input:checked~label:hover,
#like>input:checked~label:hover~label,
#like>label:hover~input:checked~label {
  color: rgb(233, 54, 40);
}

#like>input:checked~label {
  color: rgb(233, 54, 40);
  animation: 1s heartbeat infinite;
}

@keyframes heartbeat {
  from {
    transform: scale(1);
    transform-origin: center center;
    animation-timing-function: ease-out;
  }
  10% {
    transform: scale(1.2);
    animation-timing-function: ease-in;
  }
  15% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
  25% {
    transform: scale(1.15);
    animation-timing-function: ease-in;
  }
  35% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
}
<section id="like" class="rating">

  <!-- THIRD HEART -->
  <input type="radio" id="heart_3" name="like" value="3" />
  <label for="heart_3" class="heart-slider">&#9829;</label>

  <!-- SECOND HEART -->
  <input type="radio" id="heart_2" name="like" value="2" />
  <label for="heart_2" class="heart-slider">&#9829;</label>

  <!-- FIRST HEART -->
  <input type="radio" id="heart_1" name="like" value="1" />
  <label for="heart_1" class="heart-slider">&#9829;</label>

</section>

实现同步的好方法是什么?

谢谢

编辑

根据 Rounin 的回答,这里是完成工作的更新后的 CSS:

#like {
}
#like:not(:checked) > label {
  cursor:pointer;
  float: right;
  width: 30px;
  height: 30px;
  display: inline-block;

  color: (255, 255, 255);

  transition: color 0.2s;
}

#like:not(:checked) > label:hover,
#like:not(:checked) > label:hover ~ label {
  color: rgba(252, 108, 133, 1);
}
#like > input:checked + label:hover,
#like > input:checked + label:hover ~ label,
#like > input:checked ~ label:hover,
#like > input:checked ~ label:hover ~ label,
#like > label:hover ~ input:checked ~ label {
 color: rgb(252, 108, 133);

}
#like > input:checked ~ label {
  color: rgb(252, 108, 133);

}

#heart_1:checked ~ label {
animation: heartbeat1 1s infinite;
}

#heart_2:checked ~ label {
animation: heartbeat2 1s infinite;
}

#heart_3:checked ~ label {
animation: heartbeat3 1s infinite;
}


@keyframes heartbeat1 {
  from {
    transform: scale(1);
    transform-origin: center center;
    animation-timing-function: ease-out;
  }
  10% {
    transform: scale(1.2);
    animation-timing-function: ease-in;
  }
  15% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
  25% {
    transform: scale(1.15);
    animation-timing-function: ease-in;
  }
  35% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
}

@keyframes heartbeat2 {
  from {
    transform: scale(1);
    transform-origin: center center;
    animation-timing-function: ease-out;
  }
  10% {
    transform: scale(1.2);
    animation-timing-function: ease-in;
  }
  15% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
  25% {
    transform: scale(1.15);
    animation-timing-function: ease-in;
  }
  35% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
}

@keyframes heartbeat3 {
  from {
    transform: scale(1);
    transform-origin: center center;
    animation-timing-function: ease-out;
  }
  10% {
    transform: scale(1.2);
    animation-timing-function: ease-in;
  }
  15% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
  25% {
    transform: scale(1.15);
    animation-timing-function: ease-in;
  }
  35% {
    transform: scale(1);
    animation-timing-function: ease-out;
  }
}

最佳答案

您可以设置 3 个相同的动画并设置输入和标签的样式,这样当选择新的单选按钮时,旧的动画就会停止,新的动画就会开始。这样,心就永远同步:

工作示例:

div {
display: block;
position: relative;
width: 125px;
height: 60px;
box-shadow: 3px 3px 3px rgba(127, 127, 127, 0.4);
}

input {
display: none;
}

label {
position: absolute;
display: inline-block;
top: 0;
font-size: 40px;
line-height: 60px;
color: rgba(255, 127, 127, 0.75);
}


label:hover,
label:hover ~ label {
color: rgba(255, 0, 0, 0.75);
}

.like1 {
left: 10px;
}

.like2 {
left: 50px;
}

.like3 {
left: 90px;
}

#like1:checked ~ label {
animation: heartbeat1 0.8s infinite;
}

#like2:checked ~ label {
animation: heartbeat2 0.8s infinite;
}

#like3:checked ~ label {
animation: heartbeat3 0.8s infinite;
}

@keyframes heartbeat1 {
  0% {
    color: rgba(255, 0, 0, 0.5);
  }

  50% {
    color: rgba(255, 0, 0, 1);
  }
}

@keyframes heartbeat2 {
  0% {
    color: rgba(255, 0, 0, 0.5);
  }

  50% {
    color: rgba(255, 0, 0, 1);
  }
}

@keyframes heartbeat3 {
  0% {
    color: rgba(255, 0, 0, 0.5);
  }

  50% {
    color: rgba(255, 0, 0, 1);
  }
}
<div>
<input type="radio" id="like3" name="likes" value="3" />
<label class="like3" for="like3">&#9829;</label>

<input type="radio" id="like2" name="likes" value="2" />
<label class="like2" for="like2">&#9829;</label>

<input type="radio" id="like1" name="likes" value="1" />
<label class="like1" for="like1">&#9829;</label>
</div>

关于javascript - css 同步关键帧动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946700/

相关文章:

javascript - 页面加载后添加的类字段的日期选择器初始化

javascript - jquery基础入门问题: How do i make my function reusable?

jquery - 带有两个嵌套 flexbox 的动态磨碎网格不起作用?

javascript - 悬停不适用于 div 标签,而是更改内部文本元素的颜色

javascript - chrome 扩展的浏览器 Action 、后台脚本和内容脚本之间通信的上下文和方法?

Javascript如何使用已经提供的时间(以秒为单位)继续计时器

jQuery ReplaceWith() 之后 JavaScript 断开连接

javascript - 基本 jQuery slider 中上一张/下一张幻灯片的自定义链接触发器

javascript - 全宽图像到 div 在 CSS3 和 HTML5 中交叉淡入淡出

html - 表单标签的 CSS 样式