jQuery实现轮播效果

jQuery实现轮播效果

实际效果图

  • 不多说,直接上代码。
<!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>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值