动画心形图标

时间:2017-10-25 19:03:43

标签: javascript html css animation svg

所以我正在尝试为SVG图标制作动画。我有一颗心和几条线都围绕着这一类。默认情况下,我希望心脏只有一个轮廓,然后在点击时更改背景颜色(我有工作)。

我正在努力的部分只是在点击时显示心脏周围的线条。我知道这很简单,但我似乎无法绕过它。在我的CSS中,我默认设置了.lines{display: none},我还创建了一个类.lines-show{fill: red},我希望它们能够在点击心脏时显示。然后我将使用setTimeout()函数在X秒之后隐藏线条。这是我的codepen的链接,如果这有帮助https://codepen.io/Brushel/pen/xXvqgK?editors=1111这是我到目前为止的代码:

const lines = document.querySelectorAll(".lines");
const heart = document.querySelector('.heart');

heart.addEventListener('click', function(){
  heart.classList.toggle("heart-fill-up");
});
svg {
  max-height: 100px;
}

.heart {
  fill: transparent;
  transition: all .5s;
}

.heart-fill-up {
  fill: red;
}

.lines {
  display: none;
}

.lines-show {
  fill: red;
}
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 298 281" style="enable-background:new 0 0 298 281;" xml:space="preserve">
<style type="text/css">
	.heart{
    stroke:#FF0606;
    stroke-miterlimit:10;
    stroke-width: 5px;
  }
</style>
<title>icon_wishlist</title>
<g id="icon_wishlist">
	<path class="heart" d="M66.3,69.4c-19.1,20-19.1,51.5,0,71.5l84.6,87.9l84.7-87.8c19.1-20,19.1-51.5,0-71.5
		c-18-19.1-48.1-19.9-67.2-1.8c-0.6,0.6-1.2,1.2-1.8,1.8L151,85.7l-15.6-16.2c-18-19.1-48.1-19.9-67.2-1.8
		C67.6,68.2,67,68.8,66.3,69.4L66.3,69.4z"/>
</g>
<path class="lines" d="M58.1,60.6L9.4,22.1c-1.5-1.2-1.8-3.4-0.6-4.9l0,0c1.2-1.5,3.4-1.8,4.9-0.6l48.6,38.5c1.5,1.2,1.8,3.4,0.6,4.9
	l0,0C61.8,61.5,59.6,61.8,58.1,60.6z"/>
<path class="lines" d="M265.4,237l-48.6-38.5c-1.5-1.2-1.8-3.4-0.6-4.9v0c1.2-1.5,3.4-1.8,4.9-0.6l48.6,38.5c1.5,1.2,1.8,3.4,0.6,4.9
	l0,0C269.1,237.9,266.9,238.2,265.4,237z"/>
<path class="lines" d="M40.1,230.6L88,191.3c1.5-1.2,3.7-1,4.9,0.5v0c1.2,1.5,1,3.7-0.5,4.9l-47.9,39.3c-1.5,1.2-3.7,1-4.9-0.5l0,0
	C38.4,234.1,38.6,231.9,40.1,230.6z"/>
<path class="lines" d="M236.7,54.8l46-41.6c1.4-1.3,3.7-1.2,4.9,0.2v0c1.3,1.4,1.2,3.7-0.2,4.9l-46,41.6c-1.4,1.3-3.7,1.2-4.9-0.2v0
	C235.2,58.3,235.3,56.1,236.7,54.8z"/>
<path class="lines" d="M16.9,139.2h34c1.9,0,3.5,1.6,3.5,3.5v0c0,1.9-1.6,3.5-3.5,3.5h-34c-1.9,0-3.5-1.6-3.5-3.5v0
	C13.4,140.7,15,139.2,16.9,139.2z"/>
<path class="lines" d="M249.8,139.2h34c1.9,0,3.5,1.6,3.5,3.5v0c0,1.9-1.6,3.5-3.5,3.5h-34c-1.9,0-3.5-1.6-3.5-3.5v0
	C246.3,140.7,247.8,139.2,249.8,139.2z"/>
<path class="lines" d="M154.5,237.3v34c0,1.9-1.6,3.5-3.5,3.5l0,0c-1.9,0-3.5-1.6-3.5-3.5v-34c0-1.9,1.6-3.5,3.5-3.5l0,0
	C152.9,233.8,154.5,235.4,154.5,237.3z"/>
<path class="lines" d="M154.5,20.3v34c0,1.9-1.6,3.5-3.5,3.5l0,0c-1.9,0-3.5-1.6-3.5-3.5v-34c0-1.9,1.6-3.5,3.5-3.5l0,0
	C152.9,16.8,154.5,18.4,154.5,20.3z"/>
</svg>

这是我在click

上的理想结果

enter image description here

2 个答案:

答案 0 :(得分:1)

因为你有多个元素&#34;行&#34;每当你想要切换它们的类时,你需要单独对它们进行排序(我使用了for循环来执行此操作)。我还添加了一个setTimeOut来在1000毫秒后删除它们。

http://jsfiddle.net/jnqzoq1g/

xmlns:pbcore="http://www.pbcore.org/PBCore/PBCoreNamespace.html"
xsi:schemaLocation="http://www.loc.gov/mods/v3 
http://www.loc.gov/standards/mods/v3/mods-3-6.xsd
http://www.pbcore.org/PBCore/PBCoreNamespace.html 
http://pbcore.org/xsd/pbcore-2.0.xsd">

答案 1 :(得分:0)

编辑所需结果:

您可以使用document.getElementsByClassName("lines");将所有行转换为数组中的所有行,如果将其转换为数组,则可以执行

Array.from(document.getElementsByClassName("lines")).forEach( line => {
    line.style.display = "block";
});

您可以通过将切换包装在setInterval中来重复切换。

let interval = setInterval( () => {
    heart.classList.toggle("heart-fill-up");
}, 1000);

如果您不想使用javascript,可以使用css动画集重复

@keyframes heart {
  0% {
    fill: transparent; 
  }
  100% {
    fill: red;
  }
}

并将其应用于您的心脏

animation: heart 1s infinite