用js实现京东放大缩小轮播图

制作一个简易版的京东放大缩小轮播图
1.布局

<div class="box">
    <div class="child">
        <img src="./image/1.jpg" style="width: 200px; height: 150px; left: 0; bottom: 0;" alt=""/>
        <img src="./image/2.jpg" style="width: 200px; height: 150px; left: 210px; bottom: 0;" alt=""/>
        <img class="centerimg" style="height: 200px;width: 250px;left: 420px; bottom: 0;" src="./image/3.jpg" alt=""/>
        <img src="./image/4.jpg" style="width: 200px; height: 150px;left: 680px; bottom: 0;" alt=""/>
        <img src="./image/5.jpg" style="width: 200px; height: 150px;left: 890px; bottom: 0;" alt=""/>
    </div>
    <button class="btn1"></button>
    <button class="btn2"></button>
</div>

2.书写样式

<style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            margin: 0 auto;
            width: 400px;
            height: 320px;
            border: 1px solid #000;
            overflow: hidden;
        }

        .child {
            position: relative;
            margin-top: 50px;
            left: 50%;
            margin-left: -137%;
            width: 1100px;
            height: 220px;
            font-size: 0;
        }

        .child > img {
            position: absolute;
            transition: width .5s linear, height .5s linear, left .5s linear, bottom .5s linear;
        }

        .child > .centerimg {
            height: 200px;
            width: 250px;
        }

        .btn1 {
            float: left;
        }

        .btn2 {
            float: right;
        }
    </style>

3.js实现轮播

<script>
    /*
     *
     * */
    var leftbtn = document.querySelector(".btn1");
    var rightbtn = document.querySelector(".btn2");
    var imagelist = document.querySelector(".child").children;
    leftbtn.onclick = function (e) {
        if (e.detail == 1) {//控制点击1次
            /*
            * 下面是记录第0张初始的样式
            * */
            var width = imagelist[0].style.width;
            var height = imagelist[0].style.height;
            var left = imagelist[0].style.left;
            var bottom = imagelist[0].style.bottom;
            //先找属性值为
            for (var i = 0; i < imagelist.length; i++) {
                if (i < imagelist.length - 1) {
                    imagelist[i].style.width = imagelist[i + 1].style.width;
                    imagelist[i].style.height = imagelist[i + 1].style.height;
                    imagelist[i].style.left = imagelist[i + 1].style.left;
                    imagelist[i].style.bottom = imagelist[i + 1].style.bottom;
                }
                else {
                    imagelist[i].style.width = width;
                    imagelist[i].style.height = height;
                    imagelist[i].style.left = left;
                    imagelist[i].style.bottom = bottom;
                }
                /*
                * 点击左边   最右边第一张到左边第一张位置    会看到一张飞过去了
                * */
                if (parseInt(imagelist[i].style.left) == 0) {
                    imagelist[i].style.opacity = 0;
                    (function (n) {
                        setTimeout(function () {
                            imagelist[n].style.opacity = 1;
                            n = null;
                        }, 500);
                    })(i);
                }
            }
        }
    }
    rightbtn.onclick = function (e) {
        if (e.detail == 1) {
            var width = imagelist[4].style.width;
            var height = imagelist[4].style.height;
            var left = imagelist[4].style.left;
            var bottom = imagelist[4].style.bottom;
            //先找属性值为
            for (var i = imagelist.length - 1; i >= 0; i--) {
                if (parseInt(imagelist[i].style.left) == 0) {
                    imagelist[i].style.opacity = 0;
                    (function (n) {
                        setTimeout(function () {
                            imagelist[n].style.opacity = 1;
                            n = null;
                        }, 500);
                    })(i);
                }
                if (i > 0) {
                    imagelist[i].style.width = imagelist[i - 1].style.width;
                    imagelist[i].style.height = imagelist[i - 1].style.height;
                    imagelist[i].style.left = imagelist[i - 1].style.left;
                    imagelist[i].style.bottom = imagelist[i - 1].style.bottom;
                }
                else {
                    imagelist[i].style.width = width;
                    imagelist[i].style.height = height;
                    imagelist[i].style.left = left;
                    imagelist[i].style.bottom = bottom;
                }

            }
        }
    }
</script>

4.效果如下
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值