浏览器中图像的 SVG 并带有 PNG 后备
我希望在网站上使用公司徽标的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个老问题,但这里有另一个解决方案:
下载 Modernizr 的版本,该版本被精简为仅测试 SVG(假设是 )
运行测试。如果通过,则放入 SVG。如果失败,则放入位图。本质上:
SVG 是 Modernizr 的完美用例,因为没有简单的本机方法来提供后备。
注意:浏览器不会同时加载(png 和 svg)版本。
郑重声明:如果您必须支持 IE 8 及以下版本或较旧的 Android,那么这就是您现在需要 SVG 后备的唯一原因。
This is an old question, but here is another solution:
Download a version of Modernizr that is trimmed down to just testing SVG (assuming that’s the only test you need).
Run the test. If it passes, put in the SVG. If it fails, put in the bitmap. Essentially:
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.
我不会将其称为首选方式,但如果您想追求第二种选择,这应该检测 SVG 支持(来自 Raphaël 1.5.2):
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):
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.
要解决在对象标签中调整 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:
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
你唯一需要的是CSS。首先,将后备图像声明为背景图像。然后您可以使用多个背景来添加SVG。
IE8及以下版本将忽略第二个
background-image
声明,因为缺乏对多个背景的支持。顺便说一句,我在这里使用
img
元素,因为徽标是内容,而不是布局。在这种情况下使用背景图像可能看起来是错误的,但我不同意。您将获得世界上最好的:SVG 徽标,后备HTML:
CSS(使用多个背景图像):
caniuse:多个背景< /a>
对于支持 SVG CSS(使用线性渐变)
: caniuse :CSS 渐变
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 forHTML:
CSS (using multiple background images):
caniuse: multiple backgrounds
CSS (using linear gradients):
caniuse: CSS gradients
尝试 svg-web 他们有多种不同的方式来显示 svg 图像,包括带有自动回退功能的 Flash 。
Try svg-web they have a number of different ways of displaying svg images including flash with automatic fallback.
我发现的将 SVG 包含为 HTML 元素(带回退)的最佳方法是:
优点:
缺点:
请提供评论以及您能想到的其他优点/缺点。我知道 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:
Pros:
Cons:
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