轮播图

 这次用的对象写的,总体加深了对象的理解

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图(面向对象)</title>
    <link rel="stylesheet" href="css.css">
</head>
<body>
    <div id="box">
        <div class="list">
            <ul class="pic-li">
                <li class="current"><img src="./images/4.jpg" alt="" ></li>
                <li><img src="./images/5.jpg" alt=""></li>
                <li><img src="./images/7.jpg" alt="" ></li>
                <li><img src="./images/11.jpg" alt="" ></li>
                <li><img src="./images/13.jpg" alt="" ></li>
            </ul>
            <div class="btnLeft">
                <span></span>
            </div>
            <div class="btnRight">
                <span></span>
            </div>
        </div>
    </div>
</body>
</html>
<script>
    var autoPlayer = {
        index: 0,
        init: function(){
            this.initData(); 
            this.doMove();
            this.clickCircle();
            this.sideBtn();
            this.sideMove(this.btnLeft);
            this.sideMove(this.btnRight);
        },
        initData: function(){
           this.oImg = document.querySelectorAll("li");
           this.oUl = document.querySelectorAll("ul");
           this.oList = document.querySelectorAll(".list");
           this.box = document.querySelectorAll("#box")[0];
           this.creatCilcleBtn();
           this.oCount = document.querySelectorAll('.count')[0];
           this.oCountLi = document.querySelectorAll(".circle");
           this.btnLeft = document.querySelectorAll(".btnLeft")[0];
           this.btnRight = document.querySelectorAll(".btnRight")[0];
        },
        changePic:function() {
            for(var i = 0; i < this.oImg.length; i++) {
                this.oImg[i].className = '';
            }
            this.oImg[this.index].className = 'current';
        },
        goNext: function() {
            this.index = (this.index + 1) % 5;
            this.changeCircle();
            this.changePic();
        },
        doMove: function() {
            
            var oThis = this;
            //在没有开始定时器之前改变圆圈颜色的应该是第一个
            oThis.oCountLi[0].className = "'circle circleMove";  
            
            oThis.timer = setInterval(function(){
                oThis.goNext();
                // console.log(oThis.index);
            for(var i = 0; i < oThis.oImg.length; i++) {
                oThis.oCountLi[i].className = "circle";
             }
                oThis.oCountLi[oThis.index].className = 'circle circleMove';
              
            },5000)
            
        },
        creatCilcleBtn: function(){
            //获取创建的circle
           this.oCircleList = document.createElement("ul");
           this.oFrag = document.createDocumentFragment();
           this.oCircleList.className = 'count';
           for(var i = 0; i < this.oImg.length; i ++) {
               var newLi = document.createElement("li");
               newLi.className = 'circle';
               newLi.setAttribute("cirID",i + '');
               newLi.innerHTML = i + 1;
               this.oFrag.appendChild(newLi);
           }
           this.oCircleList.appendChild(this.oFrag);
           this.oList[0].appendChild(this.oCircleList);
        },
        clickCircle: function() {
            var oThis = this;
            for(var i = 0; i < oThis.oCountLi.length; i ++) {
                (function(j) {
                    oThis.oCountLi[j].onmousedown = function() {
                      
                        var cirNum = oThis.oCountLi[j].getAttribute('cirID');
                        oThis.index = Number(cirNum);
                        // console.log(oThis.index);
                        oThis.changePic();
                        oThis.changeCircle();
                    }
                }(i))
            }
        },
        changeCircle: function(){
            for(var i = 0; i < this.oImg.length; i++) {
                this.oCountLi[i].className = "circle";
            }
            this.oCountLi[this.index].className = 'circle circleMove';
        },
        sideBtn: function() {
            var oThis = this;
            oThis.box.onmouseover = function() {
                oThis.btnLeft.style.display = 'flex';
                oThis.btnRight.style.display = 'flex';
                oThis.box.onmouseout = function() {
                    oThis.btnLeft.style.display = 'none';
                    oThis.btnRight.style.display = 'none';
                };
            };
            
        },
        sideMove: function(e) {
            var oThis = this;
            e.onmousedown = function() {
                if(e == oThis.btnLeft) {
                   oThis.index = Math.abs((oThis.index + 4) % 5);
                   oThis.changeCircle();
                   oThis.changePic();
                }
                if(e == oThis.btnRight) {
                    oThis.index = Math.abs((oThis.index + 1) % 5);
                    oThis.changeCircle();
                    oThis.changePic();
                }

            }
        }
        
    }
    window.onload=autoPlayer.init();
</script>

css

* {
    padding: 0;
    margin: 0;
}
body {
    background-color: #000;
}
#box {
    width: 600px;
    height: 400px;
    margin: 100px auto;
    cursor: pointer;
    position: relative;
    border: 10px solid #fff;
    border-radius: 10px;
}
#box .list{
    width: 600px;
    height: 400px;
    overflow: hidden;
    position: relative;
}
ul {
    list-style: none;
    
}
#box .list .pic-li li {
    width: 600px;
    height: 400px;
    position: absolute;
    top: 0;
    left: 0;
    
}
img {
    width: 600px;
    height: 400px;
}
#box .current {
    z-index: 1;
}
#box .count{
    display: grid;
    width: 140px;
    height: 30px;
    position: absolute;
    bottom: 10px;
    left: 230px;
    grid-template-columns: 20px 20px 20px 20px 20px ;
    column-gap: 5px;
    border: 1px solid #f8f8f8;
    z-index: 100;
    justify-content: center;
    align-content: center;
    border-radius: 10px;
}
#box .count li{
    display: grid;
    height: 20px;
    background-color: aqua;
    justify-content: center;
    align-content: center;
    border-radius: 50%;
}
#box .count .circleMove {
    background-color: springgreen;
}

#box .btnLeft , #box .btnRight{ 
    position: absolute;
    width: 30px;
    height: 40px;
    background-color: rgba(255, 255, 255, 0.5);
    z-index: 100;
    top: 160px;
    display: flex;
    justify-content: center;
    align-items: center;
    display: none;

}

#box .btnLeft {
    left: 0;
    border-radius: 0 20px 20px  0 ;
}
#box .btnRight {
    right: 0;
    border-radius:20px 0   0 20px ;
}
#box .btnLeft span , #box .btnRight span {
    width: 15px;
    height:15px;
    border: 1px solid #f8f8f8;
    z-index: 1000;
}
#box .btnLeft span {
     transform: rotate(45deg) ;
    border-style:  none none solid solid;
}
#box .btnRight span {
    transform:  rotate(45deg);
    border-style: solid solid none none;
}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值