Jump to content

image fade in/out


barbs75

Recommended Posts

I created one recently that fades whole divs. It's a little bulky (the PHP part anyway), but it works flawlessly and it's extremely smooth. In this form, it takes 1 second to fade out and one more to fade back in.

 

Here goes:

 

<style type='text/css'>

<?php for ($i=0;$i<=100;$i++){ ?>

div.fade<?=$i?>{

z-index:1;

width: 90%;

height: 90%;

background: #D4D0C8;

vertical-align: middle;

filter: alpha(opacity=<?=$i?>);

-moz-opacity: <?=($i/100)?>;

opacity: <?=($i/100)?>;

}

<?php } ?>

</style>

<script type="text/javascript">

var fade = 0;

function fadeOut(){

if (fade>0){

fade--;

setFade();

setTimeout("fadeOut()",1);

}

else{

changeImage(); //You'll have to write this one, but should be easy

fadeIn();

}

}

function fadeIn(){

if (fade<100){

fade++;

setFade();

setTimeout("fadeIn()",1);

}

else{

fade = 100;

setFade();

enableButtons();

}

}

function setFade(){

var clssnm = "fade"+fade;

if (document.getElementById('fadediv')){

document.getElementById('fadediv').className = clssnm;

}

}

</script>

<div id="fadediv" class="fade0">Blah</div>

 

 

If you increase the setTimeout number, it will increase the fade in/out time. If you increment the fade variable more than one, it will be quicker, but not as smooth.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.