搜尋
首頁web前端css教學Cross-Browser Variable Opacity with PNG_經驗交流

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<SPAN class=linewrap>»</SPAN>
(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) && 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 && browser.isMac) |<span class="linewrap">»</span>
| (browser.isOpera && browser.isWin <span class="linewrap">»</span>
	&& browser.versionMajor >= 6) |<span class="linewrap">»</span>
| (browser.isOpera && browser.isUnix <span class="linewrap">»</span>
&& browser.versionMajor >= 6) |<span class="linewrap">»</span>
| (browser.isOpera && browser.isMac <span class="linewrap">»</span>
	&& browser.versionMajor >= 5) |<span class="linewrap">»</span>
| (browser.isOmniweb && <span class="linewrap">»</span>
	browser.versionMinor >= 3.1) |<span class="linewrap">»</span>
| (browser.isIcab && <span class="linewrap">»</span>
	browser.versionMinor >= 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;<SPAN class=linewrap>»</SPAN>
		width:'+intWidth+'px;<SPAN class=linewrap>»</SPAN>
 filter:progid:DXImageTransform.Microsoft.AlphaImageLoader<SPAN class=linewrap>»</SPAN>
 (src=\''+strPath+'.png\', sizingMethod=\'scale\')" class="linewrap">»
	id="'+strId+'" class="'+strClass+'"></div>');
	} else if (pngNormal) {
 document.write('<img  src="/static/imghwm/default1.png" data-src="'+strPath+'.png" class="lazy" 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+'" />');
	} else {
 document.write('<img  src="/static/imghwm/default1.png" data-src="'+strPath+'.gif" class="lazy" 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+'" />');
	}
}

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

It's a time-tested method, but what if we want more control over our PNGs?

Tactic 2: the Beauty & Majesty of Objects

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:

<script language="javascript" 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>






<div id="myimg"></div>


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 <img alt="Cross-Browser Variable Opacity with PNG_經驗交流" > 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...

Example 1: Translucent Image on a Photo

In this simple example, we see how the same 80% opaque PNG can be displayed on any kind of background: Translucent Image on a Photo.

Example 2: Anti-Aliased Translucent Navigation with Rollovers

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 on any background. Well, check it out: Anti-Aliased Translucent Navigation with Rollovers.

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:

  • 正しい画像をプリロードするには、「.png」または「.gif」のいずれかを含む strExt という変数を作成します。 PNG と代替 GIF がファイル拡張子を除いて同じ名前を使用している限り、ブラウザは実際に使用する画像のみをプリロードします。
  • pngLink というクラスを作成し、カーソル プロパティを「ポインター」に設定します。関数を呼び出すときにそのクラス名を関数に渡し、関数はそのクラスを PNG に適用します。その結果、IE5.5+/Win では画像リンクは実際には単なる DIV であるにもかかわらず、ユーザーが画像リンクの上にマウスを移動すると、ポインタがカーソルに変わります。 (画像の使用方法に応じて、PNG クラスに "display:block" または "display:inline" を追加して、画像を Netscape 6 で正しく表示できるようにすることもできます。 (詳細については、XHTML によるより良い生活を参照してください) 。)
  • PNG の表示に特化したいくつかのロールオーバー関数も使用します。 AlphaImageLoader を使用して PNG を動的に交換することは可能ですが、IE5.5+/Win では難しいことがわかりました。とても遅いし、効果的なロールオーバーをするには遅すぎる。より効果的なのは、PNG を含む DIV に背景色を適用することです。色は画像の透明な部分を通して輝きます。これも高速に実行できます。この関数を呼び出すときは、表示する画像の名前と HTML の色を送信します。IE5.5+/Win では色が表示され、その他の場合は画像が表示されます。
  • これらの画像にはドロップ シャドウが付いていることに注目してください。 PNG が完全に透明であっても、任意の背景画像や色を背景に貼り付けることができ、見栄えが良くなります。それはクールですか?

例 3: HTML テキストを内部に含む浮動半透明 DIV

最初の 2 つの例では、戦術 1 の即席関数を使用しました。ここで、PNG がページ上の他のコードと対話できるようにするため、今回は OpacityObject を使用して PNG を表示します。

しかし、覚えておいてください – このアプローチには欠点があり (上記を参照)、最も残念なのは、この例が IE5/Mac では完全には動作しないことです。それが苦痛を引き起こす場合は、手っ取り早く汚い機能が常にあります。それ以外の場合は、読み続けてください。

まず、DIV を作成し、それに ID を与え、高さ、幅、フォント ファミリーなど、必要なスタイル プロパティをそれに割り当てます。

次に、OpacityObject をインスタンス化するときに、その DIV の ID を渡します。画像パスも通過し、背景が半透明の DIV ができました。かっこいい!

次に、いくつかの HTML テキストを DIV に配置し、別の無関係なオブジェクト メソッドをそれに適用します (このオブジェクトは OpacityObject とは何の関係もありません。周囲にあるコードであれば何でも構いません)。これで、半透明の DIV を画面上で移動できるようになりました。うーん! 内部に HTML テキストを含むフローティング半透明 DIV.

これで、OpacityObject で何が可能になるかが垣間見えます。筋金入りの CSS/DOM の人たち、大騒ぎしてください。

可変不透明-R-あなた

取り上げたオブジェクト、関数、例のソース コードをダウンロードします。すべてのコードは、同様に含まれている調整されたバージョンの Browser Detect Lite に依存しています。 可変不透明度ソース コード

1 つの PNG と 1 つの PNG のみ

これはすべて非常にエキサイティングですが、Web 開発者を興奮させる多くの成果と同様に、今日のブラウザーで PNG を動作させることは、それほど難しいことではありません。 Internet Explorer で完全な PNG サポートを提供するよう Microsoft を説得するために、請願 に署名することを検討してみてはいかがでしょうか。運が良ければ、この記事は間もなく廃止されるでしょう。

それまでの間、このコードを改善するためのアイデアがあれば、この記事のディスカッション フォーラムに投稿してください。たとえば、PNG ホーム サイト では、アルファ透明度をサポートしているはずの他のいくつかの不明瞭なブラウザについて説明していますが、まだ検証されていません。これらの主張のいずれかを検証できる場合、またはその他の貴重な意見がある場合は、お知らせください。それに応じてコードを更新します。

リソース

タグ内に表示されている場合にのみ正しく表示されることです。
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
每周平台新聞:Galaxy Store的Web應用程序,Tappable Stories,CSS Subgrid每周平台新聞:Galaxy Store的Web應用程序,Tappable Stories,CSS SubgridApr 14, 2025 am 11:20 AM

在本週的綜述中:Firefox獲得了類似鎖匠的力量,三星的Galaxy Store開始支持Progressive Web Apps,CSS Subgrid正在Firefox發貨

每周平台新聞:Internet Explorer模式,搜索控制台中的速度報告,限制通知提示每周平台新聞:Internet Explorer模式,搜索控制台中的速度報告,限制通知提示Apr 14, 2025 am 11:15 AM

在本週的綜述中:Internet Explorer進入Edge,Google搜索控制台吹捧新的速度報告,而Firefox給出了Facebook&#039; s Notification

CSS自定義屬性的範圍的功率(和樂趣)CSS自定義屬性的範圍的功率(和樂趣)Apr 14, 2025 am 11:11 AM

您可能至少已經對CSS變量有點熟悉了。如果沒有,這是兩秒鐘的概述:它們真的稱為自定義屬性

我們是程序員我們是程序員Apr 14, 2025 am 11:04 AM

構建網站正在編程。編寫HTML和CSS正在編程。我是程序員,如果您在這裡閱讀CSS-Tricks,那麼您很有可能是您

您如何從網站上刪除未使用的CSS?您如何從網站上刪除未使用的CSS?Apr 14, 2025 am 10:59 AM

在這裡,我想預先知道的是:這是一個很難的問題。如果您降落在這裡,因為您希望指向您可以運行的工具

圖片中的圖片網絡API簡介圖片中的圖片網絡API簡介Apr 14, 2025 am 10:57 AM

圖片中的圖片在2016年發行了Macos Sierra,在Safari瀏覽器中首次出現在網絡上。這使用戶可以彈出

使用Gatsby組織和準備圖像以使圖像模糊效果的方法使用Gatsby組織和準備圖像以使圖像模糊效果的方法Apr 14, 2025 am 10:56 AM

蓋茨比(Gatsby)進行了出色的處理和處理圖像。例如,它可以幫助您節省圖像優化的時間,因為您不必手動

哦,嘿,填充百分比基於父元素的寬度哦,嘿,填充百分比基於父元素的寬度Apr 14, 2025 am 10:55 AM

今天,我學到了一些有關基於百分比的(%)填充的知識,我腦海中完全錯了!我一直認為百分比填充是基於

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境