ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【JS】使用JavaScript实现轮播图

2022-07-19 19:31:08  阅读:175  来源: 互联网

标签:index 轮播 JavaScript li height JS banner changeOne left


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
        }

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

        img {
            width: 100%;
            height: 100%;
            display: block;
        }

        header {
            width: 100%;
            height: 50px;
            text-align: center;
        }

        .banner {
            width: 800px;
            height: 500px;
            position: relative;
            margin: 50px 0;
        }

        .banner > ul {
            width: 100%;
            height: 100%;
            position: relative;
        }

        .banner > ul > li {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: opacity .5s linear;
        }

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

        .banner > ol {
            width: 200px;
            height: 30px;
            position: absolute;
            left: 30px;
            bottom: 30px;
            background-color: rgba(0, 0, 0, .5);
            display: flex;
            justify-content: space-around;
            align-items: center;
            border-radius: 15px;
        }

        .banner > ol > li {
            width: 20px;
            height: 20px;
            background-color: #fff;
            border-radius: 50px;
        }

        .banner > ol > li.active {
            background-color: orange;
        }

        .banner > div {
            width: 40px;
            height: 60px;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            background-color: rgba(0, 0, 0, .5);
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 30px;
            color: #fff;
            text-align: center;
            line-height: 60px;
        }

        .banner > div.left {
            left: 0;
        }

        .banner > div.right {
            right: 0;
        }

    </style>
</head>
<body>
<header><h1>欢欢商城</h1></header>
<div class="banner">
    <!-- 图片区域 -->
    <ul class="imgBox">
        <li class="active"><img
                src="https://img14.360buyimg.com/pop/s1180x940_jfs/t1/31576/24/16120/43491/62c24333E0d61b301/1c278ce900303beb.jpg.webp"
                alt=""></li>
        <li><img
                src="https://img14.360buyimg.com/pop/s1180x940_jfs/t1/217320/3/17745/94780/6270f940Eb607dc65/b7ae05d4a569739c.jpg.webp"
                alt=""></li>
        <li><img
                src="https://imgcps.jd.com/ling4/100008348542/5Lqs6YCJ5aW96LSn/5L2g5YC85b6X5oul5pyJ/p-5bd8253082acdd181d02f9ee/73226ace/cr/s/q.jpg"
                alt=""></li>
        <li><img
                src="https://imgcps.jd.com/ling4/100025520132/5Lqs6YCJ5aW96LSn/5L2g5YC85b6X5oul5pyJ/p-5bd8253082acdd181d02f9ff/aa4524e6/cr/s/q.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');
    var left = document.querySelector('.left');
    var right = document.querySelector('.right');
    // 准备一个变量,表示当前是第几张,默认是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) {
        // console.log('点击事件');
        // 判断点击的是左按钮
        if (e.target.className === 'left') {
            console.log('点击的是左按钮');
            // 切换上一张,调用changeOne 方法传递参数为false
            changeOne(false);
        }
        // 判断点击的是右按钮
        if (e.target.className === 'right') {
            console.log('点击的是右按钮');
            // 切换下一张,调用changeOne 方法传递参数为true
            changeOne(true);
        }
        // 判断点击的是焦点盒子
        if (e.target.dataset.name === 'point') {
            console.log('点击的是焦点盒子');
            // 拿到自己身上记录的索引
            var i = e.target.dataset.i - 0;
            // 切换某一种,调用changeOne方法传递参数为要切换的索引
            changeOne(i);
        }
    }
    // 自动切换功能
    timer = setInterval(function () {
        // 切换到下一张
        changeOne(true)
    }, 3000);
    // 显示,隐藏左右按钮
    banner.addEventListener('mouseenter', function () {
        left.style.display = 'block';
        right.style.display = 'block';
        clearInterval(timer);
    })
    banner.addEventListener('mouseleave', function () {
        left.style.display = 'none';
        right.style.display = 'none';
        timer = setInterval(function () {
            // 切换到下一张
            changeOne(true)
        }, 3000);
    })

</script>
</body>
</html>

标签:index,轮播,JavaScript,li,height,JS,banner,changeOne,left
来源: https://www.cnblogs.com/HGNET/p/16495372.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有