<html>
<head>
<style type="text/css">
div#bg1 {
height: 159px;
width: 800px;
margin-left: auto;
margin-right: auto;
background-image: url('images/bg1.jpg');
background-repeat: no-repeat;
background-position:center center;
position: relative;
z-index: 3;
}
div#bg2 {
height: 159px;
width: 800px;
margin-left: auto;
margin-right: auto;
background-image: url('images/bg2.jpg');
background-repeat: no-repeat;
background-position:center center;
position: relative;
z-index: 2;
margin-top: -159px;
}
</style>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function Animate_2()
{
$("div#bg1").animate({opacity: 100}, 2000);
$("div#bg2").animate({opacity: 0 }, 2000);
setTimeout(Animate_1, 5000);
}
function Animate_1()
{
$("div#bg1").animate({opacity: 0 }, 2000);
$("div#bg2").animate({opacity: 100}, 2000);
setTimeout(Animate_2, 5000);
}
$(function()
{
/* Start cycle */
setTimeout(Animate_1, 5000);
});
</script>
</head>
<body>
<div id="bg1"></div>
<div id="bg2"></div>
</body>
</html>
Animate_1() works fine, but Animate_2() will just display the bg2.jpg without animating the opacity.. This is the same in IE and Firefox..
Why is this >??
Your real problem is that opacity is a scale of 0 to 1, not 0 to 100. But here are some slight improvements:
You could simplify the code a lot too as you’ve just got one image on top of another.
I’ve messed with your timing values to show it faster.
Or fix it. You need
window.before setTimeout. Simple fix.