轮播图---2淡入淡出

结构

< >
样式 * { margin: 0; padding: 0; list-style: none; }

.slider {
height: 340px;
width: 790px;
margin: 100px auto;
position: relative;
}

.slider li {
position: absolute;
display: none;
}
.slider li:first-child {
display: block;
}

.arrow {
display: none;
}

.slider:hover .arrow {
display: block;
}

.arrow-left,
.arrow-right {
font-family: “SimSun”, “宋体”;
width: 30px;
height: 60px;
background-color: rgba(0, 0, 0, 0.1);
position: absolute;
top: 50%;
margin-top: -30px;
cursor: pointer;
text-align: center;
line-height: 60px;
color: #fff;
font-weight: 700;
font-size: 30px;
}

.arrow-left:hover,
.arrow-right:hover {
background-color: rgba(0, 0, 0, .5);
}

.arrow-left {
left: 0;
}

.arrow-right {
right: 0;
}
.control {
position: absolute;
bottom: 20px;
left: 50%;
margin-left: -85px;
}
.control a {
float: left;
width: 13px;
height: 13px;
background-color: #fff;
border-radius: 8px;
border: 1px solid #ccc;

}

.control a + a {
margin-left: 10px;
}
.control a.active {
background-color: red
};
脚本
// 1、添加索引
var index = 0
// 2、右侧按钮注册点击事件
$(’.arrow-right’).click(function(){
// 2.1 index++
index++;
// 2.2 判断索引位置
if(index > $(’.slider li’).length-1){
index = 0
};
console.log(index)
// 2.3 点击按钮实现图片轮播
$(’.slider li’).eq(index).fadeIn(600).siblings().fadeOut(600);
// 2.4 小点跟着轮播图动
$(’.control a’).eq(index).addClass(‘active’).siblings().removeClass(‘active’);
})
// 3、左侧按钮注册点击事件
$(’.arrow-left’).click(function(){
// 3.1 index–
index–;
// 3.2 判断索引位置
if(index < 0){
index = $(’.slider li’).length-1
};
console.log(index)
// 3.3 点击按钮实现图片轮播
$(’.slider li’).eq(index).fadeIn(600).siblings().fadeOut(600);
// 3.4 小点跟着轮播图动
$(’.control a’).eq(index).addClass(‘active’).siblings().removeClass(‘active’);
})
// 4、实现图片自动轮播
// 4.1封装自动轮播函数
var num;
function autoPlay(){
num = setInterval(function(){
$(’.arrow-right’).click()
},3000)
};
autoPlay();
// 4.2鼠标移入停止自动轮播
$(’.slider li’).mouseover(function(){
clearInterval(num)
})
// 4.3移除继续轮播
$(’.slider li’).mouseout(function(){
autoPlay();
})

扫描二维码关注公众号,回复: 3470373 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_43019637/article/details/82947873