JavaScript案例(五):轮播图;html+css+js实现轮播图功能以及自动切换图片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>轮播图</title>

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

        ul, ol, li {
            list-style: none;
        }

        img {
            width: 100%;
            height: 100%;
            display: block;
        }
        .banner {
            width: 100%;
            height: 400px;
            position: relative;
            margin: 10px 0;
        }
        .banner > ul {
            width: 100%;
            height: 100%;

            position: relative;
        }
        .banner > ul > li {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;

            opacity: 0;
        }
        .banner > ul > li.active{
            opacity: 1;
        }

        .banner > ol {
            width: 160px;
            height: 15px;
            position: absolute;
            left: 30px;
            bottom: 30px;
            background-color: rgba(50, 50, 50, 0.5);

            display: flex;
            justify-content: space-around;
            align-items: center;
            border-radius: 15px;
        }

        .banner > ol > li {
            width: 10px;
            height: 10px;
            background-color: white;
            border-radius: 50%;
            cursor: pointer;
        }

        .banner > ol > li.active {
            background-color: #4349b6;
        }

        .banner > div {
            width: 40px;
            height: 65px;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background-color: rgba(50, 50, 50, 0.5);

            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 30px;
            color: white;
        }

        .banner > div.left {
            left: 0;
        }
        .banner > div.right {
            right: 0;
        }
    </style>
</head>
<body>
    <div class="banner">
        <!--图片区域-->
        <ul class="imgBox">
            <li class="active"><img src="../static/img/1.jpg" alt=""></li>
            <li><img src="../static/img/2.jpg" alt=""></li>
            <li><img src="../static/img/3.jpg" alt=""></li>
            <li><img src="../static/img/4.jpg" alt=""></li>
        </ul>

        <!--焦点区域-->
        <ol>
            <li data-i="0" data-name="point" class="active"></li>
            <li data-i="1" data-name="point"></li>
            <li data-i="2" data-name="point"></li>
            <li data-i="3" data-name="point"></li>
        </ol>

        <!--左右切换按钮-->
        <div class="left">&lt;</div>
        <div class="right">&gt;</div>
    </div>

    <script>

        //获取到所有承载图片的 li 和所有承载焦点的 li
        var imgs = document.querySelectorAll('ul > li')
        var points = document.querySelectorAll('ol > li')
        var banner = document.querySelector('.banner')

        //准备一个变量,表示当前是第几张,默认是 0,因为默认显示的是索引第0张
        var index = 0

        //书写一个切换一张
        //约定:参数为 true,我们切换下一张; 参数为false, 切换上一张; 参数为数字,切换到指定索引的那一张
        function changeOne(type) {
            //1. 让当前这一张消失
            imgs[index].className = ''
            points[index].className = ''

            //2. 根据type 传递来的参数,来切换index 的值
            if(type === true) {
                index++
            } else if (type === false){
                index--
            } else {
                index = type
            }

            //判定一下 index 的边界值
            if (index >= imgs.length) {
                index =0
            }
            if(index < 0){
                index = imgs.length - 1
            }

            //3.让改变后的这一张显示出来
            imgs[index].className = 'active'
            points[index].className = 'active'
        }

        //给 轮播图区域盒子 绑定点击事件
        banner.onclick = function (e) {
            //判断点击的是左按钮
            if (e.target.className === 'left') {
                console.lo***击的是左按钮')
                //切换上一张,调用 changeOne 方法传递参数为 false
                changeOne(false)
            }
            //判断点击的是右按钮
            if (e.target.className === 'right'){
                console.lo***击的是右按钮")
                //切换上一张,调用 changeOne 方法传递参数为 true
                changeOne(true)
            }
            //判断点击的是焦点盒子
            if (e.target.dataset.name === 'point'){
                console.lo***击的是焦点按钮")
                //拿到自己身上记录的索引
                var i = e.target.dataset.i - 0
                //切换某一张,调用changeOne方法传递参数为要切换的索引
                changeOne(i)
            }
        }

        //自动切换功能,使用定时器,每5s切换至下一张
        setInterval(function () {
            //切换下一张
            changeOne(true)
        }, 5000)
    </script>
</body>
</html>

 图片:(图片来源于网络)

文章参考视频:b站千锋前端学习营:千锋前端JavaScript全套教程_JS零基础完美入门到项目实战https://www.bilibili.com/video/BV1W54y1J7Ed?share_source=copy_web

全部评论

相关推荐

程序员小白条:这比例牛逼,750:1
点赞 评论 收藏
分享
Hakasee:我的简历和你的基本一样,上周去了上海,boss投了三百家, 三家线下面试 第一家没有做题,全是八股和项目,因为第一次面试不怎么熟练,挂了 第二家,给你几个题目(①css垂直居中文字,字体每两秒闪烁一下以及点击弹窗,②给你一个链接,实现可视化地图,③然后是八股,图片性能优化,以及对图片app有什么想法),45分钟内做完,然后老板面试) 第三家特别偏僻,有点阴森,到了之后让了一个工位给我,有四个题目,①格式化时间 年月日当前时间星期几② 正则表达式提取新闻文字,③在文本域输入文字生成选择题以及选项④生成商品排版还是什么来着 三家都是不超过50人的小公司 两家线上牛客笔试(卡伦特,七牛云,但是笔试不仅要考前端,还要考后端,算法,甚至数学题 我的建议是如果只做了这两个vue项目且不怎么熟练的情况下,先沉淀沉淀,把react学了,上海好的公司基本都是react查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务