js上一个下一个左右滚动实现

.outerdiv {
        width: 100%;

    }
    .innerdiv{
        white-space: nowrap;
        width:70px;
        height: 70px;
        border:1px solid green;
        overflow: hidden;
        float:left;

    }
    .outerdiv ul {
        width: 1000px;
    }
    .outerdiv ul li {
        white-space: nowrap;
        font-size: 14px;
        float:left;


    }

    .outerdiv ul li {
        width: 50px;
        height: 50px;
        margin: 10px 10px ;

    }
    .outerdiv ul li:nth-child(2n) {
        background-color: red;

    }
    .outerdiv ul li:nth-child(2n+1){
        background-color: cornflowerblue;

    }
    .outerdiv ul li:last-child{
        background-color: transparent;
    }
    .leftdiv{
        height:70px;
        float: left;
       margin-right: 50px;
    }
    .rightdiv{
        height: 70px;
        float: left;
       margin-left:50px;
    }
    .centerdiv{
        height: 70px;
        width: 600px;
        margin: 30px auto;
    }
</style>
</head>
<body>
<div class="outerdiv">
    <div class="centerdiv">
        <div class="leftdiv">
            <input type="button" name="pre" value="pre">
        </div>
        <div class="innerdiv">
            <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>6</li>
                <li>7</li>
                <li>8</li>
                <li></li>
            </ul>
        </div>
        <div class="rightdiv">
            <input type="button" name="next" value="next">
        </div>

    </div>

</div>

<script src="../js/libs/jquery-1.11.3.js">
</script>
<script src="../js/libs/jquery.almvalidate.js"></script>

<script>
    var scrollWidth = $("div.innerdiv").get(0).scrollWidth;
    $("[name=pre]").bind("click",function(){
        //多增加li 错位处理
        var  left = $("div.innerdiv").get(0).scrollLeft;
        var width = $("div.innerdiv").width();
        var scrollWidth = $("div.innerdiv").get(0).scrollWidth-width;

        if(left + width >= scrollWidth){
            $("div.innerdiv").animate({"scrollLeft":0},500);
        }else{
            $("div.innerdiv").animate({"scrollLeft":left+width},500);
        }
    });

    $("[name=next]").bind("click",function(){
        var  left = $("div.innerdiv").get(0).scrollLeft;
        var width = $("div.innerdiv").width();
        var maxLeft = $("div.innerdiv").get(0).scrollWidth - width*2;
        var justLeft = maxLeft+10; //10 margin
        if(left == 0){
            $("div.innerdiv").animate({"scrollLeft":justLeft},500);
        }else{
            $("div.innerdiv").animate({"scrollLeft":left-width},500);
        }


    });
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值