jQuery 实现轮播(初始 + 升级 + 插件推荐)

内容介绍

  jQuery 实现轮播(初始 + 升级 + 插件推荐)。

一、初级轮播
1、效果图

在这里插入图片描述
👇 👇 👇 👇 👇

在这里插入图片描述

2、 实现代码
<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul,
        ol,
        li {
            list-style: none;
        }
        
        .slider-wrapper {
            width: 500px;
            height: 300px;
            overflow: hidden;
            margin: 50px auto;
            position: relative;
            border: 1px solid #ccc;
        }
        
        .slider-content {
            width: 2500px;
            height: 100%;
        }
        
        .slider-content>li {
            width: 500px;
            height: 300px;
            float: left;
            font-size: 100px;
            line-height: 300px;
            text-align: center;
            font-weight: bold;
        }
        
        .slider-content>li.one {
            background-color: red;
        }
        
        .slider-content>li.two {
            background-color: blue;
        }
        
        .slider-content>li.three {
            background-color: yellow;
        }
        
        .slider-content>li.four {
            background-color: green;
        }
        
        .slider-content>li.five {
            background-color: pink;
        }
        
        .nav {
            width: 50px;
            height: 50px;
            background-color: gray;
            position: absolute;
            top: 50%;
            left: 0;
            margin-top: -25px;
            cursor: pointer;
        }
        
        .nav-right {
            left: auto;
            right: 0;
        }
    </style>
</head>

<body>
    <div class="slider-wrapper">
        <ul class="slider-content">
            <li class="one">1</li>
            <li class="two">2</li>
            <li class="three">3</li>
            <li class="four">4</li>
            <li class="five">5</li>
        </ul>
        <div class="nav-wrapper">
            <div class="nav nav-left"></div>
            <div class="nav nav-right"></div>
        </div>
    </div>
    <script>
        let width = $('.slider-content >li').width()

        function init() {
            initEvent()
        }

        function initEvent() {
            $('.nav').on('click', onNavClick)
        }

        function onNavClick() {
            let $this = $(this)
            if ($(':animated').length > 0) {
                return
            }
            if ($this.hasClass('nav-right')) {
                $('.slider-content').animate({
                    'margin-left': -width
                }, 500, function() {
                    $(this)
                        .css('marginLeft', 0)
                        .append($(this).find('li:first'))
                })
            } else {
                $('.slider-wrapper ul')
                    .prepend($('.slider-wrapper ul>li:last'))
                    .css({
                        'marginLeft': -width
                    })
                    .animate({
                        'marginLeft': 0
                    }, 500, function() {
                        console.log('over')
                    })
            }
        }

        init()
    </script>
</body>

</html>
二、升级轮播
1、效果图

在这里插入图片描述

👇 👇 👇 👇 👇

在这里插入图片描述

2、 实现代码
<!DOCTYPE html>
<html lang="en">

<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">
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <title>升级轮播</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        ul,
        ol,
        li {
            list-style: none
        }
        
        .slider-wrapper {
            width: 500px;
            height: 300px;
            overflow: hidden;
            margin: 50px auto;
            position: relative;
            border: 1px solid #ccc;
        }
        
        .slider-content {
            width: 2500px;
            height: 100%;
        }
        
        .slider-content>li {
            width: 500px;
            height: 300px;
            float: left;
            font-size: 100px;
            line-height: 300px;
            text-align: center;
            font-weight: bold;
        }
        
        .slider-content>li.one {
            background-color: red;
        }
        
        .slider-content>li.two {
            background-color: blue;
        }
        
        .slider-content>li.three {
            background-color: yellow;
        }
        
        .slider-content>li.four {
            background-color: green;
        }
        
        .slider-content>li.five {
            background-color: pink;
        }
        
        .nav {
            width: 50px;
            height: 50px;
            background-color: gray;
            position: absolute;
            top: 50%;
            left: 0;
            margin-top: -25px;
            cursor: pointer;
        }
        
        .nav-right {
            left: auto;
            right: 0;
        }
        
        .slider-nav-wrapper {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translate(-50%);
        }
        
        .slider-nav-wrapper>li {
            float: left;
            margin-right: 10px;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: black;
            cursor: pointer;
        }
        
        .slider-nav-wrapper>li.on {
            background-color: #fff;
        }
    </style>
</head>

<body>
    <div class="slider-wrapper">
        <ul class="slider-content">
            <li index="0" class="one">1</li>
            <li index="1" class="two">2</li>
            <li index="2" class="three">3</li>
            <li index="3" class="four">4</li>
            <li index="4" class="five">5</li>
        </ul>
        <ol class="slider-nav-wrapper">
            <li class="on"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
        <div class="nav-wrapper">
            <div class="nav nav-left"></div>
            <div class="nav nav-right"></div>
        </div>

        <script>
            let width = $('.slider-content > li').width()
            let $imgWrapper = $('.slider-content');

            function init() {
                initEvent()
            }

            function initEvent() {
                $('.nav').on('click', onNavClick)
                $('.slider-nav-wrapper').on('click', 'li', onSliderNavWrapperClick)
            }

            function onSliderNavWrapperClick() {

                if ($(':animated').length > 0) {
                    return
                }

                let $tmp = $imgWrapper.find('li:first').clone()
                let newIndex = $(this).index()
                let oldIndex = $imgWrapper.find('li:first').attr('index')


                if (newIndex > oldIndex) {
                    for (let i = 0; i < newIndex - oldIndex; i++) {
                        $imgWrapper.append($imgWrapper.find('li:first'))
                    }
                    $imgWrapper
                        .prepend($tmp)
                        .animate({
                            'margin-left': -width
                        }, 500, function() {
                            $(this)
                                .find('li:first')
                                .remove()
                                .end()
                                .css('marginLeft', 0)
                            changeIconColor()
                        })
                } else if (newIndex < oldIndex) {
                    for (let i = 0; i < oldIndex - newIndex; i++) {
                        $imgWrapper.prepend($imgWrapper.find('li:last'))
                    }
                    $imgWrapper
                        .find('li:first')
                        .after($tmp)
                        .end()
                        .css('marginLeft', -width)
                        .animate({
                            'marginLeft': 0
                        }, 500, function() {
                            $tmp.remove()
                            changeIconColor()
                        })
                }
            }

            function onNavClick() {
                let $this = $(this)
                if ($(':animated').length > 0) {
                    return
                }
                if ($this.hasClass('nav-right')) {
                    $('.slider-content').animate({
                        'margin-left': -width
                    }, 500, function() {
                        $(this)
                            .css('marginLeft', 0)
                            .append($(this).find('li:first'))
                        changeIconColor()
                    })
                } else {
                    $('.slider-wrapper ul')
                        .prepend($('.slider-wrapper ul>li:last'))
                        .css({
                            'marginLeft': -width
                        })
                        .animate({
                            'marginLeft': 0
                        }, 500, function() {
                            changeIconColor()
                        })
                }
            }

            function changeIconColor() {
                let currIndex = $imgWrapper.find('li:first').attr('index')
                $('.slider-nav-wrapper')
                    .find('li')
                    .eq(currIndex)
                    .addClass('on')
                    .siblings('.on')
                    .removeClass('on')
            }

            init()
        </script>
</body>

</html>
插件推荐
1、 插件介绍

  Swiper 是一款免费以及轻量级的移动设备触控滑块的js框架,使用硬件加速过渡(如果该设备支持的话)。主要使用于移动端的网站、移动web apps,native apps和hybrid apps。主要是为IOS而设计的,同时,在Android、WP8系统也有着良好的用户体验,Swiper从3.0开始不再全面支持PC端。因此,如需在PC上兼容更多的浏览器,可以选择Swiper2.x(甚至支持IE7)。

在这里插入图片描述

2、 相关网址

swiper中文网:https://www.swiper.com.cn/usage/index.html
swiper案例演示:https://www.swiper.com.cn/demo/index.html


标签:jQuery,轮播


更多演示案例,查看 案例演示


欢迎评论留言!

  • 68
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 115
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

人间小美味695

您的鼓励是我创作的动力,感谢!

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

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

打赏作者

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

抵扣说明:

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

余额充值