一个简单的轮播图(jQuery练习)

183 阅读2分钟

不说废话,直接上代码。算是自留记录存档吧

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

        #div1 {
            width: 850px;
            height: 500px;
            margin: 50px auto;
            overflow: hidden;
            position: relative;
        }

        #div1 ul {
            position: absolute;
            left: 0;
            top: 0;
        }

        #div1 ul li {
            height: 500px;
            float: left;
        }

        #div1 ol {
            position: absolute;
            right: 10%;
            bottom: 10px;
            z-index: 6
        }

        #div1 ol li {
            width: 20px;
            height: 20px;
            background: #fff;
            margin-left: 5px;
            float: left;
            line-height: 20px;
            text-align: center;
        }

        #div1 ol li.ac {
            background: red;
            color: #fff;
        }

        #div1>a {
            text-decoration: none;
            position: absolute;
            top: 50%;
            margin-top: -10px;
            height: 40px;
            line-height: 32px;
            text-align: center;
            width: 40px;
            font-size: 40px;
            vertical-align: middle;
            color: #fff;
            background: rgba(0, 0, 0, 0.5);
            z-index: 6;
        }

        #goPrev {
            left: 0;
        }

        #goNext {
            right: 0;
        }

        img {
            width: 850px;
            height: 500px;
        }
    </style>

结构部分

<div id="div1">
        <ul>
            <li><img src="../images/1.jpg" /></li>
            <li><img src="../images/2.jpg" /></li>
            <li><img src="../images/3.jpg" /></li>
            <li><img src="../images/4.jpg" /></li>
            <li><img src="../images/5.jpg" /></li>
            <li><img src="../images/6.jpg" /></li>
        </ul>
        <ol>

        </ol>
        <a href="javascript:;" id="goPrev">&laquo;</a>
        <a href="javascript:;" id="goNext">&raquo;</a>
    </div>

js部分

 <script>
        //相当于window.onload
        $(function () {
            const $imgs = $('#div1 ul li'),
                $ul = $('#div1 ul'),
                div1 = $('#div1')
            const $len = $imgs.length,
                width = $imgs.eq(0).outerWidth()
            var index = 0,
                lastindex = 0
            var ismove = false

            //克隆第一个li到最后一个去
            //计算ul的宽度
            $ul.append($imgs.eq(0).clone()).css('width', (($len + 1) * width))

            //创建下面的按钮
            for (let i = 0; i < $len; i++) {
                $('<li>').html(i + 1).appendTo($('ol')).addClass(i === 0 ? 'ac' : '')
            }
            //点击按钮按钮设为当前对象(添加classname  ac
            //获取按钮
            $btns = $('#div1 ol li')
            $btns.on('click', function () {
                if (ismove) return
                ismove = true
                //点击的时候当前按钮获取有ac,其他的按钮没有
                $(this).addClass('ac').siblings().removeClass('ac')
                //相应的更改轮播图的图片
                lastindex = index
                index = $(this).index()
                $ul.animate({
                    left: -index * width
                }, 1000, function () {
                    ismove = false
                })
            })
            //点击左右切换图片
            $('#goNext').on('click', function () {
                if (ismove) return
                ismove = true
                lastindex = index
                index++
                if (index === $len) {
                    index = 0
                    $ul.animate({
                        //当index=len时,移动到最边上的图,但这个时候index和lastindex都不等于len
                        left: -$len * width
                        //运动结束,让ul瞬间回到index=0的位置
                    }, 1000, function () {
                        $ul.css('left', 0)
                        ismove = false
                    })
                } else {
                    $ul.animate({
                        left: -index * width
                    }, 1000, function () {
                        ismove = false
                    })

                }
                $btns.eq(index).addClass('ac')
                $btns.eq(lastindex).removeClass('ac')

            })
            $('#goPrev').on('click', function () {
                if (ismove) return
                ismove = true
                lastindex = index
                index--
                if (index < 0) {
                    index = $len - 1
                    $ul.css('left', -$len * width).animate({
                        left: -index * width
                    }, 1000, function () {
                        ismove = false
                    })
                } else {
                    $ul.animate({
                        left: -index * width
                    }, 1000, function () {
                        ismove = false
                    })
                }
                $btns.eq(index).addClass('ac')
                $btns.eq(lastindex).removeClass('ac')

            })
            //轮播能够进去就执行的原因:fn是立刻执行函数,加载完页面之后会马上执行

            div1.hover(function () {
                clearInterval(div1.timer)
            }, function fn() {
                div1.timer = setInterval(function () {
                    $('#goNext').trigger('click')
                }, 2000)
                return fn
            }())
        })
    </script>