HTML手动轮播图

HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>轮播图</title>
<!-- <script type="text/javascript" src="demo.js"></script> -->
<link rel="stylesheet" href="css/lunbotu.css">
</head>

<body>
<div id="container">
	<ul class="parent" style="left: 0;">
		<li><img src="img/img1.png"></li>
		<li><img src="img/img2.png"></li>
		<li><img src="img/img3.png"></li>
		<li><img src="img/img4.png"></li>
		<li><img src="img/img5.png"></li>
		<li><img src="img/img7.png"></li><!-- 改改改改改改改改改改改改改改改改改改 -->
	</ul>

	<div class="btnLeft">&lt;</div>
	<div class="btnRight">&gt;</div>
	<div class="modal">
		<div class="title">
			<h2>轮播图</h2>
		</div>
		<div class="dots">
			<ul class="clearfix">
				<li class="on"></li>
				<li class="off"></li>
				<li class="off"></li>
				<li class="off"></li>
				<li class="off"></li>
				<li class="off"></li><!-- 改改改改改改改改改改改改改改改改改改 -->
			</ul>
		</div>
	</div>
</div>
<script src="js/lunbotu.js"></script>

</body>
</html>

JS


	
var imgShow = document.getElementsByClassName('parent')[0],
	dotList = document.querySelectorAll('.dots >.clearfix > li');
var btnLeft = document.getElementsByClassName('btnLeft')[0],
    btnRight = document.getElementsByClassName('btnRight')[0];
var dotLen = dotList.length,
	index = 0; //轮播层的图片索引,0表示第一张

//圆点显示
function showRadius() {
	for(var i = 0; i < dotLen; i++) {
		if(dotList[i].className === "on"){
			dotList[i].className = "off";
		}
	}
	dotList[index].className = "on";
}

//向左移动
btnLeft.onclick = function() {
	index--;
    if(index < 0){  /*第1张向左时,变为第5张*/
        index = 5;/*改改改改改改改改改改改改改改改改改改*/
    }
    showRadius();
	var left;
	var imgLeft = imgShow.style.left;
	if(imgLeft === "0px") { /*当是第1张时,每张图片左移,移4张图,位置为-(4*500)*/
		left = -2500;/*改改改改改改改改改改改改改改改改改改*/
	}
	else{
		left = parseInt(imgLeft) + 500; /*由于left为负数,每左移一张加500*/
	}
	imgShow.style.left = left + "px";
}

//向右移动
btnRight.onclick = function() {
	index++;
    if(index > 5){  /*第5张向右时,变为第1张改改改改改改改改改改改改改改改改改改*/
        index = 0;
    }
    showRadius();
	var right;
	var imgLeft = imgShow.style.left;
	if(imgLeft === "-2500px") { /*当是第5张时,第1张的位置为0改改改改改改改改改改改改改改改改改改*/
		right = 0;
	}
	else{
		right = parseInt(imgLeft) - 500; /*由于left为负数,每右移一张减500*/
	}
	imgShow.style.left = right + "px";
}

// 自动轮播
/*var timer;
function autoPlay() {
	timer = setInterval(function() {
		var right;
		var imgLeft = imgShow.style.left;
		if(imgLeft === "-2000px") {
			right = 0;
		}
		else{
			right = parseInt(imgLeft) - 500;
		}
		imgShow.style.left = right + "px";
	} ,1000)
}
autoPlay();*/

for(var i = 0; i < dotLen; i++) {
    /*利用闭包传递索引*/
    (function(i) {
        dotList[i].onclick = function() {
	        var dis = index - i; //当前位置和点击的距离
	        imgShow.style.left = (parseInt(imgShow.style.left) + dis * 500) + "px";
	        index = i; //显示当前位置的圆点
	        showRadius();
    	}
    })(i);
}


CSS

* {
	margin: 0;
	padding: 0;
}

ul {
	list-style: none;
}

a {
	text-decoration: none;
}

#container {
	position: relative;
	width: 500px;
	height: 260px;
	margin: 20px auto;
	overflow: hidden;
	/*溢出隐藏:只显示一张图片*/
}

#container .parent {
	position: absolute;
	width: 3000px;
	/*整个图片层长度:500*5=2500*/
	height: 260px;
}

#container .parent li {
	float: left;
	width: 500px;
	height: 100%;
}

#container .parent li img {
	width: 100%;
	height: 100%;
}

#container .btnLeft,
#container .btnRight {
	width: 30px;
	height: 30px;
	background-color: #9E9E9E;
	border-radius: 20%;
	opacity: 80%;
	position: absolute;
	/*包含块为图片显示层container*/
	top: 0;
	bottom: 0;
	margin: auto;
	font-size: 20px;
	color: #f40;
	text-align: center;
	line-height: 30px;
}

#container .btnLeft {
	left: 10px;
}

#container .btnRight {
	right: 10px;
}

#container .btnLeft:hover,
#container .btnRight:hover {
	opacity: 90%;
	cursor: pointer;
}

/*蒙层*/
#container .modal {
	width: 100%;
	height: 40px;
	background: rgba(0, 0, 0, .3);
	position: absolute;
	left: 0;
	bottom: 0;
	line-height: 40px;
	padding: 0 40px;
	box-sizing: border-box;
}

#container .modal .title {
	float: left;
	color: #fff;
	font-size: 12px;
}

#container .modal .dots {
	float: right;
	position: absolute;
	bottom: 10px;
	left: 340px;
}

#container .modal .dots li {
	width: 15px;
	height: 15px;
	border-radius: 50%;
	float: left;
	/*可以使用行块盒*/
	/*display: inline-block;*/
	margin: 0 5px;
	cursor: pointer;
}

.clearfix::after {
	content: "";
	display: block;
	clear: both;
}

.on {
	background-color: red;
}

.off {
	background-color: gray;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值