仿照(小米官网首页轮播图)特效js代码

最近跟轮播图杠上了,以前也接触过这个轮播图的写法,但是一般都是用插件,久而久之就忘记了!昨晚有学习了一下自己写这个js,虽然是依靠零散的记忆写出来的,可能自己并未真正理解吧!!!以下是我写的代码:
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>小米官网轮播图仿照</title>
	<style type="text/css">
		.container{width: 1226px;height: 460px;margin:0 auto;position: relative;}
		.container img{position: absolute;;top:0;left: 0;display:none;}
		.left,.right{position: absolute;width: 30px;height: 60px;top:50%;margin-top:-30px;color:#fff; line-height: 60px;text-align: center; font-size: 30px;cursor:pointer;}
		.left:hover,.right:hover{background-color: #777171;}
		.left{left:0;}
		.right{right: 0;}
		.container ul{position: absolute;bottom:10px;right:10px;}
		.container ul li{float: left;list-style: none;width: 10px;height:10px;background-color: #fff;border-radius: 100%;border:1px solid #fff;margin-right:10px; line-height: 10px;}
		.container ul li.active{background-color: #776e6e;}
	</style>
</head>
<body>
	<div class="container">
				<img src="images/lb/1.jpg" alt="">
				<img src="images/lb/2.jpg" alt="">
				<img src="images/lb/3.jpg" alt="">
				<img src="images/lb/4.jpg" alt="">
				<img src="images/lb/5.jpg" alt="">
				<div class="left" id="left"><</div>
				<div class="right" id="right">></div>
				<ul>
					<li class="active"></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
				</ul>
			</div>		
			<script type="text/javascript">
				var lis=document.getElementsByTagName('li');
				var imgs=document.getElementsByTagName('img');
				imgs[0].style.display='block';
				len=lis.length;
				var index=0;
				for (var i =0; i<len; i++) {
					lis[i].tt=i;
					lis[i].οnclick=function(){
						fadeOut(imgs[index],1);
						index=this.tt;
						changeImg(index);
						fadeIn(imgs[index],1);
					}
				}
				function getId(id){return document.getElementById(id);}
				getId('right').οnclick=function(){
					fadeOut(imgs[index],1);
					index++;
					if (index>len-1) {index=0;}
					changeImg(index);
					fadeIn(imgs[index],1);

				}
				getId('left').οnclick=function(){
					fadeOut(imgs[index],1);
					index--;
					if (index<0) {index=0;alert('已经是第一张了!');}
					changeImg(index);
					fadeIn(imgs[index],1);
					
				}


				function changeImg(dd){

					for(var j=0;j<len;j++){
							lis[j].className='';
							imgs[j].style.display='none';

						}
						lis[dd].className='active';
						imgs[dd].style.display='block';

				}

				// 淡入淡出效果
				function fadeIn(obj,speed){
					obj.style.display='block';
					// obj.style.opacity='0';
					var opa=0;
					var timer=null;
					timer=setInterval(function(){

						opa=opa+speed;
						if (opa>100) {opa=100;clearInterval(timer);}
						obj.style.opacity=opa/100;		
					},13);
				}
				function fadeOut(obj,speed){
					// obj.style.display='none';
					// obj.style.opacity='100';
					var opa='100';
					var timer=null;
					timer=setInterval(function(){

						opa=opa-speed;
						if (opa<0) {opa=0;clearInterval(timer);}
						obj.style.opacity=opa/100;		
					},13);
				}
			</script>
</body>
</html>
照几张图片,放上其实是看,是否能运行吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值