Transparency with VML
Using VML is yet another option in IE to make a truecolor PNG transparent, and it solves several problems: alpha transparency, performance, and background repeat. Unfortunatelly, it comes with the price of extra non-standard markup (or dependency on JavaScript to generate it if you want your initial markup clean) and more propritary CSS. Here’s an example on how to implement it.
If, for example, you have an empty div, you need to wrap it in one VML :rect
(or :shape
) and one :fill
element, like this:
<v:rect stroked="false"> <v:fill type="tile" src="alphatest.png"> <div> </div> </v:fill> </v:rect>
Somewhere in the markup before that you also need to declare a VML namespace:
<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v" />
And in your stylesheet you need:
v/:rect { behavior:url(#default#VML); width: 100px; height: 100px; display: block; } v/:fill { behavior:url(#default#VML); }
A test page with 100 VML rect
elements loads in 0.094 seconds (almost 10 times faster than using filters) and the memory usage is under 4Mb (10 times less than the filtered page).
As you can see this solution adds more markup and proprietary CSS, but it’s still a solution and doesn’t have the penalties of the AlphaImageLoader.
(Thanks go to this post by Drew Diller and also HTML Remix, who accidentally found this side effect while working on another problem — rounded corners with VML, via snook.ca)
P.S. …and What about Other Filters
AlphaImageLoader is not the only filter that exists. Another popular one is the opacity filter.
For example, for 50% element opacity developers use the properties:
opacity: 0.5
(standard),-moz-opacity: 0.5
(early Mozilla versions, before Firefox 0.9), and- for IE,
filter: alpha(opacity=50)
.
A quick test in IE6 shows that the opacity filter is not nearly as slow as the AlphaImageLoader, but it’s still making the page slower and takes the same amount of memory. This test uses color background, not an image, but even with an image the results are pretty much the same.
[原文]http://www.yuiblog.com/blog/2008/12/08/imageopt-5/
PS VML学习 http://blog.csdn.net/hemingwang0902/archive/2009/06/27/4303369.aspx
test page | time, seconds | memory, MB |
---|---|---|
Test #3 – 100 divs, no opacity | 0.016 | 0.2 |
Test #4 – 100 divs with opacity | 0.093 | 46.7 |