日期:2014-05-16  浏览次数:20351 次

[新手]鼠标移到div上触发层透明度改变的效果
初学js,自己写的一个鼠标移到div上触发层透明度改变的效果,但不灵,搞不清哪里出了问题,望高手指点
<style   type= "text/css ">
#dvMain{
    width:   100px;
    height:   24px;
    background:   #0099FF;
    filter:alpha(opacity=100);
}

</style>

<script   type= "text/javascript ">
    function   chgAlpha()
    {
        var   i;
for(i=100;   i> =0;   i--)
{
document.dvMain.style.filter= "alpha(opacity=i) ";
setTimeout(chgAlpha(),10);
}    
    }

</script>
<body>
<div   id= "dvMain "   onmouseover= "chgAlpha(); "> </div>
</body>
</html>


------解决方案--------------------
var alpha = 100;
function chgAlpha()
{
alpha--;

setTimeout(a,10);

}
function a()
{
document.getElementById( 'dvMain ').style.filter= "alpha(opacity= " + alpha + ") ";
if (alpha > 0)
{
alpha--;
setTimeout(a,10);
}
}

//楼主瞧瞧这样可否