jQuery实现轮播效果
![实际效果图](https://i-blog.csdnimg.cn/blog_migrate/dfab1011f9c7a5a636e047bc83846536.png)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>3D手动轮播图</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
list-style: none;
line-height: 0;
}
body{
background-color: aliceblue;
opacity: 0.8;
}
img{
width: 100%;
height: 100%;
}
div{
box-shadow: 0 0 10px #000 ;
}
#div_main{
position: absolute;left: 25%; top: 100px;
width: 700px;height: 400px;
background-color: rosybrown;
z-index: 10;opacity: 1;
}
#div_left{
position: absolute;left: 10%; top: 150px;
width: 500px;height: 300px;
background-color: rosybrown;
z-index: 9;opacity: 0.8;
}
#div_rignt{
position: absolute;left: 55%; top: 150px;
width: 500px;height: 300px;
background-color: rosybrown;
z-index: 9;opacity: 0.8;
}
#div_leftSon{
position: absolute;left: 6%; top: 225px;
width: 500px;height: 150px;
background-color: rosybrown;
z-index: 8;opacity: 0.6;
}
#div_rigntSon{
position: absolute;left: 59%; top: 225px;
width: 500px;height: 150px;
background-color: rosybrown;
z-index: 8;opacity: 0.6;
}
input[type="button"]{
position: absolute;
padding: 15px;
font-size: 20px;
background-color: white;
opacity: 0.9;
border: none;border-radius: 7px;box-shadow: 0 0 20px whitesmoke;
z-index: 11;
}
#button_left{
left: 20%;top: 280px;
}
#button_right{
left: 78%;top: 280px;
}
</style>
<script src="../../js/jquery-1.12.4.js"></script>
</head>
<body>
<input type="button" value="<" id="button_left"/>
<input type="button" value=">" id="button_right"/>
<div id="div_leftSon">
<img src="http://b.hiphotos.baidu.com/zhidao/pic/item/96dda144ad34598293f69d550cf431adcaef84fc.jpg"/>
</div>
<div id="div_left">
<img src="http://pic1.win4000.com/wallpaper/8/583f83394c3a3.jpg"/>
</div>
<div id="div_main">
<img src="http://pic1.win4000.com/wallpaper/f/5462cc463dc11.jpg"/>
</div>
<div id="div_rignt">
<img src="http://www.79n.cn/uploads/allimg/130202/1-130202213035.jpg"/>
</div>
<div id="div_rigntSon">
<img src="http://pic1.win4000.com/wallpaper/e/580b343bdb11b.jpg"/>
</div>
<script type="text/javascript">
$(function(){
/**
可以实现切换的功能,但是比较“生硬”
且需要将图片路径改为“标准”格式,如:img/1.jpg
*/
// $("#button_left").click(function(){
// //获得所有的img标签
// var imgs = $("img");
// //遍历所有的标签,改变相应图片的路径,实现按钮点击改变图片的效果
// imgs.each(function(i) {
// //截取路径中数字部分
// var src_ = Number($(this).attr("src").substring(7,8));
// //改变数字
// src_ += 1;
// //如果数字为5+1,则图片换为第一张
// if(src_ == 6){
// src_ = 1;
// }
// //设置路径
// $(this).attr("src",'images/'+ src_ +'.jpg');
// });
// });
/**
通过animate()函数,实现缓动效果,对图片切换之间的各个变化的属性进行调整。
*/
//左按钮点击进行图片切换
$("#button_left").click(function(){
//获得所有的img标签
var divs = $("div");
//遍历所有的标签,改变相应图片的路径,实现按钮点击改变图片的效果
divs.each(function(i) {
var str = $(this).attr("id");
//判断每一个div的id,进行不同情况的变化处理
switch(str){
case "div_leftSon":
//避免图片切换的生硬,先进行z-index的改变,再进行其他变化
$(this).animate({
"z-index": 8
},0).animate({
left: "59%",
top: "225px",
width: "500px",
height: "150px",
opacity: "0.6"
//图片变换后,重新设置id,使其符合相应的位置
},500).attr("id","div_rigntSon");
break;
case "div_rigntSon":
$(this).animate({
"z-index": 9
},0).animate({
left: "55%",
top: "150px",
width: "500px",
height: "300px",
opacity: "0.8"
},800).attr("id","div_rignt");
break;
case "div_rignt":
$(this).animate({
"z-index": 10
},0).animate({
left: "25%",
top: "100px",
width: "700px",
height: "400px",
opacity: "1"
},500).attr("id","div_main");
break;
case "div_main":
$(this).animate({
"z-index": 9
},0).animate({
left: "10%",
top: "150px",
width: "500px",
height: "300px",
opacity: "0.8"
}).attr("id","div_left");
break;
case "div_left":
$(this).animate({
"z-index": 8
},0).animate({
left: "6%",
top: "225px",
width: "500px",
height: "150px",
opacity: "0.6"
}).attr("id","div_leftSon");
break;
}
});
});
//点击右侧按钮实现图片的切换
$("#button_right").click(function(){
//获得所有的img标签
var divs = $("div");
//遍历所有的标签,改变相应图片的路径,实现按钮点击改变图片的效果
divs.each(function(i) {
var str = $(this).attr("id");
switch(str){
case "div_rignt":
$(this).animate({
"z-index": 8
},0).animate({
left: "59%",
top: "225px",
width: "500px",
height: "150px",
opacity: "0.6"
}).attr("id","div_rigntSon");
break;
case "div_main":
$(this).animate({
"z-index": 9
},0).animate({
left: "55%",
top: "150px",
width: "500px",
height: "300px",
opacity: "0.8"
}).attr("id","div_rignt");
break;
case "div_left":
$(this).animate({
"z-index": 10,
left: "25%",
top: "100px"
},0).animate({
width: "700px",
height: "400px",
opacity: "1"
}).attr("id","div_main");
break;
case "div_leftSon":
$(this).animate({
"z-index": 9,
},0).animate({
left: "10%",
top: "150px",
width: "500px",
height: "300px",
opacity: "0.8"
},800).attr("id","div_left");
break;
case "div_rigntSon":
$(this).animate({
"z-index": 8,
},0).animate({
left: "6%",
top: "225px",
width: "500px",
height: "150px",
opacity: "0.6"
},500).attr("id","div_leftSon");
break;
}
});
});
});
</script>
</body>
</html>