【问题标题】:Pulsing Heart CSS animation脉动的心脏 CSS 动画
【发布时间】:2016-04-18 03:37:34
【问题描述】:

我正在使用 CSS 制作一个动画心脏。

我希望它脉搏 2 次,稍作休息,然后再重复一次。

我现在拥有的:

small ==> big ==> small ==> repeat animation

我要做什么:

small ==> big ==> small ==> big ==> small ==> pause ==> repeat animation

我该怎么做?

我的代码:

#button{
  width:450px;
  height:450px;
  position:relative;
  top:48px;
  margin:0 auto;
  text-align:center;
  }
#heart img{
  position:absolute;
  left:0;
  right:0;
  margin:0 auto;
  -webkit-transition: opacity 7s ease-in-out;
  -moz-transition: opacity 7s ease-in-out;
  -o-transition: opacity 7s ease-in-out;
  transition: opacity 7s ease-in-out;}

 @keyframes heartFadeInOut {
  0% {
    opacity:1;
  }
  14% {
    opacity:1;
  }
  28% {
    opacity:0;
  }
  42% {
    opacity:0;
  }
  70% {
    opacity:0;
  }
}

#heart img.top { 
  animation-name: heartFadeInOut; 
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 1s;
  animation-direction: alternate;

}
<div id="heart" >
  <img class="bottom" src="https://goo.gl/nN8Haf" width="100px">
  <img class="top" src="https://goo.gl/IIW1KE" width="100px">
</div>

另见this Fiddle

【问题讨论】:

  • 您使用 jsfiddle 而不是问题中集成的 Stack Snippet 有什么原因吗?据我所知,堆栈片段适用于您的示例。

标签: html css animation


【解决方案1】:

您可以将暂停合并到动画中。像这样:

@keyframes heartbeat
{
  0%
  {
    transform: scale( .75 );
  }
  20%
  {
    transform: scale( 1 );
  }
  40%
  {
    transform: scale( .75 );
  }
  60%
  {
    transform: scale( 1 );
  }
  80%
  {
    transform: scale( .75 );
  }
  100%
  {
    transform: scale( .75 );
  }
}

工作示例: https://jsfiddle.net/t7f97kf4/

@keyframes heartbeat
{
  0%
  {
    transform: scale( .75 );
  }
  20%
  {
    transform: scale( 1 );
  }
  40%
  {
    transform: scale( .75 );
  }
  60%
  {
    transform: scale( 1 );
  }
  80%
  {
    transform: scale( .75 );
  }
  100%
  {
    transform: scale( .75 );
  }
}

div
{
  background-color: red;
  width: 50px;
  height: 50px;
  animation: heartbeat 1s infinite;
}
<div>
Heart
</div>

编辑:

纯 CSS 心形的工作示例: https://jsfiddle.net/qLfg2mrd/

@keyframes heartbeat
{
  0%
  {
    transform: scale( .75);
  }
  
  20%
  {
    transform: scale( 1);
  }
  
  40%
  {
    transform: scale( .75);
  }
  
  60%
  {
    transform: scale( 1);
  }
  80% {
    transform: scale( .75);
  }
  
  100%
  {
    transform: scale( .75);
  }
}

#heart
{
  position: relative;
  width: 100px;
  height: 90px;
  animation: heartbeat 1s infinite;
}

#heart:before,
#heart:after
{
  position: absolute;
  content: "";
  left: 50px;
  top: 0;
  width: 50px;
  height: 80px;
  background: red;
  -moz-border-radius: 50px 50px 0 0;
  border-radius: 50px 50px 0 0;
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transform-origin: 0 100%;
  -moz-transform-origin: 0 100%;
  -ms-transform-origin: 0 100%;
  -o-transform-origin: 0 100%;
  transform-origin: 0 100%;
}

#heart:after
{
  left: 0;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-transform-origin: 100% 100%;
  -moz-transform-origin: 100% 100%;
  -ms-transform-origin: 100% 100%;
  -o-transform-origin: 100% 100%;
  transform-origin: 100% 100%;
}
&lt;div id="heart"&gt;&lt;/div&gt;

【讨论】:

  • @FernandoSouza 你也可以制作没有图像的 css heart - jsfiddle.net/qLfg2mrd
  • @Anonymous 谢谢你。我将您的链接添加到我的答案中。
  • 我正在为那个小提琴添加书签!很棒的方法。
  • @DavidWilkinson 也将这个加入书签 :) - css-tricks.com/examples/ShapesOfCSS :)
  • 你是说♥字符?这很简单,只需&amp;hearts;。不需要所有那些transforms。
【解决方案2】:

脉冲 2 次,稍作休息,然后再重复一次

试试这个。使用动画opacity 是一个糟糕的选择。 transform: scale() 将完成这项工作。

.heart:before {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  font-family: 'icons';
  font-size: 21px;
  text-indent: 0;
  font-variant: normal;
  line-height: 21px;
}
.heart {
  position: relative;
  width: 500px;
  overflow: inherit;
  margin: 50px auto;
  list-style: none;
  -webkit-animation: animateHeart 2.5s infinite;
  animation: animateHeart 2.5s infinite;
}
.heart:before,
.heart:after {
  position: absolute;
  content: '';
  top: 0;
  left: 50%;
  width: 120px;
    height: 200px;
    background: red;
    border-radius: 100px 100px 0 0;
  -webkit-transform: rotate(-45deg) translateZ(0);
  transform: rotate(-45deg) translateZ(0);
  -webkit-transform-origin: 0 100%;
  transform-origin: 0 100%;
}
.heart:after {
  left: 26%;
  -webkit-transform: rotate(45deg) translateZ(0);
  transform: rotate(45deg) translateZ(0);
  -webkit-transform-origin: 100% 100%;
  transform-origin: 100% 100%;
}
@-webkit-keyframes animateHeart {
  0% {
    -webkit-transform: scale(0.8);
  }
  5% {
    -webkit-transform: scale(0.9);
  }
  10% {
    -webkit-transform: scale(0.8);
  }
  15% {
    -webkit-transform: scale(1);
  }
  50% {
    -webkit-transform: scale(0.8);
  }
  100% {
    -webkit-transform: scale(0.8);
  }
}
@keyframes animateHeart {
  0% {
    transform: scale(0.8);
  }
  5% {
    transform: scale(0.9);
  }
  10% {
    transform: scale(0.8);
  }
  15% {
    transform: scale(1);
  }
  50% {
    transform: scale(0.8);
  }
  100% {
    transform: scale(0.8);
  }
}
span {
  font-family: 'Cantora One', sans-serif;
  font-size: 64px;
  position: absolute;
  top: 165px;
}
<div class="heart">
</div>

【讨论】:

  • 我需要在最终项目中使用图像而不是纯 CSS。但是,谢谢!我可能会使用你的动画时间,这很完美!
  • 没什么区别。您也可以通过这种方式缩放图像。试试看吧:)
  • 我可以用这种方式交替图像或更改背景图像吗?
  • @FernandoSouza 是的。试一下。如果您遇到任何问题,我会帮助您。
  • 我正在尝试,但首先要更改一些结构,因为心脏上有文字,它必须保持相同的大小。
【解决方案3】:

我喜欢ketan 的回答,但我想改进心脏动画使其更逼真。

  • 心脏在跳动时不会变大。 10% 的尺寸变化对我来说更好看。
  • 我喜欢它变大和变小
  • 当它完全停止移动时,我觉得它已经死了。即使它没有跳动,它也需要稍微膨胀或收缩
  • 我删除了“备用方向”代码,以便它每次都以相同的方式运行
  • 我明确地让心脏开始结束并处于正常比例 (1),并且动画位于序列的中间。对我来说,这样似乎更清楚。

#heart img{
  position:absolute;
  left:0;
  right:0;
  margin:0 auto;
 }

 @keyframes heartFadeInOut {
  0% {transform: scale(1);}
  25% {transform: scale(.97);}
  35% {transform: scale(.9);}
  45% {transform: scale(1.1);}
  55% {transform: scale(.9);}
  65% {transform: scale(1.1);}
  75% {transform: scale(1.03);}
  100% {transform: scale(1);}
}

#heart img.bottom { 
  animation-name: heartFadeInOut; 
  animation-iteration-count: infinite;
  animation-duration: 2s;
}
<div id="heart" >
  <img class="bottom" src="https://i.stack.imgur.com/iBCpb.png" width="100px">
</div>

【讨论】:

    【解决方案4】:

    基于各种 cmets 并利用 ♥ 我们将得到:

    body {
      font-size: 40pt;
      color: red;
    }
    @keyframes heartbeat {
      0% {
        font-size: .75em;
      }
      20% {
        font-size: 1em;
      }
      40% {
        font-size: .75em;
      }
      60% {
        font-size: 1em;
      }
      80% {
        font-size: .75em;
      }
      100% {
        font-size: .75em;
      }
    }
    div {
      animation: heartbeat 1s infinite;
    }
    <div>
      &hearts;
    </div>

    【讨论】:

      【解决方案5】:

      body{
        margin: 0;
        padding: 0;
        background: #1f1f1f;
      }
      
      body:before
      {
        position: absolute;
        content: '';
        left: 50%;
        width: 50%;
        height: 100%;
        background: rgba(0,0,0,.2);
      
      }
      
      .center
      {
        position: absolute;
        top:50%;
        left: 50%;
        background: #1f1f1f;
        transform: translate(-50%,-50%);
        padding: 100px;
        border: 5px solid white;
        border-radius: 100%;
        box-shadow:20px 20px 45px rgba(0,0,0,.4);
        z-index: 1;
        overflow: hidden;
      }
      .heart
      {
        position: relative;
        width: 100px;
        height: 100px;
        background:#ff0036;
        transform: rotate(45deg) translate(10px,10px);
        animation: ani 1s linear infinite;
      }
      .heart:before
      {
        content: '';
        width: 100%;
        height: 100%;
        background: #ff0036;
        position: absolute;
        top:-50%;
        left:0;
        border-radius: 50%;
      }
      .heart:after
      {
        content:'';
        width: 100%;
        height: 100%;
        background: #ff0036;
        position: absolute;
        bottom:0;
        right:50%;
        border-radius: 50%;
      }
      .center:before
      {
        content: '';
        position: absolute;
        top:0;
        left:-50%;
        width: 100%;
        height: 100%;
        background: rgba(0,0,0,.3);
      }
      
      @keyframes ani{
        0%{
          transform: rotate(45deg) translate(10px,10px) scale(1);
        }
        25%{
          transform: rotate(45deg) translate(10px,10px) scale(1);
        }
        30%{
          transform: rotate(45deg) translate(10px,10px) scale(1.4);
        }
        50%{
          transform: rotate(45deg) translate(10px,10px) scale(1.2);
        }
        70%{
          transform: rotate(45deg) translate(10px,10px) scale(1.4);
        }
        90%{
          transform: rotate(45deg) translate(10px,10px) scale(1);
        }
        100%{
          transform: rotate(45deg) translate(10px,10px) scale(1);
        }
      }
      <!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8">
          <title>HeartBeat Animation</title>
          <link rel="stylesheet" href="Style.css" type="text/css">
        </head>
        <body>
          <div class="center">
            <div class="heart">
              
            </div>
          </div>
        </body>
      </html>

      Output

      了解更多:Heart Beating Animation

      【讨论】:

        【解决方案6】:

        我认为这就是您想要的图像动画。不需要顶部图像。只需使用底部。

        #button{
          width:450px;
          height:450px;
          position:relative;
          top:48px;
          margin:0 auto;
          text-align:center;
          }
        #heart img{
          position:absolute;
          left:0;
          right:0;
          margin:0 auto;
         }
        
         @keyframes heartFadeInOut {
          0%
          {    transform: scale( .5 );  }
          20%
          {    transform: scale( 1 );  }
          40%
          {    transform: scale( .5 );  }
          60%
          {    transform: scale( 1 );  }
          80%
          {    transform: scale( .5 );  }
          100%
          {    transform: scale( .5 );  }
        }
        
        #heart img.bottom { 
          animation-name: heartFadeInOut; 
          animation-iteration-count: infinite;
          animation-duration: 1.5s;
          animation-direction: alternate;
        
        }
        <div id="heart" >
          <img class="bottom" src="https://goo.gl/nN8Haf" width="100px">
        </div>

        【讨论】:

        • 您的心脏在 Chrome 上出现轻度心律失常?
        • @LegoStormtroopr 所以不仅仅是我。为什么要镀铬?为什么?
        • 我不确定,可能是心脏联合窦,但您需要咨询 CSS 专家以获得更好的诊断。
        【解决方案7】:

        我正在从事的一个项目需要这个。我试图让它看起来尽可能逼真,这就是我想出的。

        @keyframes heartbeat {
            0% {
                transform: scale( .95 );
            }
        
            20% {
                transform: scale( .97 );
            }
        
            30% {
                transform: scale( .95 );
            }
        
            40% {
                transform: scale( 1 );
            }
        
            100% {
                transform: scale( .95 );
            }
        }
        

        animation: heartbeat 1s infinite;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-02
          • 2023-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多