First, to obtain transparency
Ele.filters.alpha
Returns an object of all filters for an element, which can be obtained on the basis of opacity.
But it seems that ele.filters can only store the first filter, and when we put Alpha in the second place, we can't get it, and here's the validation.
#q { font-size:20px; float : Left; Filter:alpha (opacity=50), DropShadow (Color=black, offx=10px, offy=10px, positive=Positive);} Console.dir (Q.currentstyle.filter); Console.dir (q.filters);
The results are as follows:
So, we try to use Style.filter to get string operations!
Second, set the transparency
Setting transparency is a bit of a hassle and we need to address the following three questions:
- User incoming value multiplied by 100
- IE6, 7, element only triggers haslayout to make transparency effective
- IE8, setting the opacity to 100 causes the text to blur and needs to be clear about the filter
Problem solving:
1. The first problem, the solution is relatively simple, the code is as follows
Value= (value>0.999): (value<0.001) 0:value*100
2. The second question, we detect whether the element triggered the haslayout, if not, set his zoom property to 1
3. The third problem, when there are multiple filters, we only clear the transparent part
var rfilters=/[\w:\.] +\ ([^)]+]\)/g; var filter=ele.currentstyle.filter| | ele.style.filter| | "" ; Value=style.filter=filter.replace (rfilters,function(a) { return /alpha/ I.test (a)? "" : A;})
Solve the above problems, we can formally set up! First, if our opacity can be obtained by node.filters, we can set it directly; if not, we can only use the string method.
var alpha=ele.filters.alpha| | Ele.filters[salpha]; // Ele.filters[salpha] to take care of the other two very long ways to set up a transparent filter, it's too lazy to write if (Alpha) { alpha.opacity=value; } Else { El.style.filter= (el.style.filter?el.style.filter+ ",": "") + "alpha (opacity=" +value+ ")"; }
Read and write transparency in IE