原生JavaScript实现轮播图

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		* {
			margin: 0;
			padding: 0;
			text-decoration: none;
		}
		#container {
			width: 590px;
			height: 470px;
			margin: 10px auto;
			overflow: hidden;
			position: relative;
		}
		#list {
			width: 4720px;
			height: 470px;
			position: absolute;
		}
		#list img {
			float: left;
			display: block;
		}
		#container .arrow {
			position: absolute;
			top: 210px;
			width: 35px;
			height: 50px;
			background-color: rgba(0,0,0,0.3);
			color: #cecece;
			text-align: center;
			line-height: 50px;
			font-size: 30px;
			display: none;
		}
		#container .arrow:hover {
			background-color: rgba(0,0,0,0.5);
			color: #fff;
		}
		#container:hover .arrow{
			display: block;
		}
		#prev {
			left: 0;
		}
		#next {
			right: 0;
		}
		#buttons {
			height: 10px;
			/*width: 80px;*/
			position: absolute;
			left: 20px;
			bottom: 20px;
			/*background-color: red;*/
		}
		#buttons span {
			display: block;
			float: left;
			width: 10px;
			height: 10px;
			background-color: #ccc;
			border-radius: 50%;
			margin-right: 3px;
		}
		#buttons .on {
			background-color: #fff;
			border: 2px solid #ccc;
			margin-top: -2px;
		}
	</style>
	<script>
		window.onload = function(){
			var container = document.getElementById('container');
			var list = document.getElementById('list');
			var buttons = document.getElementById('buttons').getElementsByTagName('span');
			var prev = document.getElementById('prev');
			var next = document.getElementById('next');
			var index = 0;
			var timer;

			function showButton(){	//点亮小圆点
				for (var i = 0; i < buttons.length; i++) {	//遍历每个小圆点判断是否被点亮
					if (buttons[i].className == 'on') {
						buttons[i].className = '';
						break;
					}
				}
				buttons[index].className = 'on';
			}

			function animate(offset){
				var newLeft = parseInt(list.style.left) + offset;
				list.style.left = newLeft + 'px';	//parseInt() 字符串转换为数值
				if (newLeft > 0) {	//判断left值,无限循环
					list.style.left = -4130 +'px';
				}
				if (newLeft < -4130) {
					list.style.left = 0 + 'px';
				}
			}
			function play(){	//自动播放
				timer = setInterval(function(){
					next.onclick();
				},3000);
			}
			function stop(){	//停止自动播放
				clearInterval(timer);
			}
			next.onclick = function(){	//右箭头函数
				if (index == 7) {
					index = 0;
				}else{
					index++;
				}
				showButton();
				animate(-590);
			}
			prev.onclick = function(){	//左箭头函数
				if (index == 0) {
					index = 7;
				}else{
					index--;
				}
				showButton();
				animate(590);	
			}
			for (var i = 0; i < buttons.length; i++) { //遍历每个小按钮
				buttons[i].onmouseover = function(){	//按钮绑定鼠标事件
					if (this.className == 'on') {
						return;
					}
					var myIndex = parseInt(this.getAttribute('index'));  //获取当前点击的按钮
					var offset = -590 * (myIndex - index);	//计算left值
					animate(offset);
					index = myIndex;
					showButton();
				}
			}
			container.onmouseover = stop;	//鼠标进入图片区域清除自动
			container.onmouseout = play;	//鼠标离开图片区域自动播放
			play();		//默认自动播放
		}
	</script>
</head>
<body>
	<div id="container">
		<div id="list" style="left: 0px">
			<a href=""><img src="img/1.jpg" alt=""></a>
			<a href=""><img src="img/2.jpg" alt=""></a>
			<a href=""><img src="img/3.jpg" alt=""></a>
			<a href=""><img src="img/4.jpg" alt=""></a>
			<a href=""><img src="img/5.jpg" alt=""></a>
			<a href=""><img src="img/6.jpg" alt=""></a>
			<a href=""><img src="img/7.jpg" alt=""></a>
			<a href=""><img src="img/8.jpg" alt=""></a>
		</div>
		<div id="buttons">
			<span index="0" class="on"></span>
			<span index="1"></span>
			<span index="2"></span>
			<span index="3"></span>
			<span index="4"></span>
			<span index="5"></span>
			<span index="6"></span>
			<span index="7"></span>
		</div>
		<a href="javascript:;" id="prev" class="arrow">&lt;</a>
    	<a href="javascript:;" id="next" class="arrow">&gt;</a>
	</div>
</body>
</html>

效果图效果图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值