简体   繁体   English

使用SVG作为带有缩放的CSS3背景图像

[英]Using SVG as CSS3 background-image with scaling

When I use SVG in background property like this: 当我在后台属性中使用SVG时,如下所示:

.svg-button {
    -webkit-transform: scale(3.0) // root of the problem!
    background: url(Button.svg) no-repeat;
    width: 32px;
    height: 32px;
}

I get blurred image as result. 结果我得到了模糊的图像。 At the same time text in tag with this style is clear. 同时用这种风格标记的文字很清楚。 Also if I scale page by using CTRL++ (browser zoom) instead transform property everything is clear. 此外,如果我使用CTRL ++(浏览器缩放)缩放页面而不是转换属性,一切都很清楚。

If I replace CSS background property on: 如果我替换CSS背景属性:

<object type="image/svg+xml" data="Button.svg" width="32" height="32"></object>

the image is clear in any scale in any case. 在任何情况下,图像都以任何比例清晰。

Where is the problem? 问题出在哪儿?

Sample on jsfiddle 关于jsfiddle的示例

Update: I found some more information about this problem: 更新:我找到了有关此问题的更多信息:

  1. StackOverflow question StackOverflow问题
  2. Bug ticket for Chrome (I tried my test under Safari/Chrome/IE9/10 and behaviour is the same. Chrome的错误票证 (我在Safari / Chrome / IE9 / 10下尝试了我的测试,行为也一样。

I was "playing" with this a while back and noticed this for fonts too. 我正在“玩”这一段时间,并注意到这也是字体。 Although it seems to be fixed now (for the fonts at least). 虽然它现在似乎已修复(至少对于字体)。

As far as I understand the inner workings, the contents of the scaled element are mapped to a texture, which in turn is scaled. 据我了解内部工作原理,缩放元素的内容被映射到纹理,而纹理又被缩放。

As a workaround, try using a 3d translation and move the element on the z-axis to achieve the size change. 作为解决方法,尝试使用3d平移并在z轴上移动元素以实现大小更改。 This won't yield as much control over the final outcome though. 尽管如此,这并不会对最终结果产生那么多的控制。

.svg-button {
    -webkit-transform: perspective(800px) translateZ(-300px);
    background: url(Button.svg) no-repeat;
    width: 32px;
    height: 32px;
}

For Chrome/Safari IE9/10 I have decided to use CSS zoom property instead scale property. 对于Chrome / Safari IE9 / 10,我决定使用CSS缩放属性而不是缩放属性。

.svg-button {
    zoom: 300%;
    background: url(Button.svg) no-repeat;
    width: 32px;
    height: 32px;
}

For Firefox I still use CSS scale property because Firefox doesn't support zoom property. 对于Firefox,我仍然使用CSS scale属性,因为Firefox不支持缩放属性。 At the same time Firefox scales SVG background well. 同时Firefox很好地扩展了SVG背景。 See result . 看结果

For IE9 I have written javascript which temporary modifies CSS width property and after small delay returns it back. 对于IE9,我编写了javascript临时修改CSS宽度属性,并在小延迟后返回它。 In this way I force redraw CSS background. 通过这种方式,我强制重绘CSS背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM