轮播图

<!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>

 *{

     padding: 0;

     margin: 0;

     list-style: none;

 }

 div{

     height: 400px;

     width: 500px;

     /* border: 3px solid #999999; */

     margin:0 auto;

     position: relative;

     overflow: hidden;

 }

ul{  width: 100%;

    position: absolute;

    left: 0;

    top: 0;

}

ul li{

    float: left;

}

ol{

    width: 100px;

    height: 20px;

    margin-left:-50px;

    position: absolute;

    bottom: 20px;

    left: 50%;

    right: 50%;

    background: white;

    border-radius: 10px;

    display: flex;

    justify-content: space-around;

    align-items: center;

 

}

ol li{

    width: 20px;

    height: 15px;

    background: darkblue;

    border-radius: 50%;

}

a{

    text-decoration: none;

}

.box>a{

    position: absolute;

    text-align: center;

    line-height: 25px;

    top: 50%;

    font-size: 20px;

    width: 25px;

    height: 25px;

    background: #fff;

    

 

}

a{

    text-decoration: none;

    color: #000;

 

}

.box>a.left{

    left: 0;

}

.box>a.right{

    right: 0;

}

ol li.active{

    background:red;

}

</style>

<body>

    <div class="box">

        <ul>

            <li><img src="images/1.jpg" width="500" height="400"></li>

            <li><img src="images/2.jpg" width="499" height="400"></li>

            <li><img src="images/3.jpg" width="499" height="400"></li>

            

            <li><img src="images/4.jpg" width="499" height="400"></li>

            <li><img src="images/5.jpg" width="499" height="400"></li>

        </ul>

        <ol>

            <!-- <li></li>

            <li></li>

            <li></li> -->

        </ol>

        <a href="javascript:;" class="left">&lt;</a>

        <a href="javascript:;" class="right">&gt;</a>

    </div>

    

</body>

<script>

 var box=document.querySelector(".box")

 ulis=box.querySelectorAll("ul li");

 var ol=box.querySelector("ol");

//  console.log(ol)

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

 var li=document.createElement("li")

         ol.appendChild(li)

 }

 ol.children[0].className="active"

var ul=box.querySelector('ul');

var firstli=ul.children[0].cloneNode(true)

var lastli=ul.children[ul.children.length-1].cloneNode(true);

// console.log(firstli,lastli)

// console.log(ol)

ul.appendChild(firstli);// 将复制出来的第一个放到末尾

ul.insertBefore(lastli,ul.children[0]);// 将复制出来的最后一个放到开头  (新节点,旧节点   )

ul.style.width=ul.children.length*firstli.offsetWidth+"px";// 设置ul的宽度

// console.log(ul)

var flag=true;

var index=1;

 

ul.style.left=-index*firstli.offsetWidth+"px";// 设置ul的left值


 

var index=1;

 

// 获取右按钮

var rightbtn=box.querySelector("a.right")

 

rightbtn.οnclick=function(){

    if( !flag){

        return;

 

    }

    flag=false;

    index++;

    move(ul,{left:-index*firstli.offsetWidth},function(){

        if(index==ul.children.length-1){

            index=1;

            ul.style.left=-index*firstli.offsetWidth+"px";

 

        }

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

            ol.children[i].className="";

 

        }

         ol.children[index-1].className='active';

         flag=true;// 运动结束以后将开关打开

    });

}


 

// 获取左按钮

var leftbtn=box.querySelector("a.left");

leftbtn.οnclick=function(){

    if(!flag){ // 判断开关是否开着

        return; // 如果开关是关着的,就停止往下执行

    }

    flag = false; // 能走到这里,说明开关是开着的,可以运行的,将开关关上

    index--;

    // 在这里让ul调用动画函数运动

    move(ul,{left:-index*firstli.offsetWidth},function(){

        // console.log("移动了");

        if(index==0){

            index=ul.children.length-2;

            ul.style.left = -index*firstli.offsetWidth + "px";

        }

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

            ol.children[i].className = '';

        }

        ol.children[index-1].className = 'active'

            flag = true;

    });

 

}

 

// 自动轮播

var timer;

timer=setInterval(function(){

    if(!flag){ // 判断开关是否开着

        return; // 如果开关是关着的,就停止往下执行

    }

    flag = false; // 能走到这里,说明开关是开着的,可以运行的,将开关关上

    index++;

    // 在这里让ul调用动画函数运动

    move(ul,{left:-index*firstli.offsetWidth},function(){

        // console.log("移动了");

        if(index==ul.children.length-1){

            index=1;

            ul.style.left = -index*firstli.offsetWidth + "px";

        }

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

            ol.children[i].className = '';

        }

        ol.children[index-1].className = 'active'

            flag = true;

    });

    

 

},500)

 

// box鼠标移入清除定时器

box.οnmοuseenter=function(){

    clearInterval(timer);

}


 

// box鼠标移出设置定时器

box.οnmοuseleave=function(){

    timer = setInterval(function(){

    if(!flag){ // 判断开关是否开着

        return; // 如果开关是关着的,就停止往下执行

    }

    flag = false; // 能走到这里,说明开关是开着的,可以运行的,将开关关上

    index++;

    // 在这里让ul调用动画函数运动

    move(ul,{left:-index*firstli.offsetWidth},function(){

        // console.log("移动了");

        if(index==ul.children.length-1){

            index=1;

            ul.style.left = -index*firstli.offsetWidth + "px";

        }

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

            ol.children[i].className = '';

        }

        ol.children[index-1].className = 'active'

            flag = true;

    });

},500);

 

}

 

// 小圆点的点击事件

for(let i=0;i<ol.children.length;i++ ){

    ol.children[i].οnclick=function(){

        if( !flag){

        return;

 

    }

    flag=false;

    index=i+1;

    move(ul,{left:-index*firstli.offsetWidth},function(){

        if(index==ul.children.length-1){

            index=1;

            ul.style.left=-index*firstli.offsetWidth+"px";

 

        }

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

            ol.children[i].className="";

 

        }

         ol.children[index-1].className='active';

         flag=true;// 运动结束以后将开关打开

    });

 

    }

 

}


 

//移动函数   元素 对象 函数  

function move(ele,obj,cb){

    var timerObj = {}

    for(let attr in obj){

        if(attr == "opacity"){

            obj[attr] *= 100;

        }

        timerObj[attr] = setInterval(function(){

            let l = window.getComputedStyle(ele)[attr];

            if(attr == "opacity"){

                l *= 100;

            }

            l = parseInt(l);

            let diff = obj[attr] - l;

            let percent = diff/10; 

            if(percent>=0){

                percent = Math.ceil(percent);

            }else{

                percent = Math.floor(percent);

            }

            l+=percent;

            if(l==obj[attr]){ 

                clearInterval(timerObj[attr]); 

                delete timerObj[attr]

                var k=0;

                for(var i in timerObj){

                    k++;

                }

                if(k==0){

                   cb();

                }

            }else{

                if(attr == "opacity"){

                    ele.style[attr] = l/100;

                }else{

                    ele.style[attr] = l+"px";

                }

            }

        },20);

    }

}


 

</script>

 

 

</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值