javascript图片切换

代码:

<!DOCTYPE html>
<html>
<head>
	<title>图片切换</title>
	<meta charset="utp-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<style type="text/css">
		*{margin: 0px;
		  padding: 0px;
		  list-style: none;
		  text-decoration: none;
		  }
		  #flash{
		  	width: 520px;
		  	height: 280px;
		  	margin: 0 auto;
		  	position: relative;
		  	border: 1px solid black;
		  }
		  #flash img{
		  	width: 100%;
		  	height: 100%;
		  	position: absolute;
		  	left: 0px;
		  	top: 0px;
		  	display: none;
		  }
		  #flash ul{
		  	width: 150px;
		  	height: 25px;
		  	border-radius: 20px;
		  	background-color: : rgba(255,255,255,0.5);
		  	position: absolute;

		  	left: 35%;
		  	bottom: 10%;

		  }
		  #flash ul li{
		  	width: 12px;
		  	height: 12px;
		  	margin-top: 5px;
		  	background-color: #fff;
		  	border-radius: 50%;
		  	margin-left: 15px;
		  	float: left;
		  }
		  #flash ul .li_1{
		  	background-color: #f40;
		  }
		  #flash .span-r{
		  	width: 50px;
		  	height: 50px;
		  	border-radius: 50%;
		  	position: absolute;
		  	right: 2%;
		  	top: 45%;
		  	background-color:rgba(255,255,255,0.5);

		  }
		  #flash .span-r span{
		  	width: 100%;
		  	height: 100%;
		  	color: rgba(0,0,0,0.5);
		  	font-size: xx-large;
		  	font-weight: 700;
		  	line-height: 50px;
		  	margin-left: 15px;
		  	cursor: pointer;
		  }
		  #flash .span-l{
		  	width: 50px;
		  	height: 50px;
		  	border-radius: 50%;
		  	position: absolute;
		  	left: 2%;
		  	top:45%;
		  	background-color: rgba(255,255,255,0.5);
		  }
		  #flash .span-l span{
		  	width: 100%;
		  	height: 100%;
		  	color: rgba(0,0,0,0.5);
		  	font-size: xx-large;
		  	font-weight: 700;
		  	line-height: 50px;
		  	margin-left: 15px;
		  	cursor: pointer;
		  }

	</style>
</head>
<div id="flash">
	<img src="shirt_1.jpg" alt="" style="display: block;">
	<img src="shirt_2.jpg" alt="">
	<img src="shirt_3.jpg" alt="">
	<img src="shirt_4.jpg" alt="">
	<img src="time.jpg" alt="">
	<ul>
		<li class="li_1"></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
	</ul>
	<div class="span-r">
		<span>></span>
	</div>
	<div class="span-l">
		<span><</span>
	</div>
</div>
<body>

<script type="text/javascript">
	var div=document.getElementById('flash');
	var img=document.getElementsByTagName('img');
	var ul =div.getElementsByTagName('ul')[0];
	var li=ul.getElementsByTagName('li');
	var div_r=document.getElementsByTagName('div')[1];
	var div_l=document.getElementsByTagName('div')[2];
	var len=img.length;
	var count=0;//显示序号
	function run() {
		count++;
		count =count==5?0:count;
		for(var i=0;i<len;i++){
			img[i].style.display='none';
		}
		img[count].style.display='block';
		for(var i=0;i<li.length;i++){
			li[i].style.backgroundColor="#fff";
		}
		li[count].style.backgroundColor="#f40";
	}
	var timer=setInterval(run,1000);
	div.onmouseover=function(){
		clearInterval(timer);
	}
	div.onmouseleave=function(){
		timer=setInterval(run,1000);
	}
	for(var i=0;i<len;i++){
		li[i].index=i;
		li[i].onmouseenter=function(){
			for(var i=0;i<len;i++){
				li[i].style.background="#fff";
				img[i].style.display='none';
			}
			this.style.background='#f40';
			img[this.index].style.display='block';
		}
	}
	div_r.onclick=function(){
		run();
	}
	function reverse(){
		count--;
		count=count==-1?4:count;
		for(var i=0;i<len;i++){
			img[i].style.display='none';
		}
		img[count].style.display='block';
		for(var i=0;i<li.length;i++){
			li[i].style.backgroundColor="#fff"
		}
		li[count].style.backgroundColor="#f40";
	}
	div_l.onclick=function(){
		reverse();
	}
	

</script>
</body>
</html>

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值