改版之轮播图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        div,
        ul,
        li {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
            float: left;
            width: 100px;
            height: 100px;

            background: #ccc;
            font-size: 50px;
            color: #fff;
            line-height: 100px;
            text-align: center;
        }

        .warp {
            position: relative;
            width: 100px;
            margin: 0 auto;
            height: 100px;
            border: 1px solid red;
            /*overflow: hidden;*/
        }

        ul {
            position: absolute;
            top: 0;
            left: 0;

        }

        section {
            overflow: hidden;
        }

        section a {
            float: left;
            width: 20px;
            height: 20px;
            border-radius: 10px;
            background: #5690D2;
            margin-right: 10px;
            margin-bottom: 10px;
            text-align: center;
        }

        section .action {
            background: #FF0000;
        }
    </style>
</head>
<body>
<div class="warp">
    <ul id="list">
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</div>
<section>
    <a href="#" class="action">a</a>
    <a href="#">a</a>
    <a href="#">a</a>
</section>
<input type="button" value="上一个" id="prev"/>
<input type="button" value="下一个" id="next"/>
</body>
</html>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script>
    $(function () {
        var $list = $('#list');
        var $warp = $('.warp');
        var $prev = $('#prev');
        var $next = $('#next');
        var $a = $('a');
        var iNow = 0;//轮播的下标
        var bok = true;//运动完才可再次运动
        var width = 0;

        $list.html($list.html() + $list.html());
        var $li = $list.find('li');
        $.each($list.find('li'), function () {
            width += parseInt($(this).css('width'));
        });
        $list.css('width', width);

        var $liw = width / $li.length;

        //下一个
        $next.click(function () {

            if (bok) {
                iNow++;
                //console.log(iNow);
                $list.stop().animate({'left': -$liw * iNow}, function () {
                    if (iNow == 3) {
                        $list.css({'left': 0});
                        iNow = 0;
                        $a.removeClass('action').eq(iNow).addClass('action');
                    } else {
                        $a.removeClass('action').eq(iNow).addClass('action');
                    }
                    bok = true;
                });
            }
            bok = false;
        });

        //上一个
        $prev.click(function () {
            if (bok) {
                iNow--;
                //console.log(-$liw*($li.length/2+1));
                if (iNow < 0) {
                    iNow = $li.length / 2 - 1;
                    $list.css({'left': -$liw * ($li.length / 2)});
                    $list.stop().animate({'left': -$liw * ($li.length / 2 - 1)}, function () {
                        $a.removeClass('action').eq(iNow).addClass('action');
                        bok = true;
                    });

                } else {
                    $list.stop().animate({'left': -$liw * iNow}, function () {
                        $a.removeClass('action').eq(iNow).addClass('action');
                        bok = true;
                    });
                }
                bok = false;
            }
        });

        //点击a
        $a.click(function () {
            iNow = $(this).index();
            //$a.removeClass('action').eq(iNow).addClass('action');
            $list.stop().animate({'left': -$liw * iNow}, function () {
                $a.removeClass('action').eq(iNow).addClass('action');
            });
        });

    });
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值