如何检测不渲染 .png 透明度的浏览器
我有这段代码可以根据一周中的某一天渲染图像。但在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对此问题进行了大量研究,得出的结论是,在 JavaScript 中创建功能测试是不可行的,并且 IE 6 是唯一不支持 PNG 透明度的认真使用的浏览器。既然如此,最好的选择是加布里埃尔·罗斯 (Gabriel Ross) 推荐的条件注释。可以在纯 JavaScript 中使用条件注释,我认为这就是您想要的:
更新 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:
UPDATE 17 June 2012
Note that IE 10 will not support conditional comments, so this approach will not work in future versions of IE.
有条件评论:
Conditional comments:
我认为没有一种好的通用方法来检测是否支持 png 透明度。然而,现在除了 IE6 和极少数的 IE5.5 之外,所有主流浏览器都支持它。我认为您不必担心任何其他情况。
取自此处。
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.
Taken from here.
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).