JavaScript(三十一)滑动图片轮播demo

滑动图片轮播的原理就是整体移动,也就是动态改变图片父容器的marginLeft值,来实现整体向左移动 

效果展示

全部代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .block {
            margin: 0 auto;
            width: 700px;
            height: 400px;
            position: relative;
            overflow: hidden;
        }

        .block > div:nth-child(1) {
            width: 4900px;
            height: 400px;
            margin-left: 0;
        }

        .tranAnimate {
            transition: margin-left 0.5s ease-in-out;
        }

        .block > div > img {
            float: left;
            width: 700px;
            height: 400px;
        }

        .dian {
            position: absolute;
            width: 108px;
            height: 15px;
            bottom: 20px;
            left: 296px;
        }

        .dian > div {
            width: 14px;
            height: 14px;
            border-radius: 50%;
            background-color: white;
            float: left;
            margin: 0 2px;
            border: 2px solid black;
            box-sizing: border-box;
        }

        .btn {
            width: 700px;
            height: 40px;
            position: absolute;
            top: 180px;
            z-index: 10;

        }

        .btn > span:nth-child(1) {
            width: 40px;
            background-color: #c6c6c6;
            color: white;
            display: block;
            float: left;
            text-align: center;
            line-height: 40px;
            margin-left: -40px;
            cursor: pointer;
            opacity: 1;
            transition: all 0.5s linear;
            border-radius: 50%;
        }

        .btn > span:nth-child(2) {
            color: white;
            width: 40px;
            background-color: #c6c6c6;
            display: block;
            float: right;
            text-align: center;
            line-height: 40px;
            margin-right: -40px;
            cursor: pointer;
            opacity: 1;
            transition: all 0.5s linear;
            border-radius: 50%;
        }

        .marleft {
            margin-left: 0 !important;
            opacity: 1 !important;
        }

        .marright {
            margin-right: 0 !important;
            opacity: 1 !important;
        }

    </style>
</head>
<body>
<div class="block">
    <div>
        <img src="./img/1.jpg" alt=""/>
        <img src="./img/2.jpg" alt=""/>
        <img src="./img/3.jpg" alt=""/>
        <img src="./img/4.jpg" alt=""/>
        <img src="./img/5.jpg" alt=""/>
        <img src="./img/6.jpg" alt=""/>
        <img src="./img/1.jpg" alt=""/>
    </div>
    <div class="dian">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
    <div class="btn">
        <span><</span>
        <span>></span>
    </div>
</div>


<script>
    var time;
    var b = document.getElementsByClassName("block")[0];
    var bimg = b.children[0];   //图片的父元素
    var count = 0;
    var dian = document.getElementsByClassName("dian")[0];
    var dianChild = dian.children;

    //获取按钮
    var btn = document.getElementsByClassName("btn")[0];
    var btnChild = btn.children;


    dianChild[0].style.backgroundColor = "black";
    showTime();
    function showTime() {
        time = setInterval(function () {
            Anmiton();
        }, 1500);
    }
function Anmiton(){
        bimg.className = "tranAnimate";
        dianChild[count].style.backgroundColor = "white";
        count++;
        var index = count >= bimg.children.length - 1 ? 0 : count;//判断点的位置
        dianChild[index].style.backgroundColor = "black";
        bimg.style.marginLeft = ((-700) * count) + "px";

        //动画完成状态 0.5s  完成的同时判断要不要返回
        setTimeout(function () {
            if (count >= bimg.children.length - 1) {
                count = 0;
                bimg.className = "";
                bimg.style.marginLeft = "0";
            }
        }, 500);
    }

    //点点事件
    for (var i = 0; i <= dianChild.length - 1; i++) {
        dianChild[i].index = i;
        dianChild[i].onmouseenter = function () {
            dianChild[count].style.backgroundColor = "white";
            this.style.backgroundColor = "black";
            bimg.style.marginLeft = ((-700) * this.index) + "px";
            count = this.index;
        }
    }

    //鼠标进入 离开
    b.onmouseenter = function () {
        for (var i = 0; i < btnChild.length; i++) {
            btnChild [i].className = i ? "marright" : "marleft";  //0表示alse
        }
        clearInterval(time);


    };
    b.onmouseleave = function () {
        for (var i = 0; i < btnChild.length; i++) {
            btnChild [i].className = "";  //0表示alse
        }
        showTime();
    };
    //窗体失焦获焦
    document.addEventListener("visibilitychange", function () {
        if (!document["hidden"]) {
            //激活
            showTime();
        } else {
            clearInterval(time);
        }
    });

   /*  左右按钮*/

        btnChild[0].onclick=function(){
            dianChild[count].style.backgroundColor ="white";
            count--;
            if(count<0){
                bimg.className ="";
                count=5;
                bimg.style.marginLeft = "-4200px";
                setTimeout(function () {
                    bimg.className = "tranAnimate";
                    bimg.style.marginLeft = (-700 * count) + "px";
                }, 1);

            }
            else{
                bimg.className ="tranAnimate";
                bimg.style.marginLeft =(-700*count)+"px";
            }

            dianChild[count].style.backgroundColor ="black";

        };
        btnChild [1].onclick=function(){
            Anmiton();
        }
</script>

</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值