JavaScript轮播图

在这里插入图片描述
在这里插入图片描述

轮播图的body构建

<div class="container">
            <div id="LBT" class="wrap">
            	/* 创建放置轮播图片ul*/
                <ul class="list">
                	/* active 当前图片显示*/
                    <li class="item active">0</li>
                    <li class="item">1</li>
                    <li class="item">2</li>
                    <li class="item">3</li>
                    <li class="item">4</li>
                </ul>
                <ul class="pointList">
   /* data-index 自定义标签, 点击这个按钮可以获取应该切换到第几张图片*/
                    <li class="point active " data-index = '0'></li>
                    <li class="point" data-index = '1'></li>
                    <li class="point" data-index = '2'></li>
                    <li class="point" data-index = '3'></li>
                    <li class="point" data-index = '4'></li>
                </ul>
                /* 到下一张或上一张图片*/
                <button type="button" class="bth" id="goPre"><</button>
                <button type="button" class="bth" id="goNext">></button>
            </div>
		</div>

CSS样式

		<style type="text/css">
			/* 清除样式*/
            *{
                margin: 0; padding: 0;
            }
            /* 版心样式*/
            .container{
                width: 80%;
                margin: 0 auto;
                position: relative;
            }
			.wrap{
				height: 400px;
                background-color: crimson;
			}
			.list{
				width: 100%;
				height: 400px;
				list-style: none;
				position: relative;
			}
			.item{
				width: 100%;
				height: 400px;
				font-size: 30px;
				position: absolute;
				color: white;
				opacity: 0;
				transition: 0.8s;
			}
			.item:nth-child(1){
				background-color: red;
			}
			.item:nth-child(2){
				background-color: blue;
			}
			.item:nth-child(3){
				background-color: green;
			}
			.item:nth-child(4){
				background-color:gainsboro;
			}
			.item:nth-child(5){
				background-color: royalblue;
			}
			.bth{
				position: absolute;
				width: 30px;
				height: 100px;
				top: 150px;
				z-index: 20;
			}
			#goPre{
				left:0px;
				opacity: 0.6;
			}
			#goNext{
				right:0px;
				opacity: 0.6;
			}
			.item.active{
				z-index: 10;
				opacity: 1;
			}
			.pointList{
				padding-left: 0px;
				list-style: none;
				position: absolute;
				right: 20px;
				bottom: 20px;
				z-index: 1000;
			}
			.point{
				width: 8px;
				height: 8px;
				background-color: rgba(0, 0, 0, 0.4);
				border-radius: 100%;
				float: left;
				margin-right: 10px;
				border-style: solid;
				border-width: 2px;
				border-color: rgba(255, 255, 255, 0.6);
	
				cursor: pointer;
			}
			.point.active{
				background-color: rgba(255, 255, 255, 1);
			}
		</style>

JavaScript代码`

<script>
			var items = document.getElementsByClassName("item");
			var goPreBtn = document.getElementById("goPre");
			var goNextBtn = document.getElementById("goNext");
			var points = document.getElementsByClassName("point");
		
			var index = 0;
			var clearActive = function(){
				for(var i = 0; i < items.length; i++){
					items[i].className = "item";
				}
				for(var i=0; i<points.length;i++){     
					points[i].className = "point";
				}
			}
			var goIndex = function(){
				clearActive();
				items[index].className = "item active";
				points[index].className = "point active";
			}
		
			var goNexts = function(){
				
				if(index < 4){
					index++;
				}else{  
					index = 0;
				}
		
				goIndex();
			}
		
			var goPres = function(){
				if(index > 0){
					index--;
				}else{
					index = 4;
				}
				goIndex();
			}
			goNextBtn.addEventListener("click",function(){
				goNexts();
			})
		
			goPreBtn.addEventListener("click", function(){
				goPres();
			})
		
			for(var i = 0; i < points.length; i++){
				points[i].addEventListener("click", function(){
					var pointIndex = this.getAttribute("data-index");
					index = pointIndex;
					goIndex();
				})
			}
		</script>

完整代码

<!DOCTYPE html>
<html>
    <head>
        <title>轮播图</title>
        <meta charset="utf8">
		<style type="text/css">
            *{
                margin: 0; padding: 0;
            }
            .container{
                width: 80%;
                margin: 0 auto;
                position: relative;
            }
			.wrap{
				height: 400px;
                background-color: crimson;
			}
			.list{
				width: 100%;
				height: 400px;
				list-style: none;
				position: relative;
			}
			.item{
				width: 100%;
				height: 400px;
				font-size: 30px;
				position: absolute;
				color: white;
				opacity: 0;
				transition: 0.8s;
			}
			.item:nth-child(1){
				background-color: red;
			}
			.item:nth-child(2){
				background-color: blue;
			}
			.item:nth-child(3){
				background-color: green;
			}
			.item:nth-child(4){
				background-color:gainsboro;
			}
			.item:nth-child(5){
				background-color: royalblue;
			}
			.bth{
				position: absolute;
				width: 30px;
				height: 100px;
				top: 150px;
				z-index: 20;
			}
			#goPre{
				left:0px;
				opacity: 0.6;
			}
			#goNext{
				right:0px;
				opacity: 0.6;
			}
			.item.active{
				z-index: 10;
				opacity: 1;
			}
			.pointList{
				padding-left: 0px;
				list-style: none;
				position: absolute;
				right: 20px;
				bottom: 20px;
				z-index: 1000;
			}
			.point{
				width: 8px;
				height: 8px;
				background-color: rgba(0, 0, 0, 0.4);
				border-radius: 100%;
				float: left;
				margin-right: 10px;
				border-style: solid;
				border-width: 2px;
				border-color: rgba(255, 255, 255, 0.6);
	
				cursor: pointer;
			}
			.point.active{
				background-color: rgba(255, 255, 255, 1);
			}
		</style>
	
	</head>

	<body>
        <div class="container">
            <div id="LBT" class="wrap">
                <ul class="list">
                    <li class="item active">0</li>
                    <li class="item">1</li>
                    <li class="item">2</li>
                    <li class="item">3</li>
                    <li class="item">4</li>
                </ul>
                <ul class="pointList">
                    <li class="point active " data-index = '0'></li>
                    <li class="point" data-index = '1'></li>
                    <li class="point" data-index = '2'></li>
                    <li class="point" data-index = '3'></li>
                    <li class="point" data-index = '4'></li>
                </ul>
                <button type="button" class="bth" id="goPre"><</button>
                <button type="button" class="bth" id="goNext">></button>
            </div>
		</div>

		<script>
			var items = document.getElementsByClassName("item");
			var goPreBtn = document.getElementById("goPre");
			var goNextBtn = document.getElementById("goNext");
			var points = document.getElementsByClassName("point");
		
			var index = 0;
			var clearActive = function(){
				for(var i = 0; i < items.length; i++){
					items[i].className = "item";
				}
				for(var i=0; i<points.length;i++){     
					points[i].className = "point";
				}
			}
			var goIndex = function(){
				clearActive();
				items[index].className = "item active";
				points[index].className = "point active";
			}
		
			var goNexts = function(){
				
				if(index < 4){
					index++;
				}else{  
					index = 0;
				}
		
				goIndex();
			}
		
			var goPres = function(){
				if(index > 0){
					index--;
				}else{
					index = 4;
				}
				goIndex();
			}
			goNextBtn.addEventListener("click",function(){
				goNexts();
			})
		
			goPreBtn.addEventListener("click", function(){
				goPres();
			})
		
			for(var i = 0; i < points.length; i++){
				points[i].addEventListener("click", function(){
					var pointIndex = this.getAttribute("data-index");
					index = pointIndex;
					goIndex();
				})
			}
		</script>
		  
	</body>
 
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值