如何检测不渲染 .png 透明度的浏览器

发布于 2024-11-29 02:08:00 字数 784 浏览 2 评论 0原文

我有这段代码可以根据一周中的某一天渲染图像。但在 IE6 及更低版本以及可能其他一些浏览器中,它不会呈现 png 不透明度。所以我想稍微改变一下,这样它就会检测到不渲染 alpha 透明度的浏览器,并告诉他们加载这个图像:“img/horarios2.png”。我尝试过这样做,以便排除 IE6 及更低版本的浏览器,这些浏览器因不渲染而闻名,但后来我想到了所有其他我可能不知道的浏览器,这些浏览器也不渲染,并且需要一些东西来统治它们出也。我不知道完成此任务的最佳方法,所以我愿意接受所有建议。

$(document).ready (function horario () {
var date = new Date();
var weekday = (date.getDay());

if (weekday==0)
    document.getElementById('horarios').src = "img/domingo.png";
else if (weekday==4)
    document.getElementById('horarios').src = "img/quinta.png";
else if (weekday==5)
    document.getElementById('horarios').src = "img/sexta.png";
else if (weekday==6)
    document.getElementById('horarios').src = "img/sabado.png";
else 
    document.getElementById('horarios').src = "img/quarta.png";
});

I have this code that render's a image acording to the day of the week. But in IE6 and lower and probably some other browsers it won't render png opacity. So I want to change it a litle so that it will detect the browser's that don't render alpha transparency and tell them to load this image instead: "img/horarios2.png". Ive tried making it so that it would rule out IE6 and lower that are known for not rendering, but then I thinking about all the other browsers that I probably don't know about that don't render either and needed something that would rule them out also. I don't know the best way to acomplish this so im open to all sugestions.

$(document).ready (function horario () {
var date = new Date();
var weekday = (date.getDay());

if (weekday==0)
    document.getElementById('horarios').src = "img/domingo.png";
else if (weekday==4)
    document.getElementById('horarios').src = "img/quinta.png";
else if (weekday==5)
    document.getElementById('horarios').src = "img/sexta.png";
else if (weekday==6)
    document.getElementById('horarios').src = "img/sabado.png";
else 
    document.getElementById('horarios').src = "img/quarta.png";
});

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

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

发布评论

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

评论(4

淡淡の花香 2024-12-06 02:08:01

我对此问题进行了大量研究,得出的结论是,在 JavaScript 中创建功能测试是不可行的,并且 IE 6 是唯一不支持 PNG 透明度的认真使用的浏览器。既然如此,最好的选择是加布里埃尔·罗斯 (Gabriel Ross) 推荐的条件注释。可以在纯 JavaScript 中使用条件注释,我认为这就是您想要的:

var div = document.createElement("div");
div.innerHTML = "<!--[if lte IE 6]><i></i><![endif]-->";
var isIe6orLower = (div.getElementsByTagName("i").length == 1);

alert("Is IE 6 or lower: " + isIe6orLower);

更新 2012 年 6 月 17 日

请注意 IE 10 将不支持条件注释,因此这种方法在未来版本的 IE 中将不起作用。

I've done a reasonable amount of research into this this issue and came to the conclusion that it was not feasible to create a feature test in JavaScript, and that IE 6 is the only browser in serious use that doesn't support PNG transparency. That being the case, your best bet is conditional comments, as recommended by Gabriel Ross. It is possible to use conditional comments in pure JavaScript, which I think is what you want:

var div = document.createElement("div");
div.innerHTML = "<!--[if lte IE 6]><i></i><![endif]-->";
var isIe6orLower = (div.getElementsByTagName("i").length == 1);

alert("Is IE 6 or lower: " + isIe6orLower);

UPDATE 17 June 2012

Note that IE 10 will not support conditional comments, so this approach will not work in future versions of IE.

℡Ms空城旧梦 2024-12-06 02:08:01

有条件评论:

<!--[if IE 6]>
<script>
var ie6 = true;
</script>
<![endif]-->

Conditional comments:

<!--[if IE 6]>
<script>
var ie6 = true;
</script>
<![endif]-->
忆离笙 2024-12-06 02:08:01

我认为没有一种好的通用方法来检测是否支持 png 透明度。然而,现在除了 IE6 和极少数的 IE5.5 之外,所有主流浏览器都支持它。我认为您不必担心任何其他情况。

var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");

if( badBrowser ) {
    // change to "img/horarios2.png"
}

取自此处

I don't think there's a good, generic way of detecting whether or not png transparency is supported. However I all major browsers support it nowadays except for IE6 and very rarely IE5.5. I don't think you'll have to reasonably worry about any other cases.

var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");

if( badBrowser ) {
    // change to "img/horarios2.png"
}

Taken from here.

一梦等七年七年为一梦 2024-12-06 02:08:01

IE6 可以通过 非标准 css 过滤器 渲染 png 透明度,该过滤器调用 directX 来处理透明度代表 IE。

至于其他浏览器,只有IE愚蠢到只添加了部分PNG支持。如果浏览器支持 png,您可以放心地假设它也可以处理透明度(IE 除外)。

IE6 can render png transparency via a non-standard css filter, which invokes directX to handle the transparency on IE's behalf.

As for other browsers, only IE was stupid enough to add only partial PNG support. If a browser supports png, you can safely assume it handles transparency as well (except if it's IE).

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