在瀏覽器上運行的時候透明度這個功能老是出錯,提示說無法運用currentStyle 是瀏覽器的原因嗎
function getStyle(obj,attr){
????if(obj.currentStyle){
????return obj.currentStyle[attr]; //IE瀏覽器
}
else{
????return getComputedStyle(obj,false)[attr];//火狐瀏覽器
}
}
function getStyle(obj,attr){
????if(obj.currentStyle){
????return obj.currentStyle[attr]; //IE瀏覽器
}
else{
????return getComputedStyle(obj,false)[attr];//火狐瀏覽器
}
}
2016-01-05
舉報
2016-01-05
如果變寬和變高都可以實現(xiàn)的話,有可能是透明度改變的那一部分代碼寫錯了,或者是你用的瀏覽器不兼容吧;
ie,edge,opera應該都是用的currentstyle,其他的用的都是getcomputedstyle
2016-01-06
寬和高都能實現(xiàn),就是透明度有問題呀,麻煩你幫我看看是什么地方出問題了,謝謝
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr]; //IE瀏覽器
}
else{
return getComputedStyle(obj,false)[attr];//火狐瀏覽器
}
}
function startMove(obj,attr,target,fn){//(fn是一個為回調函數(shù))
clearInterval(obj.timer);
obj.timer=setInterval(function(){
//1.取當前的值
var icur = 0;
if(attr == 'opacity'){
/*是否為透明度的判斷*/
icur = Math.round(parseFloat(getStyle(obj.attr))*100);
}
else{
icur=parseInt(getStyle(obj,attr));
}
//2.算速度
var speed =(target-icur)/8;
speed = speed > 0?Math.ceil(speed):Math.floor(speed);
//3.檢測停止
if(icur == target){
clearInterval(obj.timer);
if(fn){
fn()
}
}
else{
if(attr == 'opacity'){
<!--針對IE瀏覽器-->
obj.style.filter = 'alpha:(opacity:'+(icur + speed)+')';
<!--針對火狐或者谷歌瀏覽器-->
obj.style.opacity = (icur +speed)/100;
}
else{
obj.style[attr]= icur + speed +'px';<!--(透明度不加PX的)-->
}
}
},30)
}