【问题标题】:CSS keyframe animation not working on an SVGCSS关键帧动画不适用于SVG
【发布时间】:2015-01-15 14:57:24
【问题描述】:

请看:

http://codepen.io/richardstelmach/pen/RNwvyG

“svg”是html中的id。

CSS 是:

#svg{
  display:block;
  max-height:400px;
  margin:0 auto;
  animation:filters 2s infinite;
}

#svg .colour1{
  fill:#2bb0b7;
}

#svg .colour2{
  fill:#ab3e41;
}

/* animate effects */

@keyframes filters {
  0%{ 
    filter:hue-rotate(0deg); 
  }
  100% { 
    filter:hue-rotate(360deg); 
  }
}

动画不工作。我尝试将其更改为特定的 -webkit- CSS,并尝试将其应用于“. colour1”类,但无济于事。

我还尝试使用动画填充而不是使用色调旋转。但同样,没有运气。

【问题讨论】:

    标签: html css svg css-animations


    【解决方案1】:

    只需添加供应商前缀及其美观:

    @keyframes filters {
      0%{ 
        -webkit-filter:hue-rotate(0deg); 
      }
      100% { 
        -webkit-filter:hue-rotate(360deg); 
      }
    }
    

    【讨论】:

    • 谢谢。请注意,对于旧版本的 webkit 浏览器,“动画”还需要一个前缀。理想情况下,遗憾的是,上述内容目前需要各种前缀。 caniuse.com/#feat=css-animation
    【解决方案2】:

    您还需要为过滤器添加前缀:

    DEMO

    @-webkit-keyframes filters {
      0%{ 
        -webkit-filter:hue-rotate(0deg); 
      }
      100% { 
        -webkit-filter:hue-rotate(360deg); 
      }
    }
    

    【讨论】:

    • 谢谢,太好了。我一定没有为所有内容添加前缀。惭愧,我以为它现在已经兼容了。
    【解决方案3】:

    基本上需要为浏览器添加前缀。 :

    #svg{
      display:block;
      max-height:400px;
      margin:0 auto;
      -webkit-animation:filters 20s infinite;
    }
    
    #svg .colour1{
      fill:#2bb0b7;
    }
    
    #svg .colour2{
      fill:#ab3e41;
    }
    
    /* animate effects */
    
    @-webkit-keyframes filters{
      0%{ 
        -webkit-filter:hue-rotate(0deg); 
      }
      100% { 
        -webkit-filter:hue-rotate(360deg); 
      }
    }
    

    此处完成代码:http://codepen.io/richardstelmach/pen/RNwvyG

    需要为其他浏览器添加其他前缀。

    【讨论】:

    • 您不需要在所有内容前加上过滤器属性。支持 -webkit-filter 的浏览器会有无前缀动画
    • 我尝试从动画属性中去掉 -webkit- 前缀,但它不起作用(在 google chrome 版本 38 中)。据此:caniuse.com/#feat=css-animation。它仅适用于解释它的前缀。最新版本的 chrome 可以在没有前缀的情况下使用。
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 2020-03-07
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多