html js轮播图

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./css/style.css">
</head>
<body>
  <div class="boxx">
    <div class="box">
      <div class="box0"><img src="./img/5.jpg" alt=""></div>
      <div class="box1"><img src="./img/1.jpg" alt=""></div>
      <div class="box2"><img src="./img/2.png" alt=""></div>
      <div class="box3"><img src="./img/3.jpg" alt=""></div>
      <div class="box4"><img src="./img/4.jpg" alt=""></div>
      <div class="box5"><img src="./img/5.jpg" alt=""></div>
      <div class="box6"><img src="./img/1.jpg" alt=""></div>
    </div>
    <div class="left">
      <img src="./img/left.png" alt="">
    </div>
    <div class="right">
      <img src="./img/right.png" alt="">
    </div>
    <div class="span">
      <span class="active"></span><span></span><span></span><span></span><span></span>
    </div>
  </div>
</body>
<script src="./js/Untitled-1.js"></script>
<script src="./js/index.js"></script>
</html>

css


*{
  margin: 0;
  padding: 0;
}
body{
  background-color: #000;
}
.boxx{
  width: 500px;
  height: 200px;
  margin: 50px auto;
  border: 1px solid #000;
  background-color: #fff;
  overflow: hidden;
  border-radius: 10px;
  position: relative;
}
.boxx .box{
  width: 3500px;
  height: 100%;
  background-color: #fff;
  position: absolute;
  left: -500px;
  transition: left 0.2s linear;
}
.boxx .box div{
  width: 500px;
  height: 100%;
  float: left;
}
.boxx .box div img{
  width: 500px;
  height: 100%;
}
.left{
  position: absolute;
  top: 43%;
}
.right{
  position: absolute;
  top: 43%;
  right: 0;
}
.left,.right{
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
}
.left:active,.right:active{
  opacity: 0.7;
}
.left img,.right img{
  width: 100%;
  height: 100%;
}
.span{
  position: absolute;
  text-align: center;
  bottom: 0;
  height: 40px;
  line-height: 40px;
  left: 40%;
}
.span span{
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #000;
  margin: 5px;
  cursor: pointer;
}
.span span.active{
  background-color: rgb(56, 214, 56);
}

js

let index = 1;
let boxx_box = document.querySelector('.boxx .box');
let left = document.querySelector('.left');
let right = document.querySelector('.right');
let span = document.querySelectorAll('.span span')
let a;
let boxx = document.querySelector('.boxx');
let set_start = null;
left.onclick = function(){
    boxx_box.style.transition = 'left 0.2s linear';
    left_change();
    img_change();
    index_span();
    index_postion();
}
right.onclick = function(){
    boxx_box.style.transition = 'left 0.2s linear';
    right_change();
    img_change();
    index_span();
    index_postion();
}
add_span_color();
aas();
boxx.onmouseenter = stop;
boxx.onmouseleave = start;


function left_change(){
    index--;
}
function right_change(){
    index++;
}
function img_change(){
    if(index == 6){
        set_time();
    }
    if(index == 0){
        set_time();
    }      
}
function set_time(){
  setTimeout(function (){
    boxx_box.style.transition = 'none';
    index_change();
    index_postion();
  },200)
}
function index_change(){
    if(index == 6){
        index = 1;
    }
    if(index == 0){
        index = 5;
    }
}
function index_postion(){
    boxx_box.style.left = (500 * -index) + 'px';
}
function add_span_color(){
    for(let i=0;i<span.length;i++){
        span[i].onclick = function(){
            clear_span_color();
            span[i].className = 'active';
            index = i + 1;
            index_postion();
        }
    }
}
function clear_span_color(){
    for(let i=0;i<span.length;i++){
        span[i].className = '';
    }
}
function index_span(){
    clear_span_color();
    a = index;
    aa();
    a--;
    span[a].className = 'active';
}
function aa(){
    if(a == 6){
        a = 1;
    }
    if(a == 0){
        a = 5;
    }
}
function start(){
    aas();
}
function stop(){
    clearInterval(set_start);
}
function aas(){
    set_start = setInterval(function(){
        boxx_box.style.transition = 'left 0.2s linear';
        right_change();
        img_change();
        index_span();
        index_postion();
    },2000)
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值