淘宝首页的轮播动态效果

19 篇文章 0 订阅

<!DOCTYPE html>

<html lang="en">

 

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <style>

        * {

            margin: 0;

            padding: 0;

            list-style: none;

            bottom: 0;

        }

        

        #banner {

            overflow: hidden;

            width: 1000px;

            height: 800px;

            background: url("./picture/bg.jpg");

            margin: 0 auto;

            position: relative;

        }

        

        #img {

            width: 200%;

            height: 200%;

        }

        

        #img li {

            width: 100%;

            height: 100%;

            position: absolute;

            left: 250px;

            top: 150px;

        }

        

        .item {

            opacity: 0;

            transition: all .5s;

        }

        

        #dot {

            padding-left: 0px;

            position: absolute;

            right: 415px;

            bottom: 300px;

            z-index: 1000;

        }

        

        .point {

            width: 8px;

            height: 8px;

            background: rgba(0, 0, 0, 0.4);

            border-radius: 100%;

            float: left;

            margin-right: 13px;

            border: 1px solid rgba(0, 0, 0, 0.6);

            cursor: pointer;

        }

        

        #prev {

            left: 250px;

        }

        

        #next {

            left: 736px;

        }

        

        .btn {

            width: 50px;

            height: 100px;

            position: absolute;

            top: 270px;

            z-index: 100;

            opacity: 0.7;

            cursor: pointer;

        }

        

        .item.active {

            z-index: 10;

            opacity: 1;

        }

        

        .point.active {

            background: white;

        }

    </style>

</head>

 

<body>

    <div id="banner">

        <ul id="img">

            <li class="item active"><img src="./picture/img1.jpg"></li>

            <li class="item"><img src="./picture/img2.jpg"></li>

            <li class="item"><img src="./picture/img3.jpg"></li>

            <li class="item"><img src="./picture/img4.jpg"></li>

            <li class="item"><img src="./picture/img5.jpg"></li>

        </ul>

        <ul id="dot">

            <li class="point active" data-index="0"></li>

            <li class="point" data-index='1'></li>

            <li class="point" data-index='2'></li>

            <li class="point" data-index='3'></li>

            <li class="point" data-index='4'></li>

        </ul>

        <button id="prev" class="btn"><img src="./images/ar.png"></button>

        <button id="next" class="btn"><img src="./images/ar2.png"></button>

    </div>

    <script>

        let items = document.getElementsByClassName('item');

        let prev = document.getElementById("prev");

        let next = document.getElementById("next");

        let points = document.getElementsByClassName('point');

 

        let index = 0; //表示第几张图片在展示和第几个点,第index张照片有active这个类名

 

        // 清除classname

        let clearActer = function() {

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

                    items[i].className = 'item';

                    points[i].className = 'point'

 

                }

            }

            // 改变classname实现改变

        let goIndex = function() {

                clearActer();

                items[index].className = 'item active';

                points[index].className = 'point active'

 

            }

            // 下一张按钮

        let goNext = function() {

                if (index < 4) {

                    index++;

                } else {

                    index = 0;

                }

                goIndex();

            }

            // 上一张按钮

        let goPre = function() {

                if (index == 0) {

                    index = 4;

                } else {

                    index--;

                }

                goIndex();

            }

            // next点击句柄

        next.addEventListener("click", function() {

                goNext();

            })

            // prev点击句柄

        prev.addEventListener("click", function() {

                goPre();

            })

            // 点击点改变图片

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

            points[i].addEventListener("click", function() {

                let poitIndex = this.getAttribute('data-index');

                index = poitIndex;

                goIndex();

            })

        }

        // 自动轮播定时器

        window.onload = function() {

            setInterval(goNext, 2000);

        }

    </script>

</body>

 

</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zayyo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值