在旧版浏览器中显示 PNG 代替 SVG 的条件代码?
我正在寻找一种方法,让旧版浏览器在检测到时显示 PNG 图像来代替 SVG 作为后备。我网站的徽标当前采用 SVG 格式,但较旧的浏览器(特别是 IE 8 及更低版本)不会呈现它。我已经有了 PNG 格式的徽标。执行此操作的最佳方法是什么?
谢谢
I'm looking for a way to have older browsers display a PNG image in place of an SVG as a fallback when detected. The logo for my site is currently in SVG but older browsers, specifically IE 8 and below won't render it. I already have the logo in PNG. What's the best way to execute this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 HTML 条件注释。
http://msdn.microsoft.com/en -us/library/ms537512%28v=vs.85%29.aspx
如果您也在寻找一种方法来处理 IE 以外的浏览器,您应该使用 javascript 或 php 检查用户代理。
Use HTML conditional comments.
http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx
If you're also looking for a way to handle this for browsers other than IE, you should check the user agent with javascript or php.
我使用“透明渐变”技术,因为它仅适用于CSS不需要特定于浏览器的黑客。
该技术基于这样一个事实:能够使用 CSS 渐变的浏览器足够现代,可以支持 SVG 渲染。因此,如果我们使用由两层组成的背景图像,一层是 SVG,另一层是渐变,则只有那些能够理解渐变语法的浏览器才会尝试显示 SVG。
以下代码显示了基本的 CSS 规则:
通过此技术,所有用户都将看到图像,并且将使用最新浏览器版本的 SVG 显示图像。付出的代价是,一些也能够渲染 SVG 但不支持渐变的旧浏览器版本(例如 IE9 或 Firefox 3.5)将显示后备版本。
I use the "transparent gradient" technique because is CSS-only and does not require browser-specific hacks.
The technique is based on the fact that browsers capable of using CSS gradients are modern enough to support SVG rendering. So, if we use a background image that is composed of two layers, one being the SVG and the other being a gradient, only those browsers capable of understanding the gradient syntax will try to display the SVG.
The following code shows the basic CSS rules:
With this technique, all users will see the image and it will be displayed using SVG for the latest browser versions. The price to pay is that some old browser versions (such as IE9 or Firefox 3.5) that are also capable of rendering SVG but do not support gradients will display the fallback version.
我建议您在检测到时重写 SVG 图像的
src
属性(通过 Modernizr 或类似)浏览器本身不支持 SVG。像这样的东西:I suggest rewriting the
src
attribute of your SVG images when you detect (via Modernizr or similar) that the browser doesn't support SVG natively. Something like:这个怎么样?
另请查看 SVGeezy。
How about this?
Also take a look at SVGeezy.
跟进 Adiabatic 的评论:
您还可以将后备 img 路径设置为数据属性。这可以让您的后备路径更加灵活。
示例(HTML+JS):
To follow up on Adiabatic's comment:
You could also set the fallback img path as data-attribute. This allows for more flexibility in your fallback paths.
Example (HTML +JS):
我更喜欢内容图像的“条件注释 SVG 后备技术”,如 David Goss 所描述的,如果图像没有装饰的话。
HTML:
http://davidgoss.co.uk/2013/01/30/use-svg-for-your-logo-and-still-support-old-ie-and-android/
对于装饰图像,我使用“透明渐变 SVG 回退技术”,如 Pau Giner 所描述的。
CSS:
http://pauginer.com/post/36614680636/invisible-gradient-technique
I prefer the "conditional comments SVG fallback technique" for content images, as described by David Goss, if the images are no decoration.
HTML:
http://davidgoss.co.uk/2013/01/30/use-svg-for-your-logo-and-still-support-old-ie-and-android/
For decorational images, I use the "transparent gradient SVG fallback technique", as described by Pau Giner.
CSS:
http://pauginer.com/post/36614680636/invisible-gradient-technique
这个方法对我有用:
由于缺陷,你告诉变量“$extension”为PNG,但如果浏览器版本能够渲染SVG,那么该变量将更改为SVG。
然后在 HTML 中,您只需完成文件扩展名即可对变量进行回显。
现在,您的网页将根据浏览器版本加载“image.png”或“image.svg”。
重要信息:
** 为了使该脚本正常工作,您需要设置文件 browscap.ini,以便您可以使用 PHP 函数“get_browser”。
** 有关 SVG 的浏览器功能是从此网站获得的:
https://caniuse.com/#search=svg< /a>
This method worked for me:
By defect you are telling the variable"$extension" to be PNG, but if the browser version is capable of rendering SVG, then the variable will change to SVG.
Then in your HTML you just complete the file extension making an echo of the variable.
Now your web page will load "image.png" or "image.svg" depending on browser version.
Important information:
** in order to make this script work you need to set up the file browscap.ini so you can use the PHP function "get_browser".
** Browser capabilities regarding SVG were obtained from this website:
https://caniuse.com/#search=svg