跨浏览器setOpacity()没有浏览器嗅探? [英] Cross-browser setOpacity() without browser sniffing?

查看:63
本文介绍了跨浏览器setOpacity()没有浏览器嗅探?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有人有一个跨浏览器的setOpacity函数,而不是使用浏览器嗅探?我看了雅虎! UI功能,它通过查找window.ActiveXObject来检测

IE。我也看了Scriptaculous

,它使用navigator.userAgent。


谢谢,

彼得

大致雅虎! UI位的相关代码


var el = document.getElementById(''foo'');


if(window.ActiveXObject& & typeof el.style.filter ==''string''){//

如果没有附加

el.style.filter =''alpha(不透明度) =''+ val * 100 +''''';


if(!el.currentStyle ||!el.currentStyle.hasLayout){

el.style.zoom = 1; //当没有布局或不能告诉

}

}否则{

el.style.opacity = val;

el.style ['' - moz-opacity''] = val;

el.style ['' - khtml-opacity''] = val;

}


Scriptaculous代码的一点


Element.setOpacity = function(元素,值){

元素= $(element);

if(value == 1){

Element.setStyle(element,{opacity:

(/ Gecko) /.test(navigator.userAgent)&&

!/ Konqueror | Safari | KHTML / .test(navigator.userAgent))?

0.999999:null}) ;

if(/MSIE/.test(navigator.userAgent))

Element.setStyle(element,{filter:

Element.getStyle (element,''filter'')。replace(/ alpha \([^ \)] * \)/ gi,'''')});

} else {

if(value< 0.00001)value = 0;

Element.setStyle(element,{opacity:value});

if(/ MSIE / .test(navigator.userAgent))

Element.setStyl e(元素,

{filter:

Element.getStyle(element,''filter'')。replace(/ alpha \([^ \)] * \)/ gi,'''')+

''alpha(opacity =''+ value * 100 +'')''}};

}

}

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.ActiveXObject. I also looked at Scriptaculous
and it uses navigator.userAgent.

Thanks,
Peter
Roughly the Yahoo! UI bit of relavent code

var el = document.getElementById(''foo'');

if (window.ActiveXObject && typeof el.style.filter == ''string'') { //
in case not appended
el.style.filter = ''alpha(opacity='' + val * 100 + '')'';

if (!el.currentStyle || !el.currentStyle.hasLayout) {
el.style.zoom = 1; // when no layout or cant tell
}
} else {
el.style.opacity = val;
el.style[''-moz-opacity''] = val;
el.style[''-khtml-opacity''] = val;
}

The bit of Scriptaculous code

Element.setOpacity = function(element, value){
element= $(element);
if (value == 1){
Element.setStyle(element, { opacity:
(/Gecko/.test(navigator.userAgent) &&
!/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ?
0.999999 : null });
if(/MSIE/.test(navigator.userAgent))
Element.setStyle(element, {filter:
Element.getStyle(element,''filter'').replace(/alpha\([^\)]*\)/gi,'''')});
} else {
if(value < 0.00001) value = 0;
Element.setStyle(element, {opacity: value});
if(/MSIE/.test(navigator.userAgent))
Element.setStyle(element,
{ filter:
Element.getStyle(element,''filter'').replace(/alpha\([^\)]*\)/gi,'''') +
''alpha(opacity=''+value*100+'')'' });
}
}

推荐答案

(元素);

if(value = = 1){

Element.setStyle(元素,{不透明度:

(/Gecko/.test(navigator.userAgent)&&

!/ Konqueror | Safari | KHTML / .test(navigator.userAgent))?

0.999999:null});

if(/MSIE/.test(navigator) .userAgent))

Element.setStyle(element,{filter:

Element.getStyle(element,''filter'')。replace(/ alpha \([ ^ \)] * \)/ gi,'''')});

}否则{

if(value< 0.00001)value = 0;

Element.setStyle(element,{opacity:value});

if(/MSIE/.test(navigator.userAgent))

Element.setStyle(element,

{filter:

Element.getStyle(element,''filter'')。replace(/ alpha \ ([^ \)] * \)/ gi,'''')+

''alpha(opacity =''+ value * 100 +'')''});

}

}

(element);
if (value == 1){
Element.setStyle(element, { opacity:
(/Gecko/.test(navigator.userAgent) &&
!/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ?
0.999999 : null });
if(/MSIE/.test(navigator.userAgent))
Element.setStyle(element, {filter:
Element.getStyle(element,''filter'').replace(/alpha\([^\)]*\)/gi,'''')});
} else {
if(value < 0.00001) value = 0;
Element.setStyle(element, {opacity: value});
if(/MSIE/.test(navigator.userAgent))
Element.setStyle(element,
{ filter:
Element.getStyle(element,''filter'').replace(/alpha\([^\)]*\)/gi,'''') +
''alpha(opacity=''+value*100+'')'' });
}
}


pe ********** @ gmail.com 写道:




有没有人有一个跨浏览器的setOpacity函数,而不是
使用浏览器嗅探?我看了雅虎! UI功能,它通过查找window.ActiveXObject来检测

IE。我也看了Scriptaculous

,它使用navigator.userAgent。


谢谢,

彼得


大致是雅虎! UI位的相关代码


var el = document.getElementById(''foo'');


if(window.ActiveXObject& & typeof el.style.filter ==''string''){//

如果没有附加

el.style.filter =''alpha(不透明度) =''+ val * 100 +''''';


if(!el.currentStyle ||!el.currentStyle.hasLayout){

el.style.zoom = 1; //当没有布局或不能告诉

}

}否则{

el.style.opacity = val;

el.style ['' - moz-opacity''] = val;

el.style ['' - khtml-opacity''] = val;

}


Scriptaculous代码的一点


Element.setOpacity = function(元素,值){

元素=
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.ActiveXObject. I also looked at Scriptaculous
and it uses navigator.userAgent.

Thanks,
Peter
Roughly the Yahoo! UI bit of relavent code

var el = document.getElementById(''foo'');

if (window.ActiveXObject && typeof el.style.filter == ''string'') { //
in case not appended
el.style.filter = ''alpha(opacity='' + val * 100 + '')'';

if (!el.currentStyle || !el.currentStyle.hasLayout) {
el.style.zoom = 1; // when no layout or cant tell
}
} else {
el.style.opacity = val;
el.style[''-moz-opacity''] = val;
el.style[''-khtml-opacity''] = val;
}

The bit of Scriptaculous code

Element.setOpacity = function(element, value){
element=


(元素);

if(value == 1){

Element.setStyle(element,{opacity:

(/Gecko/.test(navigator.userAgent)&&

!/ Konqueror | Safari | KHTML / .test(navigator.userAgent))?

0.999999:null});

if(/MSIE/.test(navigator.userAgent))

Element.setStyle(element,{filter:

Element.getStyle(element,''filter'')。replace(/ alpha \([^ \)] * \)/ gi,'''')});

} else {

if(value< 0.00001)value = 0;

Element.setStyle(element,{o pacity:value});

if(/MSIE/.test(navigator.userAgent))

Element.setStyle(element,

{ filter:

Element.getStyle(element,''filter'')。replace(/ alpha \([^ \)] * \)/ gi,'''')+

''alpha(opacity =''+ value * 100 +'')''});

}

}
(element);
if (value == 1){
Element.setStyle(element, { opacity:
(/Gecko/.test(navigator.userAgent) &&
!/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ?
0.999999 : null });
if(/MSIE/.test(navigator.userAgent))
Element.setStyle(element, {filter:
Element.getStyle(element,''filter'').replace(/alpha\([^\)]*\)/gi,'''')});
} else {
if(value < 0.00001) value = 0;
Element.setStyle(element, {opacity: value});
if(/MSIE/.test(navigator.userAgent))
Element.setStyle(element,
{ filter:
Element.getStyle(element,''filter'').replace(/alpha\([^\)]*\)/gi,'''') +
''alpha(opacity=''+value*100+'')'' });
}
}



永远不会玩不透明,但似乎如果你做的话可以使用

类似于:

el.style.filter =''alpha(opacity =''+ val * 100 +'')'';

el.style.opacity = val;

el.style ['' -moz-opacity''] = val;

el.style ['' - khtml-opacity''] = val;

Never played with opacity, but it seems it would work if you did
something like:
el.style.filter = ''alpha(opacity='' + val * 100 + '')'';
el.style.opacity = val;
el.style[''-moz-opacity''] = val;
el.style[''-khtml-opacity''] = val;


这篇关于跨浏览器setOpacity()没有浏览器嗅探?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆