jquery淡入淡出轮播图
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin: 0;padding: 0;}
ul,ol{ list-style: none;}
.wrapper{
width: 670px;
height: 240px;
margin: 100px auto;
overflow: hidden;
position: relative;
}
ul{
position:relative;
}
ul li{
position:absolute;
top:0;
left:0;
}
ol{
position: absolute;
right: 0;
bottom: 10px;
width: 190px;
}
ol li{
float: left;
width: 20px;
height: 20px;
margin: 0 5px;
text-align: center;
border-radius: 50%;
cursor: default;
background-color: #fff;
}
ol li.current{
background-color: pink;
color: #fff;
}
.headle{position: absolute;left:0;z-index:9999;height:60px;width:100%;top:90px;}
.headle a {width:30px;height:30px;background:#000;display:block;color:#fff;text-decoration:none;text-align:center;}
.headle a:nth-child(1){float:left;margin-left:20px;}
.headle a:nth-child(2){float: right;margin-right:20px;}
</style>
</head>
<body>
<div class="wrapper">
<div class="headle"><a href="##" class="lt"><</a><a href="##" class="rt">></a></div>
<ul id="box">
<li style="z-index: 6;"><img src="images/1.jpg" alt=""/></li>
<li style="z-index: 5;"><img src="images/2.jpg" alt=""/></li>
<li style="z-index: 4;"><img src="images/3.jpg" alt=""/></li>
<li style="z-index: 3;"><img src="images/4.jpg" alt=""/></li>
<li style="z-index: 2;"><img src="images/5.jpg" alt=""/></li>
<li style="z-index: 1;"><img src="images/6.jpg" alt=""/></li>
</ul>
<ol style="z-index: 10;" id="uu">
<li class="current">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ol>
</div>
</body>
</html>
<!-- jquery公用文件,可上网找 -->
<script src="jquery.js"></script>
<script type="text/javascript">
//fade为立即执行函数,返回值为函数;
var fade = (function() {
var $box = $('#box');
var $uuBox = $('#uu');
var $lt = $('.lt');
var $rt = $('.rt');
var timer = null;
var index = 0;
return {
// 初始化函数
init() {
this.events();
this.autoPlay();
this.goleft();
},
// 鼠标移入按钮时,样式改变,
showImage() {
// eq(),寻找下标位置,addClass()添加类名,siblings()寻找除了自身以外,有相同匹配条件的元素;removeClass:移除类名;
$uuBox.children("li").eq(index).addClass('current').siblings().removeClass("current");
//找到相应下标下的元素,选中的淡入,其他相匹配的同级元素淡出
$box.children("li").eq(index).fadeIn().siblings().fadeOut();
},
// 设置定时器,自动轮播
autoPlay() {
var _this = this;
// 执行前先关闭定时器
clearInterval(timer)
timer = setInterval(function() {
index++;
if(index == $box.children("li").length) {
index = 0;
}
// 传递一个小标,调用showImage()函数
_this.showImage(index);
},2000)
},
// 添加点击下面按钮时触发的事件
events(){
var _this = this;
$uuBox.children('li').on('click', function() {
// console.log($(this).index());
index = $(this).index();
_this.showImage();
_this.autoPlay();
})
},
goleft(){
var _this = this;
$lt.on('click',function(){
index--;
if(index < 0){
index = 6;
}
_this.showImage(index);
})
}
}
})()
fade.init()
</script>