HI there,
I wonder if anybody can give me a hand with this.
I am writing a script which will allow me to swap some images (using also jquery to give the fading effect) but things are not quite working the way they should. I am really new to javascript and to jquery, and even if I am reading tutorial and try to practice my code is still a bit dodgy.
Now, this is what I came up with:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>Photography Home</title>
<link rel="stylesheet" type="text/css" href="containers.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
<!--
var myImages=new Array("test1.jpg", "test2.jpg", "test3.jpg");
var temp;
function swapImages()
{
var randomNumber=Math.floor(Math.random()*myImages.length);
if(temp==randomNumber) //so that the picture inserted is always different
{
return swapImages()
}
temp=randomNumber;
imageOut(randomNumber);
}
function imageOut(number)
{
$("#picture").fadeOut(2000);
imageIn(number);
}
function imageIn(imageGenerated)
{
document.getElementById('picture').src = myImages[imageGenerated];
$("#picture").fadeIn(2000);
setTimeout("swapImages()",2000);
}
//-->
</script>
</head>
<body id="page_body" onload="swapImages()">
<div class="wrapper"> <!-- MAIN CONTAINER -->
<p>
<img src="test1.jpg" id="picture" alt=" ">
</p>
</div>
</body>
</html>
The rest of the code with images and css is on a test site here: http://antobbo.webspace.virginmedia.com/javascript_tests/2011_05_26/test.htm
What I have done in the code is to create 3 functions: swapImages(), imageOut(), imageIn()
and pass a parameter to the functions which I will eventually use to swap the images.
Now, the problem is that the transition from one image to another, as you can see in from the above link, is not that smooth, and I don't quite understand why. I know that the reason will probably be obvious to you but I wonder if you could possibly take a little bit of time to explain it to me too : - )
thanks a lot