前端js练习大图滚动

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			
			*{
				margin: 0;
				padding: 0;
			}
			div{
				width: 520px;
				height: 350px;
				border: aquamarine 2px solid;
				margin: 20px auto;
				position: relative;
			}
			div img{
				position: absolute;
				left: 0;
				top: 0;
				opacity: 0;
			}
			div img:first-child{
				opacity: 1;
			}
			
			.btn{
				position: absolute;
				bottom: 0;
				width: 100%;
				height: 40px;
				border: none;
				text-align: center;
			}
			.btn p{
				display: inline-block;
				width: 60px;
				height: 40px;
				text-align: center;
				line-height: 40px;
				background-color: gold;
				font-weight: bold;
			}
		</style>
	</head>
	<body>
		<div>
			<img src="img/t1.png"/>
			<img src="img/t2.jpg"/>
			<img src="img/t3.jpg"/>
			<img src="img/t4.jpg"/>
			<img src="img/t5.jpg"/>
			<div class="btn">
			<p>图1</p>
			<p>图2</p>
			<p>图3</p>
			<p>图4</p>
			<p>图5</p>
			</div>
		</div>
	</body>
</html>
<script type="text/javascript">
	var pArray = document.getElementsByTagName("p");
	var imgArray = document.getElementsByTagName("img");
	
	//定义变量 来记录下标
	var index = 0;
	for(var i = 0; i < pArray.length; i++){
		pArray[i].onclick = function(){
			//隐藏当前正在显示的图片!!
			imgArray[index].style.opacity = 0;
			pArray[index].style.backgroundColor = "gold";

			//查找下标i
			for(var j = 0; j < pArray.length; j++){
				if(this == pArray[j]){
					index = j;
					break;
				}
			}
			//按钮he图片下标一致
			imgArray[index].style.opacity = 1;
		    pArray[index].style.backgroundColor = "orange";

		}
	}
	
	
	
	
	
</script>

运行结果:
在这里插入图片描述
点击:
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值