javascript + = div的不透明度未添加

时间:2013-06-28 11:23:02

标签: javascript opacity

function popupGrowStage3() {
if (popupContentContainer.style.opacity < 1) {
    popupContentContainer.style.opacity+= 0.01;
    setTimeout(popupGrowStage3, 1000 / 60);
    alert(popupContentContainer.style.opacity);
} else {
    isPopupShowing = 1;
}
}
popupGrowStage3();

以上代码似乎每次都不会向不透明度添加0.01。警报只是说0.01。为什么要这样做?

popupContentContainer.style.width = popup.style.width;
    popupContentContainer.style.height = popup.style.height;
    popupContentContainer.style.backgroundColor = "#111111";
    popupContentContainer.style.opacity = 0;
    popup.appendChild(popupContentContainer);

此代码在此之前

1 个答案:

答案 0 :(得分:1)

popupContentContainer.style.opacity的类型是字符串。因此,当您使用字符串执行+=时,结果将是连接的字符串。

对于Ex:

var a = '2';
a += 2; //results '22'

变化:

popupContentContainer.style.opacity+= 0.01;

到:

popupContentContainer.style.opacity = parseFloat(popupContentContainer.style.opacity) + 0.01;