<jQery-点击按钮图片切换两种实现>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		#app {
			width: 600px;
			height: 600px;
			border: 1px solid #000;
			position: relative;
			margin: 0 auto;
			text-align: center;
		}
		#app img {
			position: absolute;
			width: 500px;
			height: 500px;
			display: none;
			left: 50px;
			top: 50px;
		}
		#app img.choose {
			display: block;
		}
		#but {
			margin: 10px auto 10px;
			text-align: center;
		}
	</style>
</head>
<body>
	<h1>点击按钮进行图片的切换</h1>
	<div id="app">
		<img src="/Users/macbookair/Desktop/picture/0.jpg" alt="">
		<img src="/Users/macbookair/Desktop/picture/1.jpg" alt="">
		<img src="/Users/macbookair/Desktop/picture/2.jpg" alt="">
		<img src="/Users/macbookair/Desktop/picture/3.jpg" alt="">
		<img src="/Users/macbookair/Desktop/picture/4.jpg" alt="">
		<img src="/Users/macbookair/Desktop/picture/5.jpg" alt="">
	</div>
	<div id="but">
		<button>下一张</button>
		<button>上一张</button>
	</div>
<!--
	<script type="text/javascript">
//原生js实现
		var ap1=document.getElementById('app').getElementsByTagName('img');
		var but1=document.getElementById('but').getElementsByTagName('button');
		var i=0;
		ap1[i].className="choose";
		but1[0].onclick=function() {
			if( (i+1)==ap1.length ) {
				alert("最后一张了!");
				return;
			}
			ap1[i].className="";
			ap1[++i].className="choose";
		}
		but1[1].onclick=function() {
			if( i<=0 ) {
				alert("第一张!");
				return;
			}
			ap1[i].className="";
			ap1[--i].className="choose";
		}
	</script>
-->
//jQuery实现
	<script src="../jquery-1.12.3/jquery-1.12.3.min.js">
	</script>
	<script type="text/javascript">
		var i=0;
		var im=$('img');
		im.eq(i).show(500);
		$('button').eq(0).click(function() {
			if ( (i+1)==im.size() ) {
				alert("最后一张了");
				return;
			}
			//jQuery fadeIn() 用于淡入已隐藏的元素
			//jQuery fadeOut() 方法用于淡出可见元素。
			im.eq(i++).fadeOut(500);//隐藏
			im.eq(i).fadeIn(500);
		});
		$('button').eq(1).click(function() {
			if ( i==0 ) {
				alert("最后一张了");
				return;
			}
			im.eq(i--).fadeOut(500);
			im.eq(i).fadeIn(500);
		});
	</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值