使用html+css+js实现轮播图(图片数量可随意改变)

使用html+css+js实现轮播图(图片数量可随意改变)

提示:讲道理,像这种不是自己写的代码会有点难看懂(主要我写的太乱了这个),所以有需要就用吧,那个地方需要修改了在改进。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>轮播</title>
		<style type="text/css">
			button{
				margin-left: 50%;
			}
			*{
				padding: 0px;
				margin: 0px;
				
			}
			.top_blast:after{
				content: " ";
				display: table;
				clear: both;
			}
			
			
			ul a img{
				width: 600px;
			}
			ul{
				
				list-style: none;
				position: absolute;
			}
			ul li{
				float: left;
				margin-right: 10px;
			}
			#kuang{
				width: 600px;
				border: 2px solid black;
				margin: 50px auto;
				position: relative;
			}
			

		</style>
	</head>
	<body>
		<button id="right">右移</button>
		<br  /><br  />
		<div id="kuang" class="top_blast">
			<ul class="top_blast">
				<li class="photo"><a href="#"><img src="img/ONE.gif" /></a></li>
				<li class="photo"><a href="#"><img src="img/TWO.gif" /></a></li>
				<li class="photo"><a href="#"><img src="img/THREE.gif" /></a></li>
				<li class="photo"><a href="#"><img src="img/THREE.gif" /></a></li>
			</ul>
			
		</div>
		<script type="text/javascript">
			var middle_image = document.getElementsByClassName("photo");
			var count = middle_image.length;
			var one_width = parseInt(getComputedStyle(middle_image[1], null)["width"]);
			var middle_len = count*(one_width+10);
			
			var ul_biao = document.getElementsByTagName("ul");
			ul_biao[0].style.width = middle_len + "px";
			var image_height = ul_biao[0].offsetHeight;
			var box = document.getElementById("kuang")
			box.style.height = image_height+"px";
			
		
			var right_button = document.getElementById("right");
			var index = 1;
			
			var speed;
			var timer;
			
			right_button.onclick=function(){
				var num = parseInt(ul_biao[0].offsetLeft);
			if (num%610==0){
				if(index==1){
					var speed = 50;
					var middle = 0
					timer=setInterval(function(){
						var now_pos = parseInt(ul_biao[0].offsetLeft);
						if(middle >= (count-1)*610-50){
							speed = ((count-1)*610)%50;
							clearInterval(timer);
						}
						ul_biao[0].style.left = now_pos - speed + "px";
						now_pos = parseInt(ul_biao[0].offsetLeft);
						middle += speed;
					
					}, 50);
					index = count;
					
				}else{
					var speed = 20;
					var middle = 0;
					timer=setInterval(function(){
						var now_pos = parseInt(ul_biao[0].offsetLeft);
						if(middle >= 600){
							speed = 10;
							clearInterval(timer);
						}
						ul_biao[0].style.left = now_pos + speed + "px";
						middle += speed;
					
					}, 50);
					index -= 1;	
				}
				
			}
			}
		</script>
	</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值