在旧版浏览器中显示 PNG 代替 SVG 的条件代码?

发布于 2024-12-28 06:26:59 字数 138 浏览 2 评论 0原文

我正在寻找一种方法,让旧版浏览器在检测到时显示 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 技术交流群。

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

发布评论

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

评论(8

姐不稀罕 2025-01-04 06:26:59

使用 HTML 条件注释。

<!--[if lte IE 8]><img src="logo.png" /><![endif]-->
<!--[if gt IE 8]><img src="logo.svg" /><![endif]-->
<!--[if !IE]> --><img src="logo.svg" /><!-- <![endif]-->

http://msdn.microsoft.com/en -us/library/ms537512%28v=vs.85%29.aspx

如果您也在寻找一种方法来处理 IE 以外的浏览器,您应该使用 javascript 或 php 检查用户代理。

Use HTML conditional comments.

<!--[if lte IE 8]><img src="logo.png" /><![endif]-->
<!--[if gt IE 8]><img src="logo.svg" /><![endif]-->
<!--[if !IE]> --><img src="logo.svg" /><!-- <![endif]-->

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.

画离情绘悲伤 2025-01-04 06:26:59
<object type="image/svg+xml" data="image.svg">
    <img src="image.png" alt="image"/>
</object>
<object type="image/svg+xml" data="image.svg">
    <img src="image.png" alt="image"/>
</object>
香橙ぽ 2025-01-04 06:26:59

我使用“透明渐变”技术,因为它仅适用于CSS不需要特定于浏览器的黑客。

该技术基于这样一个事实:能够使用 CSS 渐变的浏览器足够现代,可以支持 SVG 渲染。因此,如果我们使用由两层组成的背景图像,一层是 SVG,另一层是渐变,则只有那些能够理解渐变语法的浏览器才会尝试显示 SVG。

以下代码显示了基本的 CSS 规则:

background: transparent url(fallback-image.png) center center no-repeat;
background-image: -webkit-linear-gradient(transparent, transparent), url(vector-image.svg);
background-image: -moz-linear-gradient(transparent, transparent), url(vector-image.svg);
background-image: linear-gradient(transparent, transparent), url(vector-image.svg);

通过此技术,所有用户都将看到图像,并且将使用最新浏览器版本的 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:

background: transparent url(fallback-image.png) center center no-repeat;
background-image: -webkit-linear-gradient(transparent, transparent), url(vector-image.svg);
background-image: -moz-linear-gradient(transparent, transparent), url(vector-image.svg);
background-image: linear-gradient(transparent, transparent), url(vector-image.svg);

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.

涙—继续流 2025-01-04 06:26:59

我建议您在检测到时重写 SVG 图像的 src 属性(通过 Modernizr 或类似)浏览器本身不支持 SVG。像这样的东西:

if (!Modernizr.svg) {
    var imgs = document.getElementsByTagName('img');
    var endsWithDotSvg = /.*\.svg$/
    var i=0;
    var l = imgs.length;
    for (; i != l; ++i) {
        if (imgs[i].src.match(endsWithDotSvg)) {
            imgs[i].src = imgs[i].src.slice(0, -3) + "png";
        }
    }
}

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:

if (!Modernizr.svg) {
    var imgs = document.getElementsByTagName('img');
    var endsWithDotSvg = /.*\.svg$/
    var i=0;
    var l = imgs.length;
    for (; i != l; ++i) {
        if (imgs[i].src.match(endsWithDotSvg)) {
            imgs[i].src = imgs[i].src.slice(0, -3) + "png";
        }
    }
}
荒岛晴空 2025-01-04 06:26:59

这个怎么样?

<img src="your.svg" onerror="this.src=your.png">  

另请查看 SVGeezy

How about this?

<img src="your.svg" onerror="this.src=your.png">  

Also take a look at SVGeezy.

梦太阳 2025-01-04 06:26:59

跟进 Adiabatic 的评论:
您还可以将后备 img 路径设置为数据属性。这可以让您的后备路径更加灵活。

示例(HTML+JS):

<object type="image/svg+xml" data="image.svg" data-fallback="image.png"></object>

<script>
if (!Modernizr.svg) {
    var imgs = document.getElementsByTagName('img');
    var endsWithDotSvg = /.*\.svg$/
    var i=0;
    var l = imgs.length;
    for (; i != l; ++i) {
        if (imgs[i].src.match(endsWithDotSvg)) {
           var fallback = imgs[i].getAttribute('data-fallback');
           if(typeof fallback !== "undefined" && fallback !== "")
           {
               imgs[i].src = fallback;
           }
        }
    }
}
</script>

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):

<object type="image/svg+xml" data="image.svg" data-fallback="image.png"></object>

<script>
if (!Modernizr.svg) {
    var imgs = document.getElementsByTagName('img');
    var endsWithDotSvg = /.*\.svg$/
    var i=0;
    var l = imgs.length;
    for (; i != l; ++i) {
        if (imgs[i].src.match(endsWithDotSvg)) {
           var fallback = imgs[i].getAttribute('data-fallback');
           if(typeof fallback !== "undefined" && fallback !== "")
           {
               imgs[i].src = fallback;
           }
        }
    }
}
</script>
陪我终i 2025-01-04 06:26:59

我更喜欢内容图像的“条件注释 SVG 后备技术”,如 David Goss 所描述的,如果图像没有装饰的话。

HTML:

<a class="site-logo" href="/">
    <!--[if gte IE 9]><!--><img alt="Acme Supplies" src="logo.svg" data-fallback="logo.png" onerror="this.src=this.getAttribute('data-fallback');this.onerror=null;" /><!--<![endif]-->
    <!--[if lt IE 9]><img alt="Acme Supplies" src="logo.png" /><![endif]-->
</a>

http://davidgoss.co.uk/2013/01/30/use-svg-for-your-logo-and-still-support-old-ie-and-android/

对于装饰图像,我使用“透明渐变 SVG 回退技术”,如 Pau Giner 所描述的。

CSS:

.icon-file {
    background: transparent url(../images/file.png) center center no-repeat;
    background-image: -webkit-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:      -o-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:    -moz-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:     -ms-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:         linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-size: 100%;
}

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:

<a class="site-logo" href="/">
    <!--[if gte IE 9]><!--><img alt="Acme Supplies" src="logo.svg" data-fallback="logo.png" onerror="this.src=this.getAttribute('data-fallback');this.onerror=null;" /><!--<![endif]-->
    <!--[if lt IE 9]><img alt="Acme Supplies" src="logo.png" /><![endif]-->
</a>

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:

.icon-file {
    background: transparent url(../images/file.png) center center no-repeat;
    background-image: -webkit-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:      -o-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:    -moz-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:     -ms-linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-image:         linear-gradient(transparent,transparent), url(../images-svg/file.svg);
    background-size: 100%;
}

http://pauginer.com/post/36614680636/invisible-gradient-technique

挖个坑埋了你 2025-01-04 06:26:59

这个方法对我有用:

<?php
$browser = get_browser(null, true);
$extension = "png";

if ($browser['browser'] == "Chrome" && $browser['version'] >= 49){
    $extension = "svg";
}else if($browser['browser'] == "Firefox" && $browser['version'] >= 57){
    $extension = "svg";
}else if($browser['browser'] == "Opera" && $browser['version'] >= 49){
    $extension = "svg";
}else if($browser['browser'] == "IE" && $browser['version'] >= 11){
    $extension = "svg";
}else if($browser['browser'] == "Safari" && $browser['version'] >= 11){
    $extension = "svg";

}
?>

由于缺陷,你告诉变量“$extension”为PNG,但如果浏览器版本能够渲染SVG,那么该变量将更改为SVG。

然后在 HTML 中,您只需完成文件扩展名即可对变量进行回显。

<img src="image.<?php echo $extension ?>">

现在,您的网页将根据浏览器版本加载“image.png”或“image.svg”。

重要信息:

** 为了使该脚本正常工作,您需要设置文件 browscap.ini,以便您可以使用 PHP 函数“get_browser”。

** 有关 SVG 的浏览器功能是从此网站获得的:

https://caniuse.com/#search=svg< /a>

This method worked for me:

<?php
$browser = get_browser(null, true);
$extension = "png";

if ($browser['browser'] == "Chrome" && $browser['version'] >= 49){
    $extension = "svg";
}else if($browser['browser'] == "Firefox" && $browser['version'] >= 57){
    $extension = "svg";
}else if($browser['browser'] == "Opera" && $browser['version'] >= 49){
    $extension = "svg";
}else if($browser['browser'] == "IE" && $browser['version'] >= 11){
    $extension = "svg";
}else if($browser['browser'] == "Safari" && $browser['version'] >= 11){
    $extension = "svg";

}
?>

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.

<img src="image.<?php echo $extension ?>">

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

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