用jQuery实现轮播图

怎么用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>

  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值