JS学习日记--animation<3>

实现水平轮播效果

主要思想:四张图片的话,就把第一张的一个复制放到最末尾,第四章的一个复制放到最开头,
当切换到第四章的时候就无动画的跳转到开头的第四张的复制上,切换到第一张的时候同理,其他的就没什么了,处理好当前图片的索引值与实际图片数量的关系即可

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>水平轮播</title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			ul{
				list-style: none;
				
			}
			.slider{
				width: 700px;
				height: 400px;
				position: relative;
				overflow: hidden;
			}
			.list{
				position: absolute;
				
			}
			.item{
				width: 700px;
				height: 400px;
				float: left;
			}
			img{
				
				width: 700px;
				height: 400px;
				display: block;
			}
			.prev{
				position: absolute;
				top: 150px;
				width: 70px;
				height: 90px;
				opacity: 0.7;
				
			}
			.next{
				position: absolute;
				right: 0px;
				top: 150px;
				width: 70px;
				height: 90px;
				opacity: 0.7;
				
			}
			.bullet{
				left: 250px;
				width: 30px;
				height: 30px;
				background-color: #FF0000;
				margin-left: 5px;
				top: 350px;
				position: relative;
				float: left;
				cursor: pointer;
			}
			.focus
			{
				background-color: #FFFF00;
			}
		</style>
	<script src="../js/animation.js" type="text/javascript" charset="utf-8"></script>
	
	<script type="text/javascript" >
		
		window.onload=function()
		{
			var currentIndex;
			var lis;
			var liWidth;
			var len;
			var list;
			var bullets;
			var id;
			init();
			function init()
			{
				
				currentIndex=1;
				var li_1=document.getElementsByClassName('item')[0];
				var copy_1=li_1.cloneNode(true);
				var li_last=document.getElementsByClassName('item')[3];
				var copy_last=li_last.cloneNode(true);
				 list=document.getElementsByClassName('list')[0];
				list.appendChild(copy_1);
				list.insertBefore(copy_last,li_1);
				lis=document.getElementsByClassName('item');
				
				liWidth=lis[0].offsetWidth;
				list.style.left=-liWidth+'px';
				 len=lis.length;
				list.style.width=liWidth*len+'px';
				document.getElementsByClassName('prev')[0].onclick=function(){
					sliderPrve();
				}
				document.getElementsByClassName('next')[0].onclick=function(){
					sliderNext();
				}
				 bullets=document.getElementsByClassName('bullet');
				for(var i=0;i<bullets.length;i++)
				{
					bullets[i].index=i;
					bullets[i].onclick=function()
					{
						currentIndex=this.index+1;
						sliderTo(currentIndex);
					}
				}
				
				var slider=document.getElementsByClassName('slider')[0];
				slider.onmouseover=function()//移入停止轮播
				{
					stop();
				}
				slider.onmouseout=function()//移出继续
				{
					auto();
				}
				auto();
				
			}
			
			function sliderNext()
			{
				currentIndex++;
				sliderTo(currentIndex);
			}
			function sliderPrve()
			{
				currentIndex--;
				sliderTo(currentIndex);
			}
			function sliderTo(index)
			{
				 
				if(index===len){
					currentIndex=index=2;
					list.style.left=-liWidth+'px';
				}
				if(index===-1)
				{
					currentIndex=index=len-3;
					list.style.left=-(len-2)*liWidth+"px";
				}
				
				console.log(index);
				if(index===0)
				{
					focusIndex=bullets.length-1;
				}else if(index===len-1){
					focusIndex=0;
				}else{
					focusIndex=index-1;
				}
			
				document.querySelector('.focus').className='bullet';
				bullets[focusIndex].className='bullet focus'
				
				var left=-index*liWidth;
				
				animate(list,{left:left})
				
		
				
			}
			function auto()//自动轮播
			{
				clearInterval(id);
				id=setInterval(function()
				{
					sliderNext();
				},4000)
			}
			
			function stop()//停止自动轮播
			{
				clearInterval(id);
			}
			
			
			
			
		}
		
	</script>
	
	
	
	
	
	
	
	
	</head>
	<body>
		<div class="slider">
			<ul class="list">
				<li class="item "id="1"><img src="../img/img (1).jpg" alt=""></li>
				<li class="item"id="2"><img src="../img/img (2).jpg" alt=""></li>
				<li class="item"id="3"><img src="../img/img (3).jpg" alt=""></li>
				<li class="item"id="4"><img src="../img/img (4).jpg" alt=""></li>
			</ul>
			<button type="button" class="prev"><</button>
			<button type="button" class="next">></button>
			<ul class="pagination">
				<li class="bullet focus">1</li>
				<li class="bullet">2</li>
				<li class="bullet">3</li>
				<li class="bullet">4</li>
			</ul>
		</div>
	</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值