轮播图的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>