Hi,
Does anyone have a a cross-browser setOpacity function that does not
use browser sniffing? I looked at the Yahoo! UI function and it detects
IE by looking for window.ActiveXO bject. I also looked at Scriptaculous
and it uses navigator.userA gent.
Thanks,
Peter
Roughly the Yahoo! UI bit of relavent code
var el = document.getEle mentById('foo') ;
if (window.ActiveX Object && typeof el.style.filter == 'string') { //
in case not appended
el.style.filter = 'alpha(opacity= ' + val * 100 + ')';
if (!el.currentSty le || !el.currentStyl e.hasLayout) {
el.style.zoom = 1; // when no layout or cant tell
}
} else {
el.style.opacit y = val;
el.style['-moz-opacity'] = val;
el.style['-khtml-opacity'] = val;
}
The bit of Scriptaculous code
Element.setOpac ity = function(elemen t, value){
element= $(element);
if (value == 1){
Element.setStyl e(element, { opacity:
(/Gecko/.test(navigator .userAgent) &&
!/Konqueror|Safar i|KHTML/.test(navigator .userAgent)) ?
0.999999 : null });
if(/MSIE/.test(navigator .userAgent))
Element.setStyl e(element, {filter:
Element.getStyl e(element,'filt er').replace(/alpha\([^\)]*\)/gi,'')});
} else {
if(value < 0.00001) value = 0;
Element.setStyl e(element, {opacity: value});
if(/MSIE/.test(navigator .userAgent))
Element.setStyl e(element,
{ filter:
Element.getStyl e(element,'filt er').replace(/alpha\([^\)]*\)/gi,'') +
'alpha(opacity= '+value*100+')' });
}
}