<DIV ID="myDiv"
STYLE="position:relative;
height:250px;
width:250px;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader»
(src='myimage.png',sizingMethod='scale');"></DIV>
(Line wraps are marked ». –[i]Ed.[/i])
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.
[h1]Serving up PNGs with JavaScript[/h1]
So the trick is to determine the user's browser and serve up the images appropriately: if IE5.5+/Win, then we use [code]AlphaImageLoader[/code]; 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 [url=http://www.dithered.com/javascript/browser_detect/index.html]Browser Detect Lite[/url], 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) && browser.isWin32) {
var pngAlpha = true;
// else, if the browser can display PNGs normally, then do that
} else if ((browser.isGecko) |»
| (browser.isIE5up && browser.isMac) |»
| (browser.isOpera && browser.isWin »
&& browser.versionMajor >= 6) |»
| (browser.isOpera && browser.isUnix »
&& browser.versionMajor >= 6) |»
| (browser.isOpera && browser.isMac »
&& browser.versionMajor >= 5) |»
| (browser.isOmniweb && »
browser.versionMinor >= 3.1) |»
| (browser.isIcab && »
browser.versionMinor >= 1.9) |»
| (browser.isWebtv) |»
| (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.)
[h1]Tactic 1: Quick and Dirty with document.writes[/h1]
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, » intHeight, strClass, strAlt) { if (pngAlpha) { document.write('<div style="height:'+intHeight+'px;» width:'+intWidth+'px;» filter:progid:DXImageTransform.Microsoft.AlphaImageLoader» (src=\''+strPath+'.png\', sizingMethod=\'scale\')" » id="'+strId+'" class="'+strClass+'"></div>'); } else if (pngNormal) { document.write('<img src="'+strPath+'.png" » width="'+intWidth+'"» height="'+intHeight+'" name="'+strId+'" » border="0" class="'+strClass+'" alt="'+strAlt+'" />'); } else { document.write('<img src="'+strPath+'.gif" » width="'+intWidth+'"» height="'+intHeight+'" name="'+strId+'" » border="0" class="'+strClass+'" alt="'+strAlt+'" />'); } }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 <noscript> tag that contains a regular [code]<img>[/code] reference. So the respectable browsers get PNGs normally, IE5.5+/Win gets them with the filter, and all other browsers get regular GIFs, whether they have JavaScript turned on or not. It's a time-tested method, but what if we want more control over our PNGs? [h1]Tactic 2: the Beauty & Majesty of Objects[/h1] When I told the programmer in the office next door that I was writing this article, he took one look at my code, glowered at me, and said, “Fool. Where's the abstraction? You need to use objects.” So now we have a JavaScript object to display PNGs. Here's how we use it:
<html><head>
<script language="javascript"
src="browserdetect_lite.js"
type="text/javascript">
</script>
<script language="javascript"
src="opacity.js"
type="text/javascript"></script>
<script type="text/javascript">
var objMyImg = null;
function init() {
objMyImg = new OpacityObject('myimg','/images/myimage');
objMyImg.setBackground();
}
</script>
<style type="text/css">
#myimg {
background: url('back.png')
repeat; position:absolute;
left: 10px; top: 10px;
width: 200px;
height: 200px;
}
</style>
</head>
<body onload="init()" background="back.jpg">
<div id="myimg"></div>
</body>
</html>
That's it. The cool thing about the OpacityObject is that we just pass it a DIV ID and an image path and we're done. Using the appropriate technique, it applies the image as a background of the DIV, and from there we can do whatever we want with it. Fill it with text, move it across the screen, resize it dynamically, whatever – just like any other DIV.
The object works in any CSS 1-capable browser that can dynamically apply a background image to a DIV with JavaScript. It's completely flexible, and we could even use it in place of the above function.
The trade-off is that it doesn't degrade as nicely. Netscape 4.7/Win/Mac and Opera 5/Mac, for instance, won't display an image at all. And it has another significant problem, which is this:
IE5/Mac only supports alpha transparency when the PNG resides in an [code]<img>[/code] tag, not when it's set as the background property of a DIV. So PNGs displayed with the OpacityObject will appear 100% opaque in IE5/Mac. This problem is especially frustrating because IE5/Mac is the only browser which natively supports PNG and behaves this way. We've notified Microsoft about this apparent bug and hope for it to be fixed in an upcoming release.
But for now, these issues are the trade-off for flexibility. Obviously, choose the right tactic based on the particular needs of your project. Between them both, you can do pretty much anything with PNGs – like, for instance...
[h1]Example 1: Translucent Image on a Photo[/h1]
In this simple example, we see how the same 80% opaque PNG can be displayed on any kind of background: [url=http://oaksanderson.com/opacity/examples/ex1.htm]Translucent Image on a Photo[/url].
[h1]Example 2: Anti-Aliased Translucent Navigation with Rollovers[/h1]
What a beautiful thing it would be, I'm sure you've thought from time to time, to create translucent anti-aliased images that work [i]on any background.[/i] Well, check it out: [url=http://oaksanderson.com/opacity/examples/ex2.htm]Anti-Aliased Translucent Navigation with Rollovers[/url].
Mouse over the images, observe the behavior of the rollovers, and click “change background” to see how the images behave on different backgrounds. Then view the source. There are a few things worth noting here:
[list]
[*]To preload the correct images, we create a variable called [code]strExt[/code], which contains either “.png” or “.gif.” As long as our PNGs and alternate GIFs use the same names except for the file extension, the browser will only preload the images that it's actually going to use.
[/*][*]We create a class called [code]pngLink[/code] and set the cursor property to “pointer.” We pass that class name to the function when we call it, and the function applies the class to the PNG. The result is that the user's pointer turns into a cursor when he rolls over the image links, even though, in IE5.5+/Win, they're really just DIVs. (You might also want to add [code]"display:block"[/code] or [code]"display:inline"[/code] to your PNG class, depending on how you're using the images, to make them display correctly in Netscape 6. (For details, see [url=http://alistapart.com/articles/betterliving/]Better Living Through XHTML[/url].)
[/*][*]We also use a couple of rollover functions specifically for displaying PNGs. It turns out that, while it's possible to dynamically swap out PNGs using the [code]AlphaImageLoader[/code], IE5.5+/Win has a tough time of it; it's damn slow, too slow for effective rollovers. What works better is to apply a background color to the DIV that contains the PNG – the color will shine through the transparent part of the image, and do it fast, too. When we call the function, we send along the name of the image to be displayed and an HTML color – IE5.5+/Win will display the color, and the others will display the image.
[/*][*]Notice how those images even have drop shadows. You could stick any background image or color behind them and they would still look great, even if the PNGs were completely transparent. Is that cool or what? [/*][/list]
[h1]Example 3: Floating Translucent DIV with HTML Text Inside[/h1]
In the first two examples, we used the quick-and-dirty function from tactic one. Now, we want our PNG to interact with other code on the page, so this time we display it with the OpacityObject.
But remember – there are drawbacks to this approach (see above), the most heartbreaking of which is that this example doesn't work perfectly on IE5/Mac. If that causes you pain, then there's always the quick and dirty function. Otherwise, read on.
First we create a DIV, give it an ID, and assign any style properties we want to it – height, width, font family, etc.
Then we pass along the ID of that DIV when we instantiate the OpacityObject. We pass along the image path, too, and now we have a DIV with a translucent background. Cool!
Next we put some HTML text in the DIV and apply another unrelated object method to it (this object has nothing to do with the OpacityObject – it could be any code you have lying around). Now we can move the translucent DIV around the screen. Wheee! [url=http://oaksanderson.com/opacity/examples/ex3.htm]Floating Translucent DIV with HTML Text Inside[/url].
So there's a glimpse of what's possible with the OpacityObject. You hardcore CSS/DOM folks, go nuts.
[h1]Variably Opaque-R-You[/h1]
Download the source code for the object, functions, and examples we covered. All the code relies on our tweaked version of Browser Detect Lite, which is included as well. [url=http://oaksanderson.com/opacity/opacity1.zip]Variable Opacity Source Code[/url].
[h1]One PNG and One PNG Only[/h1]
This is all very exciting, but, as with many achievements that get web developers excited, making PNG work in today's browsers simply shouldn't be this hard. You might consider signing the [url=http://www.petitiononline.com/msiepng/petition.html]petition[/url] to persuade Microsoft to provide full PNG support in Internet Explorer. With any luck, this article will soon be obsolete.
In the meantime, post any ideas for improvements to this code in the discussion forum for this article. The [url=http://www.libpng.org/pub/png/]PNG home site[/url], for instance, talks about a few other obscure browsers that should support alpha transparency, but that haven't been verified yet. If you can verify any of these claims, or have any other valuable input, let us know, and we'll update the code accordingly.
[h1]Resources[/h1]
[list]
[*][url=http://www.libpng.org/pub/png/]PNG Home Site[/url]
[/*][*][url=http://msdn.microsoft.com/workshop/author/filter/reference/filters/alphaimageloader.asp][code]AlphaImageLoader[/code] Filter page on MSDN[/url]
[/*][*][url=http://webfx.eae.net/dhtml/pngbehavior/pngbehavior.html]PNG Behavior at WebFX[/url], an alternate way to make PNGs display in IE. Involves using the runtimeStyle object. The downside with this approach is that it only correctly displays a PNG if it's displayed within an [code]<img>[/code] tag, not if it's a CSS background image[/*][/list]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有