为什么这个CSS转换不适用于锚内的SVG

Und*_*ion 6 css anchor svg transition hover

我正在尝试转换嵌入式SVG对象的填充和路径,但这似乎不起作用(Code Pen here):

SVG:

<a class="simple-link svg-link" href="">
  Some Text
  <svg version="1.1" id="next-page-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
      viewBox="0 0 25 25" enable-background="new 0 0 25 25" xml:space="preserve" preserveAspectRatio="xMinYMin meet">
    <circle class="the-background" cx="12.5" cy="12.5" r="12.5"/>
    <g>
      <path class="the-icon"  d="M16.088,11.421l-3.404,3.362l-3.418-3.362v-1.204l3.418,3.444l3.404-3.444V11.421z"/>
     </g>
  </svg>
</a>
Run Code Online (Sandbox Code Playgroud)

萨斯:

a
{
  width:200px;
  height:200px;
  overflow: hidden;

  @include transition(color, 1s);
  @include transition(background, 1s);

  svg
  {
    width:200px;
    height:200px;

    .the-background
    {
      @include transition(fill, 1s);
      fill: grey;
    }

    .the-icon
    {
      @include transition(fill, 2.5s);
    }
  }

  &:hover
  {
    color: red;
    background: black;
    .the-background
    {
      fill: black;
    }

    .the-icon
    {
      fill: red;
    } 

  }
}
Run Code Online (Sandbox Code Playgroud)

为什么填充动画不会悬停?

小智 12

我解决这个问题的方法是放在fill="currentColor"我想要转换的svg路径元素上.然后,我添加colortransition属性周围的锚标记,并进行锚标记,而不是SVG路径本身的CSS过渡.下面是一个非常简单的例子:

HTML:

<a>
    <svg>
        <path fill="currentColor" />
    </svg>
</a>
Run Code Online (Sandbox Code Playgroud)

SCSS:

a { color: black; transition: color .2s linear;
    &:hover { color: white; } }
Run Code Online (Sandbox Code Playgroud)


Zac*_*ier 6

转换不起作用的原因是因为它在一个链接中.

要解决这个问题,请将链接放在SVG中,而不是像SO帖子所说的那样

要么

使SVG成为链接的兄弟,并使用兄弟选择器

/* This goes within `a { ...` */
&:hover + svg { /* Or use ~ to select all */
  .the-background
  {
    fill: black;
  }

  .the-icon
  {
    fill: red;
  } 
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我刚刚发现,为了在一个锚元素中转换一个svg填充,它只能使用rgba颜色代码.我没有研究过为什么会这样,但它正在研究我的项目 - 这是一个例子:http://rawesome.leveragenewagemedia.com/(悬停社交媒体图标).

这是我正在使用的SASS:

.icon {
  display: inline-block;
  width: 20px;
  height: 20px;
  fill: rgba(0,0,0,.2);
  -webkit-transition: fill .5s;
  -moz-transition: fill .5s;
  -ms-transition: fill .5s;
  -o-transition: fill .5s;
  transition: fill .5s;

  &:hover {
    fill: rgba(0,0,0,.5);
  }
}
Run Code Online (Sandbox Code Playgroud)