js 实现轮播图

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

</head>

<style>

* {

margin: 0px;

padding: 0px;

}

.bigBox {

margin-left: 20px;

margin-top: 20px;

height: 400px;

width: 800px;

position: relative;

}

.oneUl {

height: 400px;

width: 800px;

list-style: none;

position: relative;

}

.item {

width: 100%;

height: 100%;

font-size: 60px;

color: antiquewhite;

text-align: center;

line-height: 400px;

position: absolute;

}

.item:nth-child(1) {

background-color: rgb(2, 223, 223);

}

.item:nth-child(2) {

background-color: rgb(109, 184, 219);

} .item:nth-child(3) {

background-color: rgb(209, 159, 32);

} .item:nth-child(4) {

background-color: rgb(253, 161, 176);

} .item:nth-child(5) {

background-color: rgb(144, 173, 76);

}

button {

position: absolute;

top: 50%;

margin-top: -30px;

height: 60px;

width: 40px;

z-index: 10;

transition: all 0.2s linear;

border: none;

outline: none;

background-color: rgba(0, 0, 0, 0.1);

font-size: 28px;

color: aliceblue;

}

button:hover {

cursor: pointer;

color: antiquewhite;

background-color: rgba(0, 0, 0, 0.3);

/* font-size: 30px; */

}

.right {

right: 0px;

}

.showLi{

z-index: 5;

}

.twoUl {

list-style: none;

position: absolute;

bottom: 10px;

left: 355px;

/* right: auto; */

z-index: 10;

}

.point {

height: 10px;

width: 10px;

background-color: white;

/* border:2px solid rgba(0, 0, 0, 0.2); */

float: left;

border-radius: 100%;

margin-right: 8px;

/* position: absolute; */

}

.point:hover {

cursor: pointer;

}

.showPoint {

background-color: rgba(92, 89, 89, 0.5)

}

</style>

<body>

<div class="bigBox">

<ul class="oneUl">

<li class="item showLi">0</li>

<li class="item">1</li>

<li class="item">2</li>

<li class="item ">3</li>

<li class="item">4</li>

</ul>

<button class="left"><</button>

<button class="right">></button>

<ul class="twoUl">

<li class="point showPoint" point-index="0"></li>

<li class="point" point-index="1"></li>

<li class="point" point-index="2"></li>

<li class="point" point-index="3"></li>

<li class="point" point-index="4"></li>

</ul>

</div>

 

<script>

var items = document.getElementsByClassName('item');

var goRightBut = document.getElementsByClassName('right')[0];

var goLeftBut = document.getElementsByClassName('left')[0];

var points = document.getElementsByClassName('point');

// console.log(goRightBut);

var index = 0;

var clearClass = function(){

for(var i = 0; i < items.length; i++){

items[i].className = 'item';

points[i].className = 'point';

}

}

var goIndext = function(){

clearClass();

items[index].className = 'item showLi';

points[index].className = 'point showPoint';

}

var goNext = function(){

if(index >= 4){

index = 0;

}

else {

index++

}

goIndext();

}

var goPre = function(){

if(index <= 0){

index = 4;

}

else {

index--;

}

goIndext();

}

goRightBut.addEventListener('click',function(){

goNext();

})

goLeftBut.addEventListener('click',function(){

goPre();

})

for (var i = 0; i < points.length; i++){

points[i].addEventListener('click',function(){

var pointindex = this.getAttribute('point-index');

index = pointindex

goIndext();

})

}

</script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值