浏览器中图像的 SVG 并带有 PNG 后备

发布于 2024-10-30 05:03:21 字数 826 浏览 1 评论 0原文

我希望在网站上使用公司徽标的 SVG 版本。目前,所有当前版本的主要浏览器(IE、Safari、Chrome、Firefox、Opera)都支持 SVG,所以这看起来并不疯狂。然而,旧的浏览器仍然存在,所以我需要回退到 PNG 支持。

显而易见的解决方案是将 SVG 内容放入 object 标记中,如下所示(原谅内联样式...):

<object data='logo.svg' style='height:3em' >
    <img src='logo.png' style='height:3em' />
</object>

理论上,如果可能,应该渲染 object ,或者否则渲染img。然而,Chrome 不喜欢这样,并将 height 样式应用于对象本身,而不是 SVG,所以我最终得到了一个带有滚动条的类似 iframe 的小框,显示了一个巨大的徽标。

另一个解决方案是使用 PNG 作为 img 源,然后在渲染时使用 JavaScript 将其与 SVG 源交换(如果我认为我正在支持 SVG 的浏览器上运行)。这并不理想,因为 PNG 仍然会被下载,而且我不确信我可以正确检测 SVG 支持。不幸的是,jQuery 似乎没有 SVG 检测功能。

最后,由于我的网站是使用 ASP.NET 部署的,因此我可以在提供页面之前检查用户代理字符串,并根据我认为它是否支持 SVG 来指定 img 源。但这也有一个潜在的问题,即我不确信自己能做出正确的决定。

对图像进行 SVG 处理的首选方法是什么?

I'm looking to use SVG versions of a company logo on a website. At present, all current versions of major browsers (IE, Safari, Chrome, Firefox, Opera) support SVG, so this doesn't seem crazy. However, old browsers are still out there, so I need to fall back to PNG support.

The obvious solution is to put the SVG content in an object tag like so (forgive the inline styles...):

<object data='logo.svg' style='height:3em' >
    <img src='logo.png' style='height:3em' />
</object>

Which in theory should render the object if possible, or else render the img. However, Chrome doesn't like this and applies the height style to the object itself but not the SVG, so I end up with a little iframe-like box with scrollbars, showing a huge logo.

Another solution would be to use the PNG as the img source, and then swap it out at render time with the SVG source with javascript, if I think I'm running on a SVG-capable browser. This is not ideal because the PNG will still get downloaded, and I'm not confidant I can properly detect SVG support. Unfortunately, jQuery doesn't seem to have a SVG-detect feature.

Finally, since my website is deployed with ASP.NET, I could inspect the user agent string before serving the page, and specify the img source depending on whether I think it will support SVG. But this also has the potential problem that I am not confidant I can make the right call.

What is the preferred way of doing SVG for images?

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

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

发布评论

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

评论(6

梦魇绽荼蘼 2024-11-06 05:03:21

这是一个老问题,但这里有另一个解决方案:

  1. 下载 Modernizr 的版本,该版本被精简为仅测试 SVG(假设是 )

  2. 运行测试。如果通过,则放入 SVG。如果失败,则放入位图。本质上:

    if (!Modernizr.svg) { 
        $("#logo").css("背景图片", "url(fallback.png)"); 
    }
    

SVG 是 Modernizr 的完美用例,因为没有简单的本机方法来提供后备。

注意:浏览器不会同时加载(png 和 svg)版本。

郑重声明:如果您必须支持 IE 8 及以下版本或较旧的 Android,那么这就是您现在需要 SVG 后备的唯一原因。

This is an old question, but here is another solution:

  1. Download a version of Modernizr that is trimmed down to just testing SVG (assuming that’s the only test you need).

  2. Run the test. If it passes, put in the SVG. If it fails, put in the bitmap. Essentially:

    if (!Modernizr.svg) { 
        $("#logo").css("background-image", "url(fallback.png)"); 
    }
    

SVG is a perfect use case for Modernizr, because there is no simple native way to provide a fallback.

Note: The browser don't load both (png and svg) versions.

For the record: the only reason you would need a fallback for SVG these days if you have to support IE 8 and down, or older Android.

北陌 2024-11-06 05:03:21

我不会将其称为首选方式,但如果您想追求第二种选择,这应该检测 SVG 支持(来自 Raphaël 1.5.2):

if(window.SVGAngle || 
    document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") {
    // supports SVG
else {
    // no SVG
}

Raphaël 使用它来确定是否应该使用 VML(IE)或 SVG(其他)进行渲染。

出于好奇,为什么您的徽标采用 SVG 格式?如果您已经有 PNG 版本,这似乎需要大量工作。

I wouldn't call it the preferred way, but if you want to pursue your second option this should detect SVG support (from Raphaël 1.5.2):

if(window.SVGAngle || 
    document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") {
    // supports SVG
else {
    // no SVG
}

Raphaël uses this to determine if it should render using VML (IE) or SVG (everyone else).

Out of curiosity, why SVG for your logo? If you already have a PNG version, this seems like a lot of work.

甜点 2024-11-06 05:03:21

要解决在对象标签中调整 SVG 大小的问题:

将“preserveAspectRatio”和“viewBox”属性添加到 svg 标记。在文本编辑器中打开文件并找到标签。在该标记中,添加以下属性:

<块引用>

preserveAspectRatio="xMinYMin meet" viewBox="0 0 {width} {height}"

将 {width} 和 {height} 替换为 viewBox 的一些默认值。我使用 SVG 标签的“宽度”和“高度”属性中的值。保存 SVG,它现在应该按预期缩放。

请参阅:我该如何缩放嵌入了 的顽固 SVG标签?

对象标签中的 SVG 存在的问题是它们会吞噬点击次数。

SVG 作为带有 PNG 后备的背景图像: http://www.broken-links.com/2010/06/14/using-svg-in-backgrounds-with-png-fallback/

我最喜欢的是使用 img 标签和onerror 处理程序将 src 标签更改为 PNG。

另一个很好的资源:http://www.schepers.cc/svg/blendups/embedding。 html

To solve your problem w/resizing SVGs in the object tag:

Add "preserveAspectRatio" and "viewBox" attributes to the svg tag. Open the file in a text editor and find the tag. in that tag, add the following attributes:

preserveAspectRatio="xMinYMin meet" viewBox="0 0 {width} {height}"

Replace {width} and {height} with some defaults for the viewBox. I use the values from the "width" and "height" attributes of the SVG tag. Save the SVG and it should now scale as expected.

See: How do I scale a stubborn SVG embedded with the <object> tag?

The problem w/SVGs in the object tag, though is that they swallow the clicks.

SVG as background-image w/PNG fallback: http://www.broken-links.com/2010/06/14/using-svg-in-backgrounds-with-png-fallback/

My favorite is using the img tag and an onerror handler to change the src tag to a PNG.

Another good resource: http://www.schepers.cc/svg/blendups/embedding.html

戏舞 2024-11-06 05:03:21

你唯一需要的是CSS。首先,将后备图像声明为背景图像。然后您可以使用多个背景来添加SVG。

IE8及以下版本将忽略第二个background-image声明,因为缺乏对多个背景的支持。

顺便说一句,我在这里使用 img 元素,因为徽标是内容,而不是布局。在这种情况下使用背景图像可能看起来是错误的,但我不同意。您将获得世界上最好的:SVG 徽标,后备

HTML:

<a href="/" class="site-logo">
    <!-- base64 encoded 1x1 px big transparent gif -->
    <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="company logo">
</a>

CSS(使用多个背景图像):

caniuse:多个背景< /a>

  • PNG 适用于 IE <9、FF <3.6、Opera <10.5
  • SVG 适用于所有其他支持 SVG 的
  • Android 2.x 不会有 PNG 或 SVG,因为这些版本实际上支持多个背景,但不支持SVG
  • 的浏览器,只有一个 HTTP 请求
.site-logo > img {
    /* Dimensions of your image need to be set */
    width: 32px;
    height: 32px;

    /* Fallback for <IE9 */
    background-image: url(logo.png);

    /* multiple backgrounds are ignored by <IE9 */
    background-image: url(logo.svg), none;
}

对于支持 SVG CSS(使用线性渐变)

caniuse :CSS 渐变

  • PNG 适用于 IE <10、FF <3.6、Safari <4.0、Opera <11.1、Opera Mini、Opera Mobile <11.1
  • SVG 适用于所有其他支持 SVG 的设备(如果供应商前缀为指定)
  • 忽略 webkit 的旧渐变语法会使 Android 2.x 使用 PNG 后备
.site-logo > img {
    /* Dimensions of your image need to be set */
    width: 32px;
    height: 32px;

    background: transparent url(logo.png) center center no-repeat;
    background-image: -webkit-linear-gradient(transparent, transparent), url(logo.svg);
    background-image:         linear-gradient(transparent, transparent), url(logo.svg);
}

The only thing you need is CSS. First you declare the fallback image as a background-image. Then you can use multiple backgrounds to add the SVG.

IE8 and below will ignore the second background-image-declaration, because the lacking support of multiple backgrounds.

By the way, I'm using the img element here, because a logo is content, not layout. Using background-images might appear to be wrong in this context, but I disagree. You get the best of the worlds: SVG logo, fallback for

HTML:

<a href="/" class="site-logo">
    <!-- base64 encoded 1x1 px big transparent gif -->
    <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="company logo">
</a>

CSS (using multiple background images):

caniuse: multiple backgrounds

  • PNG for IE <9, FF <3.6, Opera <10.5
  • SVG for all the others supporting SVG
  • Android 2.x won't have a PNG or SVG, due to these versions actually supporting multiple backgrounds, but not SVG
  • There is only one HTTP request made for browsers supporting SVG
.site-logo > img {
    /* Dimensions of your image need to be set */
    width: 32px;
    height: 32px;

    /* Fallback for <IE9 */
    background-image: url(logo.png);

    /* multiple backgrounds are ignored by <IE9 */
    background-image: url(logo.svg), none;
}

CSS (using linear gradients):

caniuse: CSS gradients

  • PNG for IE <10, FF <3.6, Safari <4.0, Opera <11.1, Opera Mini, Opera Mobile <11.1
  • SVG for all the others supporting SVG (if vendor-prefixes are specified)
  • Ignoring the old gradient syntax for webkit makes Android 2.x use the PNG fallback
.site-logo > img {
    /* Dimensions of your image need to be set */
    width: 32px;
    height: 32px;

    background: transparent url(logo.png) center center no-repeat;
    background-image: -webkit-linear-gradient(transparent, transparent), url(logo.svg);
    background-image:         linear-gradient(transparent, transparent), url(logo.svg);
}
寒冷纷飞旳雪 2024-11-06 05:03:21

尝试 svg-web 他们有多种不同的方式来显示 svg 图像,包括带有自动回退功能的 Flash 。

Try svg-web they have a number of different ways of displaying svg images including flash with automatic fallback.

梦一生花开无言 2024-11-06 05:03:21

我发现的将 SVG 包含为 HTML 元素(带回退)的最佳方法是:

<svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 100 100" style="width: 100px; height: 100px; vertical-align: top;">
    <image xlink:href="image.svg" src="fallback.png" width="100%" height="100%"/>
</svg> 

优点:

  • 在我测试过的每个设备/浏览器(IE6-IE11、Android 2.0+、IOS3-)中提供回退7)
  • 每个测试浏览器仅加载一张图像(IE9-IE11 除外)
  • 外部加载的图像允许缓存图像

缺点:

  • 无法在 IE9-IE11 中用作可缩放(响应式)图像(查看此问题)
  • IE9-IE11 加载两个图像
  • IOS3- 4(Mobile Safari)具有 SVG 支持,但显示 PNG(因为它缺乏内联 SVG 支持)
  • SVG 文件不得具有高度/宽度属性(对此不确定,但已在某处读过,在我的测试中,我的 SVG 没有它们)无论如何)
  • 不验证

请提供评论以及您能想到的其他优点/缺点。我知道 SVG 在某些浏览器中可能会出现像素化,但由于使用 browserstack 进行模拟,我无法测试放大效果。

来源:http://lynn.ru/examples/svg/en.html

The best method I have found including SVG as an HTML element (with fallback) is this one:

<svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 100 100" style="width: 100px; height: 100px; vertical-align: top;">
    <image xlink:href="image.svg" src="fallback.png" width="100%" height="100%"/>
</svg> 

Pros:

  • Provides fallback in every device/browser I have tested (IE6-IE11, Android 2.0+, IOS3-7)
  • Only one image is loaded for each tested browser (except IE9-IE11)
  • Externally loaded images allows image to be cached

Cons:

  • Unable to use as scaleable (responsive) image in IE9-IE11 (see this question)
  • IE9-IE11 loads both images
  • IOS3-4 (Mobile Safari) has SVG support but displays the PNG (since it lacks inline SVG support)
  • SVG file must not have height / width attributes (unsure about this, but have read about it somewhere and in my tests my SVG did not have them anyway)
  • Does not validate

Please provide comments with additional pros / cons you can think of. I know for one SVG's can appear pixeled in some browsers, but I was unable to test zooming in since using browserstack for emulation.

Source: http://lynn.ru/examples/svg/en.html

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