3

I am fairly new to JavaScript and i need a very simple script to fade an image in and out slowly on a loop. Any help would be much appreciated.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
Piers
  • 31
  • 1
  • 1
  • 2
  • 5
    If you can resist to jQuery's sirens songs here is a DOM way: http://stackoverflow.com/questions/2207586 – Mic Oct 15 '10 at 16:18

2 Answers2

5

The easiest way would be using jQuery:

<img src="..." id="myImage">


<script type="text/javascript">
jQuery(function(){

   // Fade In
   $("#myImage").fadeIn();

   // Fade Out
   $("#myImage").fadeOut();

});
</script>

Documentation: http://api.jquery.com/fadeIn/

You can also change the fade duration by passing a parameter

 // predefined slow (200ms)
 $("#myImage").fadeOut("slow");

 // predefined fast (600ms)
 $("#myImage").fadeOut("fast");

 // 1500 ms
 $("#myImage").fadeOut(1500);

Update creating a loop:

function fadeIn()
{
   $(this).fadeIn( fadeOut );
}

function fadeOut()
{
   $(this).fadeOut( fadeIn );
}

fadeIn.call($("#myImage"));
jantimon
  • 36,840
  • 23
  • 122
  • 185
0

There is a fadein function in jQuery

$('#imgName').fadeIn('slow');
GôTô
  • 7,974
  • 3
  • 32
  • 43