/* 轮播图片默认的样式*/
.bg {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
-webkit-transition: opacity 2s linear;
-moz-transition: opacity 2s linear;
-o-transition: opacity 2s linear;
transition: opacity 2s linear;
opacity:0;
filter:alpha(opacity=0);
}
.fadein{
opacity:100;
filter:alpha(opacity=100);
}
CSS:<div id="imgs">
<div id="bg1" class="bg"></div>
<div id="bg2" class="bg"></div>
</div>
.bg {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
-webkit-transition: opacity 2s linear;
-moz-transition: opacity 2s linear;
-o-transition: opacity 2s linear;
transition: opacity 2s linear;
opacity:0;
}
#bg1 {
background: url(http://i1.tietuku.com/7b57a678c8999dbas.jpg) no-repeat;
background-size: cover;
}
#bg2 {
background: url(http://i1.tietuku.com/1182f22573e6051fs.jpg) no-repeat;
background-size: cover;
}
.fadein {
opacity: 100;
filter: alpha(opacity=100);
}
demo中只用了两张图片,如果需要插入更多的图片,可以在id=“imgs”的div中加入一个新的子div ,class加上bg即可,然后在CSS中加入该div的描述,比如HTML中加入<div id="bg3" class="bg"></div>,然后CSS中则加入// 替换class达到淡入淡出的效果
function fadeIn(e) {
e.className = "bg fadein"
};
function fadeOut(e) {
e.className = "bg"
};
//申明图片数组中当前的轮播图片
cur_img = document.getElementById("imgs").children.length - 1;
//图片轮播函数
function turnImgs(imgs) {
var imgs = document.getElementById("imgs").children;
if (cur_img == 0) {
fadeOut(imgs[cur_img]);
cur_img = imgs.length - 1;
fadeIn(imgs[cur_img]);
} else {
fadeOut(imgs[cur_img]);
fadeIn(imgs[cur_img - 1]);
cur_img--;
}
}
//设置轮播间隔
setInterval(turnImgs, 3000);
function setOpacity(ele, opacity) { if (ele.style.opacity != undefined) { ///兼容FF和GG和新版本IE ele.style.opacity = opacity / 100; } else { ///兼容老版本ie ele.style.filter = "alpha(opacity=" + opacity + ")"; } }
function setOpacity(ele, opacity) { if (document.all) { ///兼容ie ele.style.filter = "alpha(opacity=" + opacity + ")"; } ele { ///兼容FF和GG ele.style.opacity = opacity / 100; } }
function fadein(ele, opacity, speed) { if (ele) { var v = ele.style.filter.replace("alpha(opacity=", "").replace(")", "") || ele.style.opacity; v < 1 && (v = v * 100); var count = speed / 1000; var avg = count < 2 ? (opacity / count) : (opacity / count - 1); var timer = null; timer = setInterval(function() { if (v < opacity) { v += avg; setOpacity(ele, v); } else { clearInterval(timer); } }, 500); } }
function fadeout(ele, opacity, speed) { if (ele) { var v = ele.style.filter.replace("alpha(opacity=", "").replace(")", "") || ele.style.opacity || 100; v < 1 && (v = v * 100); var count = speed / 1000; var avg = (100 - opacity) / count; var timer = null; timer = setInterval(function() { if (v - avg > opacity) { v -= avg; setOpacity(ele, v); } else { clearInterval(timer); } }, 500); } }
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript" src="fade.js"></script> <script type="text/javascript"> window.onload = function () { document.getElementById('Button1').onclick = function () { fadeout(document.getElementById('DV'), 0, 6000); } document.getElementById('Button2').onclick = function () { fadein(document.getElementById('DV'), 80, 6000); } } </script> </head> <body> <div id="DV" style="background-color: green; width: 400px; height: 400px;"></div> <input id="Button1" type="button" value="button" /> <input id="Button2" type="button" value="button" /> </body> </html>
即直接在callback处写函数。<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000, function(){
document.getElementById("pp").innerHTML = "效果出现了!";
});
});
});
</script>
</head>
<body>
<p>演示带有不同参数的 fadeIn() 方法。</p>
<p id="pp">Testing</p>
<button>点击这里,使三个矩形淡入</button>
<br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div>
<br>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div>
<br>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>
三、新API<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript">
function myFunc(){
document.getElementById("pp").innerHTML = "效果也出现了!";
};
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn("slow");
$("#div2").fadeIn("fast");
$("#div3").fadeIn(3000, myFunc);
});
});
</script>
</head>
<body>
<p>演示带有不同参数的 fadeIn() 方法。</p>
<p id="pp">Testing</p>
<button>点击这里,使三个矩形淡入</button>
<br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div>
<br>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div>
<br>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛