网页中是否有相当于 SVG 图像精灵的功能?

发布于 2024-09-30 03:07:37 字数 362 浏览 0 评论 0原文

SVG 图像不是位图,因此(除非我遗漏了某些内容)您不能像处理网页上使用的其他图像文件那样进行精灵绘制(请参阅 http://www.alistapart.com/articles/sprites)。

但是是否有一种等效的机制可以仅将 SVG 图像的一部分显示为 CSS 背景呢?

例如虚语法:

div.my-special-svg-div {
    background-image: url(sprite-image.svg#one-shape-in-the-svg-file);
}

SVG images aren’t bitmaps, so (unless I’m missing something) you can’t do spriting like you can with other images files used on web pages (see http://www.alistapart.com/articles/sprites).

But is there an equivalent mechanism to display only part of an SVG image as a CSS background?

E.g. imaginary syntax:

div.my-special-svg-div {
    background-image: url(sprite-image.svg#one-shape-in-the-svg-file);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失去的东西太少 2024-10-07 03:07:37

您可以使用“传统”CSS sprite 技术来分割具有背景位置的 SVG 图像,这是一个简单的示例,尽管如果您也开始使用背景大小,它确实会有点混乱。如果您在 SVG 图像上定义尺寸,可能会更容易:

<svg version="1.1" 
     xmlns:svg="http://www.w3.org/2000/svg"
     xmlns="http://www.w3.org/2000/svg"
     width="320"
     height="240"
     viewBox="0 0 320 240">

You can use 'traditional' CSS sprite techniques to slice up SVG images with background position, here's a quick example, though it does get a little confusing if you also start using background-size. It's probably easier if you define a size on your SVG image:

<svg version="1.1" 
     xmlns:svg="http://www.w3.org/2000/svg"
     xmlns="http://www.w3.org/2000/svg"
     width="320"
     height="240"
     viewBox="0 0 320 240">
心不设防 2024-10-07 03:07:37

我尝试过的一种方法不是 spriting,但实现了类似的目标,即使用数据 URI 将 url 编码的 SVG 图像直接包含在 CSS 文件中。

例如

background-image: url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0A%20%20%3Crect%20fill%3D%22%23CCD%22%20%20width%3D%22100%25%22%20height%3D%22100%25%22%2F%3E%0A%20%20%3Crect%20fill%3D%22%23039%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20rx%3D%221em%22%2F%3E%0A%3C%2Fsvg%3E);

(参见http://intertwingly.net/blog/2008/09/ 07/SVG-via-CSS

因此,所有 SVG 图像最终都会出现在 CSS 文件中。 Gzipping 应该可以很好地将多个 SVG 文件压缩到一个 CSS 文件中。

据我所知,上面的 CSS 适用于 Opera 9.5+、IE 9 beta、Safari 5 和 Chrome 6。似乎不适用于 Firefox 3.6 或其他浏览器的早期版本。

One approach I’ve tried that isn’t spriting, but achieves similar aims, is to include url-encoded SVG images directly in the CSS file, using data URIs.

E.g.

background-image: url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0A%20%20%3Crect%20fill%3D%22%23CCD%22%20%20width%3D%22100%25%22%20height%3D%22100%25%22%2F%3E%0A%20%20%3Crect%20fill%3D%22%23039%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20rx%3D%221em%22%2F%3E%0A%3C%2Fsvg%3E);

(See http://intertwingly.net/blog/2008/09/07/SVG-via-CSS)

So, all your SVG images end up in the CSS file. Gzipping should squish multiple SVG files in one CSS file quite nicely.

As far as I can tell, the CSS above works in Opera 9.5+, IE 9 beta, Safari 5, and Chrome 6. Doesn’t seem to work in Firefox as of 3.6, or earlier versions of the other browsers.

北凤男飞 2024-10-07 03:07:37

SVG 在技术上甚至不受 W3C 标准的支持。如果没有插件,它甚至无法在 IE 中运行。也许你会发现一些不起眼的 Mozilla 插件可以让你做到这一点,但据我所知你的代码不会验证。

SVG isn't even technically supported by W3C standards. It won't even work in IE without a plugin. Maybe you'll find some obscure Mozilla plugin that lets you do that but as far as I know your code won't validate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文