日期:2014-05-16 浏览次数:20327 次
<HEAD>
<TITLE> 淡入淡出 </TITLE>
<style>
div { text-align:center; font-size:12px; font-weight:bold; font-family:Arial}
div .normal {float:left; width:20px; height:15px; cursor:pointer; filter:alpha(opacity:50); margin-left:1px; background:white; color:#333;}
div .active {float:left; width:20px; height:15px; cursor:pointer; filter:alpha(opacity:50); margin-left:1px; background:red; color:white; }
</style>
</HEAD>
<BODY>
<div style="width:475px; height:60px; border:2px solid gray">
<div id="div1" style="width:475px; height:60px;"></div>
<div id="div2" style="width:475px; height:60px; float:left; margin-top:-60px; opacity:100"></div>
<div id="div_idx" style="height:15px; margin-top: -15px">
<div class="active" onclick="clk(this)">1</div>
<div class="normal" onclick="clk(this)">2</div>
<div class="normal" onclick="clk(this)">3</div>
<div class="normal" onclick="clk(this)">4</div>
<div class="normal" onclick="clk(this)">5</div>
</div>
</div>
//两个重叠的层,div1在下,div2在上。通过改变div2的透明度实现淡入淡出效果。
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
var currIdx=1, timer, timer2;
//图片数组
var arr = ["http://info-database.csdn.net/Upload/2013-03-05/aliyun3-475-60-0305.jpg",
"http://info-database.csdn.net/Upload/2013-03-15/5th-yunjisuan-475-60-0315.jpg",
"http://info-database.csdn.net/Upload/2013-03-13/unity-560-95-0311.jpg",
"http://info-database.csdn.net/Upload/2013-03-21/ibm-platform-760-90-0321.jpg",
"http://info-database.csdn.net/Upload/2013-03-19/go-760-90-0319.jpg"
];
//装入第一张图片
div2.style.background = "url('"+ arr[0] +"') no-repeat";
//每5秒换一张图片
timer = window.setInterval("fun()", 5000);
//换图片
function fun(){
//改背景图片
if (currIdx == arr.length) currIdx = 0;
if ( div2.style.opacity == 0) div2.style.background = "url('"+ arr[currIdx] +"') no-repeat";
else div1.style.back