SVG简单动画

时间:2015-06-11 09:38:12

标签: html css animation svg polygon

我正在尝试学习如何使用SVG元素,因此我在Illustrator中制作了简单的图标/徽标。然后我在我的例子中使用了这个SVG图标:



@import url(http://fonts.googleapis.com/css?family=Lato);
 body {
  background-color: #2F2724;
  font-family: lato, sans-serif;
}
.main {
  text-align: center;
}
.letterS {
  fill: #fff;
  font-size: 180px;
  text-shadow: 0px 2px 2px #000;
}
#svgHex {
  display: block;
  margin: 40px auto;
  width: 350px;
}
.slogan {
  color: #D0E799;
  font-size: 60px;
  margin-top: 40px;
  margin-bottom: 0px;
  text-shadow: 0px 2px 2px #000;
}
.pSlogan {
  margin-top: 0px;
  font-size: 36px;
  color: #fff;
  text-shadow: 0px 2px 2px #000;
}

<div class="main">

  <svg version="1.1" id="svgHex" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="325.099px" height="289.284px" viewBox="-36.337 0 325.099 289.284" enable-background="new -36.337 0 325.099 289.284" xml:space="preserve">

    <g id="Layer_2">
      <polygon fill="none" stroke="#D0E39A" stroke-width="8" stroke-miterlimit="10" points="4.969,74.618 126.212,4.618 
		247.455,74.618 247.455,214.618 126.212,284.618 4.969,214.618 	" />
    </g>
    <g>
      <polygon display="none" fill="#FFFFFF" points="-4.712,158.921 -36.337,140.662 -36.337,68.452 -3.882,68.452 -4.712,68.931 	" />
      <polygon fill="#FFFFFF" points="-4.712,171.388 -4.712,201.722 -36.337,183.462 -36.337,153.129 	" />
      <polygon fill="#FFFFFF" points="21.379,261.787 67.138,261.787 114.762,289.283 69.002,289.283 	" />
      <polygon fill="#FFFFFF" points="-36.337,228.302 -36.337,195.93 -4.712,214.189 -4.712,220.305 53.858,254.12 8.381,254.12 	" />
    </g>
    <g>
      <polygon display="none" fill="#FFFFFF" points="257.137,158.921 288.762,140.662 288.762,68.452 256.307,68.452 257.137,68.931 	
		" />
      <polygon fill="#FFFFFF" points="257.137,171.388 257.137,201.722 288.762,183.462 288.762,153.129 	" />
      <polygon fill="#FFFFFF" points="231.046,261.787 185.286,261.787 137.663,289.283 183.423,289.283 	" />
      <polygon fill="#FFFFFF" points="288.762,228.302 288.762,195.93 257.137,214.189 257.137,220.305 198.568,254.12 244.044,254.12 	
		" />
    </g>
    <text transform="matrix(1 0 0 1 78.5125 208.6184)" class="letterS">S</text>
  </svg>

  <h1 class="slogan">BEST DESIGNS</h1>
  <p class="pSlogan">for your bussines</p>
</div>
&#13;
&#13;
&#13;

几乎所有内部​​代码都是由Illustrator生成的。我添加了id =&#34; svgHex&#34;到SVG和class =&#34; letterS&#34;到了&#34; S&#34;六角内的字母。

现在我想为六边形外的那些白色元素制作动画,但我不知道如何在CSS中访问它们。我试图在单个多边形中添加一个类并使用显示但是到目前为止没有任何工作。我并不真正关心任何特定的动画,只是想学习如何使用它们。

我不确定我是做得好还是一切都不好,并且有更好的方法制作这种图标动画。

任何建议将不胜感激。我是一个初学者,所以不要讨厌,请:)

1 个答案:

答案 0 :(得分:1)

除了@ web-tiki的答案之外,您还可以通过应用ID和&amp ;;将:hover和关键帧动画应用于SVG的所选'组件/元素'。类。

例如,将鼠标悬停在“S”上。

JSfiddle Demo

@import url(http://fonts.googleapis.com/css?family=Lato);
 body {
  background-color: #2F2724;
  font-family: lato, sans-serif;
}
.main {
  text-align: center;
}
.letterS {
  fill: #fff;
  font-size: 180px;
  text-shadow: 0px 2px 2px #000;
}
#svgHex {
  display: block;
  margin: 40px auto;
  width: 350px;
}
.slogan {
  color: #D0E799;
  font-size: 60px;
  margin-top: 40px;
  margin-bottom: 0px;
  text-shadow: 0px 2px 2px #000;
}
.pSlogan {
  margin-top: 0px;
  font-size: 36px;
  color: #fff;
  text-shadow: 0px 2px 2px #000;
}
svg text {
  transition: fill 0.5s ease;
}
svg text:hover {
  fill: red;
}
@keyframes blinky {
  0% {
    fill: none;
  }
  100% {
    fill: white;
  }
}
#vert-l,
#vert-r {
  animation: blinky 2s infinite;
}
<div class="main">

  <svg version="1.1" id="svgHex" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="325.099px" height="289.284px" viewBox="-36.337 0 325.099 289.284" enable-background="new -36.337 0 325.099 289.284" xml:space="preserve">

    <g id="Layer_2">
      <polygon fill="none" stroke="#D0E39A" stroke-width="8" stroke-miterlimit="10" points="4.969,74.618 126.212,4.618 
		247.455,74.618 247.455,214.618 126.212,284.618 4.969,214.618 	" />
    </g>
    <g>
      <polygon id="vert-l" fill="none" points="-4.712,158.921 -36.337,140.662 -36.337,68.452 -3.882,68.452 -4.712,68.931 	" />
      <polygon fill="#FFFFFF" points="-4.712,171.388 -4.712,201.722 -36.337,183.462 -36.337,153.129 	" />
      <polygon fill="#FFFFFF" points="21.379,261.787 67.138,261.787 114.762,289.283 69.002,289.283 	" />
      <polygon fill="#FFFFFF" points="-36.337,228.302 -36.337,195.93 -4.712,214.189 -4.712,220.305 53.858,254.12 8.381,254.12 	" />
    </g>
    <g>
      <polygon id="vert-r" fill="none" points="257.137,158.921 288.762,140.662 288.762,68.452 256.307,68.452 257.137,68.931 	
		" />
      <polygon fill="#FFFFFF" points="257.137,171.388 257.137,201.722 288.762,183.462 288.762,153.129 	" />
      <polygon fill="#FFFFFF" points="231.046,261.787 185.286,261.787 137.663,289.283 183.423,289.283 	" />
      <polygon fill="#FFFFFF" points="288.762,228.302 288.762,195.93 257.137,214.189 257.137,220.305 198.568,254.12 244.044,254.12 	
		" />
    </g>
    <text transform="matrix(1 0 0 1 78.5125 208.6184)" class="letterS">S</text>
  </svg>

  <h1 class="slogan">BEST DESIGNS</h1>
  <p class="pSlogan">for your bussines</p>
</div>

我应该提一下,如果你在Illustrator中使用多个命名的图层,我相信这些图层名称将作为ID直接导出到SVG代码中。