0

I want to make my image button fade when I put the cursor over it. I am using a script I found online but it doesn't seem to be working.

<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>
user701510
  • 5,563
  • 17
  • 61
  • 86
  • See my answer below... the way you used setTimeout will eat up alot of process and slow ur animation – Ibu May 28 '11 at 23:41

3 Answers3

1

It looks like you're missing a function called SetOpacity, find this from where you got the script from and add it to your code. This should then work.

Oliver
  • 8,794
  • 2
  • 40
  • 60
1

Here is a very efficient way of doing fades in or out in js without using jquery, a

Fade in and fade out with JavaScript & CSS

Although with jquery you write less code $(element).fade().

Community
  • 1
  • 1
Ibu
  • 42,752
  • 13
  • 76
  • 103
1

Here is the code with an implementation of the SetOpacity method:

<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>
kevinAlbs
  • 1,114
  • 2
  • 11
  • 20