【问题标题】:Heart Animation on click点击时的心脏动画
【发布时间】:2020-06-23 23:58:45
【问题描述】:

所以我想创建一个动画,在其中单击心脏,心脏周围有一个圆圈,该圆圈按比例放大然后逐渐消失。我试图给 SVG 一个边框,但什么也没显示?老实说,我不确定我在这里做错了什么。任何帮助将不胜感激。我希望它类似于 Twitter 心脏,但当你点击它时心脏周围没有圆点。这是我的代码笔的链接https://codepen.io/Brushel/pen/MEEYgQ

(function() {
  var animatedHeart, heartanimation, heartanimationthree;

  animatedHeart = document.getElementById('heart1');

  heartanimation = document.getElementById('heart2');

  heartanimationthree = document.getElementById('heart3');

  animatedHeart.addEventListener('click', function() {
    return animatedHeart.classList.toggle('fill');
  });

  heartanimation.addEventListener('click', function() {
    return heartanimation.classList.toggle('fillup');
  });

  heartanimationthree.addEventListener('click', function() {
    return heartanimationthree.classList.toggle('heartscale');
  });

}).call(this);
body {
  max-width: 1200px;
  margin: 0 auto;
  position: relative;
  background: black;
  height: 100vh;
}

h2 {
  text-align: center;
  color: white;
  font-weight: 300;
  letter-spacing: 0.6px;
  font-size: 3em;
}

.container {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
      flex-wrap: wrap;
  -webkit-box-pack: justify;
      -ms-flex-pack: justify;
          justify-content: space-between;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
      -ms-flex-direction: row;
          flex-direction: row;
  max-width: 1280px;
  margin: 10px auto;
  padding-left: 15px;
  padding-right: 15px;
  margin-left: auto;
  margin-right: auto;
}

.col-12 {
  -webkit-box-flex: 0;
      -ms-flex: 0 0 100%;
          flex: 0 0 100%;
}

#heart1 {
  max-height: 70px;
  fill: white;
  stroke: white;
  cursor: pointer;
}

.fill {
  -webkit-animation: love .5s linear alternate forwards;
          animation: love .5s linear alternate forwards;
  stroke: #D22128;
  stroke-width: 5px;
}

@-webkit-keyframes love {
  0% {
    stroke-dashoffset: 1000;
    stroke: #D22128;
  }
  50% {
    stroke-dashoffset: 0;
    stroke: #D22128;
    fill: #D22128;
    -webkit-transform: translateY(5px);
            transform: translateY(5px);
  }
  100% {
    stroke: transparent;
    fill: #D22128;
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
}

@keyframes love {
  0% {
    stroke-dashoffset: 1000;
    stroke: #D22128;
  }
  50% {
    stroke-dashoffset: 0;
    stroke: #D22128;
    fill: #D22128;
    -webkit-transform: translateY(5px);
            transform: translateY(5px);
  }
  100% {
    stroke: transparent;
    fill: #D22128;
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
}
#heart2 {
  max-height: 70px;
  fill: white;
  cursor: pointer;
}

.fillup {
  -webkit-animation: lovebutton .5s alternate forwards;
          animation: lovebutton .5s alternate forwards;
}

@-webkit-keyframes lovebutton {
  0% {
    -webkit-transform: translate3d(0px, 0, 0);
            transform: translate3d(0px, 0, 0);
    fill: #D22128;
  }
  25% {
    -webkit-transform: translate3d(4px, 0, 0);
            transform: translate3d(4px, 0, 0);
    fill: #D22128;
  }
  100% {
    -webkit-transform: translate3d(0px, 0, 0);
            transform: translate3d(0px, 0, 0);
    fill: #D22128;
  }
}

@keyframes lovebutton {
  0% {
    -webkit-transform: translate3d(0px, 0, 0);
            transform: translate3d(0px, 0, 0);
    fill: #D22128;
  }
  25% {
    -webkit-transform: translate3d(4px, 0, 0);
            transform: translate3d(4px, 0, 0);
    fill: #D22128;
  }
  100% {
    -webkit-transform: translate3d(0px, 0, 0);
            transform: translate3d(0px, 0, 0);
    fill: #D22128;
  }
}
#heart3 {
  max-height: 70px;
  fill: white;
  stroke: white;
  cursor: pointer;
}

.heartscale {
  -webkit-animation: heart-beat .5s alternate forwards;
          animation: heart-beat .5s alternate forwards;
  stroke-width: 0px;
}

@-webkit-keyframes heart-beat {
  0% {
    -webkit-transform: scale(1, 1);
            transform: scale(1, 1);
  }
  50% {
    -webkit-transform: scale(1.3, 1.3);
            transform: scale(1.3, 1.3);
    fill: #D22128;
  }
  100% {
    -webkit-transform: scale(1, 1);
            transform: scale(1, 1);
    fill: #D22128;
  }
}

@keyframes heart-beat {
  0% {
    -webkit-transform: scale(1, 1);
            transform: scale(1, 1);
  }
  50% {
    -webkit-transform: scale(1.3, 1.3);
            transform: scale(1.3, 1.3);
    fill: #D22128;
  }
  100% {
    -webkit-transform: scale(1, 1);
            transform: scale(1, 1);
    fill: #D22128;
  }
}
<body>
  <div class="container">
    <div class="col-12">
      <h2>Animated Hearts</h2>
      <div class="circle">
  <svg class="mo-icon__svg" x="0px" y="0px"
	 viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve" id="heart1">
<g id="icon_x5F_wishlist">
	<g transform="translate(0,-952.36218)">
		<path class="st0" id="st0" d="M15.33293,980.7616c-19.11968,19.8092-19.10147,51.68518,0,71.51379l84.61456,87.86926
			c28.23759-29.25574,56.47517-58.51135,84.71275-87.76758c19.11969-19.80969,19.11969-51.70477,0-71.51422
			c-19.12007-19.80945-49.90512-19.80994-69.02521,0l-15.58933,16.15155l-15.68754-16.25305
			c-19.12008-19.80945-49.90513-19.80945-69.02521,0L15.33293,980.7616z"/>
	</g>
</g>
</svg>
    </div>
<!--     End of the first Heart     -->
        
        <svg class="mo-icon__svg" x="0px" y="0px"
	 viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve" id="heart2">
<g id="icon_x5F_wishlist">
	<g transform="translate(0,-952.36218)">
		<path class="st0" id="st0" d="M15.33293,980.7616c-19.11968,19.8092-19.10147,51.68518,0,71.51379l84.61456,87.86926
			c28.23759-29.25574,56.47517-58.51135,84.71275-87.76758c19.11969-19.80969,19.11969-51.70477,0-71.51422
			c-19.12007-19.80945-49.90512-19.80994-69.02521,0l-15.58933,16.15155l-15.68754-16.25305
			c-19.12008-19.80945-49.90513-19.80945-69.02521,0L15.33293,980.7616z"/>
	</g>
</g>
</svg>
      
<!--    End of second heart    -->
      
      <svg class="mo-icon__svg" x="0px" y="0px"
	 viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve" id="heart3">
<g id="icon_x5F_wishlist">
	<g transform="translate(0,-952.36218)">
		<path class="st0" id="st0" d="M15.33293,980.7616c-19.11968,19.8092-19.10147,51.68518,0,71.51379l84.61456,87.86926
			c28.23759-29.25574,56.47517-58.51135,84.71275-87.76758c19.11969-19.80969,19.11969-51.70477,0-71.51422
			c-19.12007-19.80945-49.90512-19.80994-69.02521,0l-15.58933,16.15155l-15.68754-16.25305
			c-19.12008-19.80945-49.90513-19.80945-69.02521,0L15.33293,980.7616z"/>
	</g>
</g>
</svg>
      


    </div>
  </div>
</body>

【问题讨论】:

  • 所以你想在一颗心周围加上一个边框?
  • 是的,但我不希望边框触动人心。我知道我可以使用 stoke: {} 但我想让边框根本不触及心脏。如果这有任何意义@tomsmithweb

标签: javascript css animation svg css-animations


【解决方案1】:

最后一个看起来很接近!我认为在 css 中有一种更简单(更简单)的方法。

Nicolas Escoffer 在这里有一篇很棒的文章:

文章: https://blog.prototypr.io/twitter-s-heart-animation-in-full-css-b1c00ca5b774

代码笔: https://codepen.io/OxyDesign/pen/avXVbo

否则我会建议使用您创建的圆形 div 的边框创建一个关键帧动画。确保将圆形 div 设置在 heart .svg 之外,这样它就不会包裹元素:

我根据你原来的 codepen 在下面为你做了一个例子:

#HTML:

<body>
  <div class="container">
    <div class="col-12">
      <h2>&nbsp;</h2>
      <br>
      <div id="circle" class="circle"></div>
       <svg class="mo-icon__svg" x="0px" y="0px"
     viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve" id="heart4">
<g id="icon_x5F_wishlist">
    <g transform="translate(0,-952.36218)">
        <path class="st0" id="st0" d="M15.33293,980.7616c-19.11968,19.8092-19.10147,51.68518,0,71.51379l84.61456,87.86926
            c28.23759-29.25574,56.47517-58.51135,84.71275-87.76758c19.11969-19.80969,19.11969-51.70477,0-71.51422
            c-19.12007-19.80945-49.90512-19.80994-69.02521,0l-15.58933,16.15155l-15.68754-16.25305
            c-19.12008-19.80945-49.90513-19.80945-69.02521,0L15.33293,980.7616z"/>
    </g>
</g>
</svg>
  </div>
</body>

#SCSS:

body {
  max-width: 1200px;
  margin: 0 auto;
  position: relative;
  background: black;
  height: 100vh;
  color: white;
}

h2{
  text-align: center;
  color: white;
  font-weight: 300;
  letter-spacing: 0.6px;
  font-size: 3em;
  user-select: none;
  
}

// Set up basic Grid
.container {
  display: block;
  flex-wrap: wrap;
  justify-content: space-between;
  flex-direction: row;
  max-width: 1280px;
  margin: 10px auto;
  padding-left: 50%;
  margin-left: auto;
  margin-right: auto;
}

.col-12{
  flex: 0 0 100%;
}

// Fourth Animation


#heart4 {
  max-height: 70px;
  fill: white;
  stroke: white;
  cursor: pointer;
  position: relative;
  width: 5rem;
}

.dot-animation {
  position: relative;
}


.dot {
  height: 10px;
  width: 10px;
  background: white;
  border-radius: 50%;
  position: absolute;
  &:nth-child(1){
    background: #D22128;
    left: 49.7%;
    top: 75px;
  }
   &:nth-child(2){
    background: #D22128;
    left: 49.4%;
    bottom: 8%;
  }
   &:nth-child(3){
    background: #D22128;
    left: 53.5%;
    top: 30px;
  }
   &:nth-child(4){
    background: #D22128;
    left: 45%;
    top: 30px;
  }
}

.bands {
  animation: snap 1s forwards;
  stroke-width: 0;
}

@keyframes snap {
  from { 
  transform: scale3d(1, 1, 1); 
  filter: brightness(0.5);
  }
  30% {  
    transform: scale3d(1.25, 0.75, 1); }
  40% {  
    transform: scale3d(0.75, 1.25, 1); }
  50% {  
    transform: scale3d(1.15, 0.85, 1); }
  65% {  
    transform: scale3d(.95, 1.05, 1); }
  75% {  
    transform: scale3d(1.05, .95, 1); }
  to {   
    transform: scale3d(1, 1, 1);
    fill: #D22128; 
    filter: brightness(1);
  }
}

.circle {
  position: absolute;
  display: block;
  border: 1px solid white;
  margin-left: 2.5rem;
  top: 8rem;
}

.burst {
    animation: burst .5s forwards;
}

@keyframes burst {
  0% {
  border: 1px solid white;
  border-radius: 50% 50%;
  width: 0.25rem;
  height: 0.25rem;
    
  }
  100% {
  border: 5px solid white;
  border-radius: 50% 50%;
  width: 7rem;
  height: 7rem;
  margin-left: -.90rem;
  margin-top: -2.45rem;
  opacity: 0;
  z-index: 999;
  }
}

#JS:

# Grab Element
heartanimationfour = document.getElementById('heart4')
circle = document.getElementById('circle')

# Add event listener for the click
heartanimationfour.addEventListener 'click', ->
  heartanimationfour.classList.toggle 'bands',
  circle.classList.toggle 'burst'

https://codepen.io/cwoolf123/pen/wvMdyKq?editors=1111

【讨论】:

    猜你喜欢
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多