Can you modify a loaded SVG background-image with CSS?

谁都会走 提交于 2019-12-23 18:52:14

问题


We're using SVGs for the first time for our mobile HTML5 based app sprites file (they make dealing with retina an other screen size differences easy).

I know generated SVGs can be manipulated directly (as they are text files) but can one modify an SVG asset (in this case, a background image loaded via the CSS) via CSS or scripting?

For instance, can we load an all-black SVG object, and then change it to white?


回答1:


You can't access the DOM of the SVG file if it's loaded as an image (either through img or background-image). You can't style it via CSS either.

If you intend to do these kind of manipulations you should append the SVG to a div. You should still be able to perform sprite-like manipulations to that div. You will be able to style it via CSS or javascript.


The SVG should be inlined. You can copy and paste the SVG into the div that used to contain background-image or load it there through an ajax request.

Instead of animating background-position you can animate with left or -webkit-transform: translate(...).



来源:https://stackoverflow.com/questions/13391873/can-you-modify-a-loaded-svg-background-image-with-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!