使用javascript onmouseover制作按钮淡出

时间:2011-05-28 23:33:48

标签: javascript html forms button fading

当我把光标放在它上面时,我想让我的图像按钮褪色。我使用的是我在网上找到的脚本,但它似乎没有用。

<html>
<head>
<script type="text/javascript">


function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
    var steps = Math.ceil(fps * (time / 1000));
    var delta = (toOpacity - fromOpacity) / steps;

    FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
    SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps)
        setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
}

</script>
</head>


<body>

<form action="opacity.php" method="post">

<input type="image" name="blue" id="ImgAkxl2" value="blue" src="streetfighter.jpg"
onmouseover="UpdateOpacity2()"


/>
</form>

<script language="JavaScript" type="text/javascript">



            function UpdateOpacity2()
            {

                FadeOpacity("ImgAkxl2", 100, 50, 2000, 10);

            }

</script>



</body>
</html>

3 个答案:

答案 0 :(得分:1)

看起来您缺少一个名为SetOpacity的函数,从您从中获取脚本的位置找到它并将其添加到您的代码中。这应该有效。

答案 1 :(得分:1)

这是一种在不使用jquery的情况下在js中进行淡入或淡出的非常有效的方法,

Fade in and fade out with JavaScript & CSS

尽管使用jquery,您可以编写更少的代码$(element).fade()

答案 2 :(得分:1)

以下是具有SetOpacity方法实现的代码:

<html>
<head>
<script type="text/javascript">


function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
var steps = Math.ceil(fps * (time / 1000));
var delta = (toOpacity - fromOpacity) / steps;

FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
{
SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));

if (stepNum < steps)
    setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);
}


function SetOpacity(element, op)
{
element.style.opacity = op/100;
element.style.filter = 'alpha(opacity='+ op+")";
}

</script>
</head>


<body>

<form action="opacity.php" method="post">

<input type="image" name="blue" id="ImgAkxl2" value="blue" src="us.jpg"
onmouseover="UpdateOpacity2()"/>
</form>

<script language="JavaScript" type="text/javascript">
        function UpdateOpacity2()
        {
            FadeOpacity("ImgAkxl2", 100, 50, 2000, 10);

        }
</script>



</body>
</html>