首页 前端知识 用jQuery实现轮播图

用jQuery实现轮播图

2024-06-18 23:06:12 前端知识 前端哥 77 927 我要收藏

怎么用jq实现简单的轮播图呢?

首先完成基本html结构:

 <div id="app">
        <div class="wrapper">
            <!-- 图片 图片链接记得改成自己的-->
            <div class="photo">
                <ul>
                    <li><img src="./images/1.jpg" alt=""></li>
                    <li><img src="./images/2.jpg" alt=""></li>
                    <li><img src="./images/3.jpg" alt=""></li>
                    <li><img src="./images/4.jpg" alt=""></li>
                    <li><img src="./images/5.jpg" alt=""></li>
                    <!-- 追加一张,方便无缝衔接 -->
                    <li><img src="./images/1.jpg" alt=""></li>
                </ul>
            </div>
            <!-- 导航点 -->
            <div class="point">
                <ol>
                    <li data-index="0" class="active">1</li>
                    <li data-index="1">2</li>
                    <li data-index="2">3</li>
                    <li data-index="3">4</li>
                    <li data-index="4">5</li>
                </ol>
            </div>
            <!-- 左右按钮 -->
            <div class="btn left"><i></i></div>
            <div class="btn right"><i></i></div>
        </div>
    </div>

然后是css样式:

<style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        img {
            vertical-align: middle;
        }

        .wrapper {
            width: 500px;
            height: 200px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #fff;
            position: relative;
        }

        .photo {
            width: 500px;
            height: 200px;
            overflow: hidden;
        }

        .photo ul {
            width: 1000%;
            height: 200px;
        }

        .photo ul li {
            width: 10%;
            float: left;
        }

        .point {
            position: absolute;
            right: 20px;
            bottom: 20px;
        }

        .point ol li {
            width: 24px;
            height: 24px;
            border: 1px solid #ccc;
            float: left;
            margin: 0 5px;
            background-color: #fff;
            text-align: center;
            line-height: 24px;
            font-size: 15px;
            cursor: pointer;
        }

        .point ol li.active {
            background-color: yellow;
        }

        .btn {
            position: absolute;
            top: 50%;
            margin-top: -20px;
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, .3);
            cursor: pointer;
            text-align: center;
            line-height: 40px;
        }

        .btn.left {
            left: 20px;
        }

        .btn.right {
            right: 20px;
        }

        .btn i {
            display: inline-block;
            vertical-align: middle;
            width: 14px;
            height: 14px;
            border-bottom: 3px solid rgba(255, 255, 255, .5);

        }

        .btn.left i {
            border-left: 3px solid rgba(255, 255, 255, .5);
            transform: rotate(45deg);
            margin-left: 10px;
        }

        .btn.right i {
            border-right: 3px solid rgba(255, 255, 255, .5);
            transform: rotate(-45deg);
            margin-right: 10px;
        }
    </style>

然后就是最重要的script了,记得先引入jq哦!!

//引入jQuery
<script src="./jquery/jquery.js"></script>
<script>
        //图片和导航点的下标
        var index = 0;
        var index2 = 0;
        //图片的宽
        var width = parseInt($(".photo").find('img').css("width"));
        // console.log(width);
        //向右移动
        var toRight = function () {
            index++;
            if (index > 5) {
                index = 1;
                $(".photo ul").css("marginLeft", "0px")
            }
            $('ul').animate({
                marginLeft: -(width * index)
            })
            index2++;
            if (index2 > 4) {
                index2 = 0
            }
            getDH(index2);
        }
        //导航点高亮
        var getDH = function (index2) {
            for (var j = 0; j < $('.point ol li').length; j++) {
                $('.point ol li').eq(j).removeClass('active')
            }
            $('.point ol li').eq(index2).addClass('active')
        }

        // 向左移动
        var toLeft = function () {
            index--
            if (index < 0) {
                index = 4;
                $(".photo ul").css("marginLeft", `-${width * 5}px`)
            }
            $('ul').animate({
                marginLeft: -(width * index)
            })
            index2--;
            if (index2 < 0) {
                index2 = 4;
            }
            getDH(index2);
        }
        //定时器执行向右移动
        var timer = setInterval(function () {
            toRight();
        }, 1500)

        //点击导航点切换图片
        $('.point ol li').on('click', function () {
            //    console.log( $(this));
            $(this).addClass('active').siblings().removeClass('active');
            index2 = $(this).attr("data-index");
            $('ul').animate({
                marginLeft: -(width * index2)
            })
        })

        //点击左右箭头按钮切换图片
        $('.right').on("click", toRight);
        $('.left').on("click", toLeft);

        //鼠标移入停止定时器
        $('.wrapper').on("mouseenter", function () {
            clearInterval(timer);
            // console.log(1);
        })

        //鼠标移出执行定时器
        $('.wrapper').on('mouseleave', function () {
            timer = setInterval(function () {
                toRight();
            }, 3000)
        })

    </script>

这样子就实现了!

下面是全部代码:

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>轮播图</title>
    <script src="./jquery/jquery.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        img {
            vertical-align: middle;
        }

        .wrapper {
            width: 500px;
            height: 200px;
            padding: 10px;
            border: 1px solid #ccc;
            background-color: #fff;
            position: relative;
        }

        .photo {
            width: 500px;
            height: 200px;
            overflow: hidden;
        }

        .photo ul {
            width: 1000%;
            height: 200px;
        }

        .photo ul li {
            width: 10%;
            float: left;
        }

        .point {
            position: absolute;
            right: 20px;
            bottom: 20px;
        }

        .point ol li {
            width: 24px;
            height: 24px;
            border: 1px solid #ccc;
            float: left;
            margin: 0 5px;
            background-color: #fff;
            text-align: center;
            line-height: 24px;
            font-size: 15px;
            cursor: pointer;
        }

        .point ol li.active {
            background-color: yellow;
        }

        .btn {
            position: absolute;
            top: 50%;
            margin-top: -20px;
            width: 40px;
            height: 40px;
            background-color: rgba(0, 0, 0, .3);
            cursor: pointer;
            text-align: center;
            line-height: 40px;
        }

        .btn.left {
            left: 20px;
        }

        .btn.right {
            right: 20px;
        }

        .btn i {
            display: inline-block;
            vertical-align: middle;
            width: 14px;
            height: 14px;
            border-bottom: 3px solid rgba(255, 255, 255, .5);

        }

        .btn.left i {
            border-left: 3px solid rgba(255, 255, 255, .5);
            transform: rotate(45deg);
            margin-left: 10px;
        }

        .btn.right i {
            border-right: 3px solid rgba(255, 255, 255, .5);
            transform: rotate(-45deg);
            margin-right: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="wrapper">
            <!-- 图片 -->
            <div class="photo">
                <ul>
                    <li><img src="./images/1.jpg" alt=""></li>
                    <li><img src="./images/2.jpg" alt=""></li>
                    <li><img src="./images/3.jpg" alt=""></li>
                    <li><img src="./images/4.jpg" alt=""></li>
                    <li><img src="./images/5.jpg" alt=""></li>
                    <!-- 追加一张 -->
                    <li><img src="./images/1.jpg" alt=""></li>
                </ul>
            </div>
            <!-- 导航点 -->
            <div class="point">
                <ol>
                    <li data-index="0" class="active">1</li>
                    <li data-index="1">2</li>
                    <li data-index="2">3</li>
                    <li data-index="3">4</li>
                    <li data-index="4">5</li>
                </ol>
            </div>
            <!-- 按钮 -->
            <div class="btn left"><i></i></div>
            <div class="btn right"><i></i></div>
        </div>
    </div>
    <script>
        //图片和导航点的下标
        var index = 0;
        var index2 = 0;
        //图片的宽
        var width = parseInt($(".photo").find('img').css("width"));
        // console.log(width);
        //向右移动
        var toRight = function () {
            index++;
            if (index > 5) {
                index = 1;
                $(".photo ul").css("marginLeft", "0px")
            }
            $('ul').animate({
                marginLeft: -(width * index)
            })
            index2++;
            if (index2 > 4) {
                index2 = 0
            }
            getDH(index2);
        }
        //导航点高亮
        var getDH = function (index2) {
            for (var j = 0; j < $('.point ol li').length; j++) {
                $('.point ol li').eq(j).removeClass('active')
            }
            $('.point ol li').eq(index2).addClass('active')
        }

        // 向左移动
        var toLeft = function () {
            index--
            if (index < 0) {
                index = 4;
                $(".photo ul").css("marginLeft", `-${width * 5}px`)
            }
            $('ul').animate({
                marginLeft: -(width * index)
            })
            index2--;
            if (index2 < 0) {
                index2 = 4;
            }
            getDH(index2);
        }
        //定时器执行向右移动
        var timer = setInterval(function () {
            toRight();
        }, 1500)

        //点击导航点切换图片
        $('.point ol li').on('click', function () {
            //    console.log( $(this));
            $(this).addClass('active').siblings().removeClass('active');
            index2 = $(this).attr("data-index");
            $('ul').animate({
                marginLeft: -(width * index2)
            })
        })

        //点击左右箭头按钮切换图片
        $('.right').on("click", toRight);
        $('.left').on("click", toLeft);

        //鼠标移入停止定时器
        $('.wrapper').on("mouseenter", function () {
            clearInterval(timer);
            // console.log(1);
        })

        //鼠标移出执行定时器
        $('.wrapper').on('mouseleave', function () {
            timer = setInterval(function () {
                toRight();
            }, 3000)
        })

    </script>
</body>

</html>

转载请注明出处或者链接地址:https://www.qianduange.cn//article/12693.html
标签
评论
发布的文章
大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!