登录  /  注册
首页 > web前端 > css教程 > 正文

Cross-Browser Variable Opacity with PNG_经验交流

php中文网
发布: 2016-05-16 12:09:43
原创
1744人浏览过

Periodically, someone tells me about the magic of PNG, how it's the ideal image format for the web, and that someday we'll all be using it on our sites instead of GIF. People have been saying this for years, and by now most of us have stopped listening. Sadly, flaky browser support has made PNG impractical for almost everything; but now, with a few simple workarounds, we can finally put one of its most compelling features to use.

PNG? What?

The Portable Network Graphics, or PNG (pronounced “ping”), image format has been around since 1995, having cropped up during the now long-forgotten GIF scare, when Compuserve and Unisys announced they would begin charging royalties for the use of the GIF image format.

To provide GIF support in their applications, software makers like Adobe and Macromedia must pay royalty fees – fees which are passed down to the end user in the selling cost of the software.

When PNG appeared on the scene, web designers were ready to make the switch to the free, superior format and shun GIF forever. But over time, browsers continually failed to support PNG, and eventually most people started to forget about it. Today, nearly everyone still uses GIF habitually.

Which is a shame, because PNG makes GIF look pretty pathetic: it supports gamma correction, (sometimes) smaller file sizes, loss-less compression, up to 48-bit color, and, best of all, true alpha transparency.

To get why alpha transparency is a big deal, we must first understand one of the most annoying limitations of GIF.

Binary Transparency: the Scourge of GIF

When it comes to transparency, GIF doesn't cut it. Whereas PNG supports alpha transparency, GIF only supports binary transparency, which is a big limitation and has a couple of important implications.

For one, a GIF image can either use no transparent colors at all or have one color that's completely transparent – there are no degrees of transparency.

And if a complex GIF does contain a transparent color, the background color of the web page must match the transparent color, or else the anti-aliased area around the transparent color will be surrounded by ugly haloing and fringing. If you've spent more than five minutes as a web designer, you know what I'm talking about.

The result is that any anti-aliased transparent GIF is inextricably tied to the background color of the web page on which it lives. If you ever decide to change that color, you must also change the GIF.

Miraculously, PNG doesn't behave that way. A PNG can be transparent in varying degrees – in other words, it can be of variable opacity. And a transparent PNG is background-independent: it can live on any background color or image. Say you want your navigation on monkeys-run-amuck.com to be 65% opaque so you can see through it to your orangutan background image. You can do that. A transparent anti-aliased “Gorillas, Chimps, Gibbons, et al” title that can sit on top of any background color or image? You can do that, too.

So What About Browser Support?

By now, of course, we'd all be up to our ears in PNGs if browsers supported them reliably. But seven years after the format's inception, you still can't slap a PNG onto a web page like you can a GIF or JPG. It's disgraceful, but not as bad as it sounds.

It turns out that most of the latest versions of the major browsers fully support alpha transparency with PNG – namely, Netscape 6, Opera 6, and recently-released Mozilla 1, all on Windows; and, for the Mac, Internet Explorer 5, Netscape 6, Opera 5, Mozilla 1, OmniWeb 3.1, and ICab 1.9. Incredibly, PNG even works on Opera 6 for Linux, on WebTV, and on Sega Dreamcast.

Now, what's missing from that list?

IE5.5+/Win, bless its heart, will, in fact, display a PNG, but it doesn't natively support alpha transparency. In IE5.5+/Win, the transparent area of your PNG will display at 100% opacity – that is, it won't be transparent at all.

Bugger. So what do we do now?

Proprietary Code-o-Rama: the AlphaImageLoader Filter

IE4+/Win supports a variety of non-standard, largely ridiculous visual filters that you can apply to any image's style. You can, for instance, fade in an image with a gradient wipe, or make it stretch from nothing to full size, or even make it swipe into place circularly, like a scene change in Star Wars.

A non-pointless gem among these is the AlphaImageLoader filter, which is supported in IE5.5+/Win. When used to display a PNG, it allows for full alpha transparency support. All you have to do is this:

<div id="myDiv" style="position:relative; 
	height:250px; 
	width:250px;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader&lt;SPAN class=linewrap&gt;»&lt;/SPAN&gt;
(src='myimage.png',sizingMethod='scale');"></div>
登录后复制

(Line wraps are marked ». –Ed.)

And you're in business. Perfect alpha transparency. This code works great, with only the small drawback that it's not part of any accepted web standard, and no other browser on the planet understands it.

Serving up PNGs with JavaScript

So the trick is to determine the user's browser and serve up the images appropriately: if IE5.5+/Win, then we use AlphaImageLoader; if a browser with native PNG support, then we display PNGs the normal way; if anything else, then we display alternate GIFs, because we can't be sure that a PNG will display correctly or at all.

Using a slightly tweaked version of Chris Nott's Browser Detect Lite, we set some global variables to this effect that we can use later on.

// if IE5.5+ on Win32, then display PNGs with AlphaImageLoader
if ((browser.isIE55 || browser.isIE6up) &amp;&amp; browser.isWin32) {
	var pngAlpha = true;
// else, if the browser can display PNGs normally, then do that
} else if ((browser.isGecko) |<span class="linewrap">»</span>
| (browser.isIE5up &amp;&amp; browser.isMac) |<span class="linewrap">»</span>
| (browser.isOpera &amp;&amp; browser.isWin <span class="linewrap">»</span>
	&amp;&amp; browser.versionMajor &gt;= 6) |<span class="linewrap">»</span>
| (browser.isOpera &amp;&amp; browser.isUnix <span class="linewrap">»</span>
&amp;&amp; browser.versionMajor &gt;= 6) |<span class="linewrap">»</span>
| (browser.isOpera &amp;&amp; browser.isMac <span class="linewrap">»</span>
	&amp;&amp; browser.versionMajor &gt;= 5) |<span class="linewrap">»</span>
| (browser.isOmniweb &amp;&amp; <span class="linewrap">»</span>
	browser.versionMinor &gt;= 3.1) |<span class="linewrap">»</span>
| (browser.isIcab &amp;&amp; <span class="linewrap">»</span>
	browser.versionMinor &gt;= 1.9) |<span class="linewrap">»</span>
| (browser.isWebtv) |<span class="linewrap">»</span>
| (browser.isDreamcast)) {
	var pngNormal = true;
}
登录后复制

(Note for the faint of heart: complete source code for all the examples we cover is available at the end of the article.)

Tactic 1: Quick and Dirty with document.writes

The simplest, most reliable way to spit out PNGs is using inline document.writes based on the above detection. So we use a function like this:

function od_displayImage(strId, strPath, intWidth, <span class="linewrap">»</span>
	intHeight, strClass, strAlt) {	
 if (pngAlpha) {
 document.write('<div style="height:'+intHeight+'px;&lt;SPAN class=linewrap&gt;»&lt;/SPAN&gt;
		width:'+intWidth+'px;&lt;SPAN class=linewrap&gt;»&lt;/SPAN&gt;
 filter:progid:DXImageTransform.Microsoft.AlphaImageLoader&lt;SPAN class=linewrap&gt;»&lt;/SPAN&gt;
 (src=\''+strPath+'.png\', sizingMethod=\'scale\')" class="linewrap">»
	id="'+strId+'" class="'+strClass+'"&gt;</div>');
	} else if (pngNormal) {
 document.write('<img  src="'+strPath+'.png" class="linewrap" alt="Cross-Browser Variable Opacity with PNG_经验交流" >»
	width="'+intWidth+'"<span class="linewrap">»</span>
 height="'+intHeight+'" name="'+strId+'" <span class="linewrap">»</span>
	border="0" class="'+strClass+'" alt="'+strAlt+'" /&gt;');
	} else {
 document.write('<img  src="'+strPath+'.gif" class="linewrap" alt="Cross-Browser Variable Opacity with PNG_经验交流" >»
	width="'+intWidth+'"<span class="linewrap">»</span>
 height="'+intHeight+'" name="'+strId+'" <span class="linewrap">»</span>
	border="0" class="'+strClass+'" alt="'+strAlt+'" /&gt;');
	}
}
登录后复制

Now we can call the od_displayImage function from anywhere on the page. Any JavaScript-capable browser will display an image, and, if we want to be really careful, we can accompany each call with a

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号