匆忙写的一个透明度变化的轮播效果,欢迎指出错误,感谢!!!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery透明度变化轮播效果</title>
<script type="text/javascript" src="../bootstrap-3.3.7/js/jquery2.0.js"></script>
<style>
* {
font-family: 微软雅黑;
}
.container {
width: 1140px;
padding: 0 15px;
margin: 0 auto;
}
#zz_carousel {
width: 1140px;
height: 500px;
overflow: hidden;
position: relative;
}
#zz_carousel:hover .arrow {
display: block;
}
#zz_carousel .paging {
position: absolute;
bottom: 15px;
left: 50%;
}
#zz_carousel .paging span {
display: inline-block;
width: 15px;
height: 15px;
border-radius: 50%;
background-color: #eee;
margin-left: 15px;
cursor: pointer;
}
#zz_carousel .paging span.active {
background-color: darkslategrey;
}
#zz_carousel .image_reel {
position: absolute;
}
#zz_carousel .image_reel img {
width: 1140px;
height: 500px;
display: none;
}
#zz_carousel .image_reel img:first-child{
display: block;
}
#zz_carousel .next {
position: absolute;
top: 50%;
margin-top: -50px;
right: 0;
}
#zz_carousel .previous {
position: absolute;
top: 50%;
margin-top: -50px;
left: 0;
}
a {
text-decoration: none;
color: #fff;
}
#zz_carousel .arrow {
font-size: 50px;
display: none;
}
#zz_carousel .arrow:hover {
background-color: #f0f3ef;
color: #091b22;
}
</style>
</head>
<body>
<div class="container">
<div class="carousel" id="zz_carousel">
<!--图片容器,移动的元素-->
<div class="image_reel">
<a href="#"><img src="images/1.jpg" alt=""></a>
<a href="#"><img src="images/2.jpg" alt=""></a>
<a href="#"><img src="images/3.jpg" alt=""></a>
<a href="#"><img src="images/4.jpg" alt=""></a>
<a href="#"><img src="images/5.jpg" alt=""></a>
</div>
<!--图片下方的小圆点,js动态添加-->
<div class="paging"></div>
<!--左右控制按钮-->
<div class="change">
<sapn class="previous"><a href="#" class="arrow"><</a></sapn>
<span class="next"><a href="#" class="arrow">></a></span>
</div>
</div>
</div>
<script>
$(function () {
//1. 获取元素
var $zz_carousel = $('#zz_carousel');
var $imgs = $zz_carousel.find('.image_reel img');
var $paging = $zz_carousel.find('.paging');
var $previous =$zz_carousel.find('.previous');
var $next =$zz_carousel.find('.next');
//2. 添加小圆点
var picIdx = 0;
$imgs.each(function (index) {
var $span = $('<span></span>');
$span.idx = index;
$span.appendTo($paging);
$span.click(function () {
$(this).addClass('active').siblings().removeClass('active');
picIdx = $span.idx;
$imgs.eq(picIdx).stop().fadeIn();
$imgs.not($imgs.eq(picIdx)).fadeOut();
});
});
//给paging设置margin-left 让它居中
$paging.children().eq(0).addClass('active');
$paging.css({
marginLeft : -($paging.outerWidth() / 2)
});
//3. 左右按钮点击事件
$previous.click(function () {
if(picIdx == 0){
picIdx = $imgs.length ;
}
picIdx --;
$imgs.eq(picIdx).stop().fadeIn();
$imgs.not($imgs.eq(picIdx)).fadeOut();
$paging.children().eq(picIdx).addClass('active');
$paging.children().not($paging.children().eq(picIdx)).removeClass('active');
});
$next.click(function () {
if(picIdx == $imgs.length - 1){
picIdx = -1;
}
picIdx ++;
$imgs.eq(picIdx).stop().fadeIn();
$imgs.not($imgs.eq(picIdx)).fadeOut();
$paging.children().eq(picIdx).addClass('active');
$paging.children().not($paging.children().eq(picIdx)).removeClass('active');
});
//4. 自动播放和鼠标移入移出事件
var timer = setInterval(function () {
$next.trigger('click');
},3000);
$zz_carousel.hover(function () {
clearInterval(timer);
},function () {
clearInterval(timer);
timer = setInterval(function () {
$next.trigger('click');
},3000);
})
});
</script>
</body>
</html>
来源:CSDN
作者:hgzzzz
链接:https://blog.csdn.net/hgzzzz/article/details/81557225