点击上下移动

<!DOCTYPE html>

<html>

 

    <head>

        <meta charset="UTF-8">

        <title></title>

        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

        <style>

            * {

                padding: 0;

                margin: 0;

            }

 

            .content {

                margin-top: 80px;

            }

 

            .box {

                width: 100px;

                height: 600px;

                border: 1px solid #000;

                overflow: hidden;

            }

 

            ul {

                list-style: none;

            }

 

            .top {

                width: 100px;

                height: 30px;

                margin-bottom: 10px;

                background-color: #FF4500;

                text-align: center;

                color: #fff;

            }

 

            .bottom {

                width: 100px;

                height: 30px;

                margin-top: 10px;

                background-color: slateblue;

                text-align: center;

                color: #fff;

            }

 

            .content_ul {

                width: 100%;

            }

 

            .li {

                width: 100%;

                height: 100px;

                text-align: center;

                font-size: 2rem;

                color: #fff;

            }

 

            .li:nth-of-type(odd) {

                background: #ff0000;

            }

 

            .li:nth-of-type(even) {

                background: #0000ff;

            }

        </style>

    </head>

 

    <body>

 

        <div class="content">

            <div class="top">TOP</div>

            <div class="box">

                <ul class="content_ul">

                    <li class="li">1</li>

                    <li class="li">2</li>

                    <li class="li">3</li>

                    <li class="li">4</li>

                    <li class="li">5</li>

                    <li class="li">6</li>

                    <li class="li">7</li>

                    <li class="li">8</li>

                </ul>

 

            </div>

            <div class="bottom">bottom</div>

        </div>

 

    </body>

 

</html>

<script>

    $(function() {

        var a = 0;

        var c = 100;

        $(".bottom ").click(function() {

            var box_coordinate = $(".box").offset(); //获取固定的盒子坐标

            var li_coordinate = $(".li:first").offset(); //获取第一个值的坐标

            var top = li_coordinate.top;

 

            if(parseInt(top)!=  parseInt(box_coordinate.top + 1)) {

                a += c;

                $(".content_ul").css("margin-top", a)

            } else {

                alert("已经到顶了哦")

            }

 

        })

 

        $(".top").click(function() {

            var box_coordinate = $(".box").offset(); //获取固定的盒子坐标

 

            var li_coordinate = $(".li:last").offset(); //获取最后一个值

            var top = li_coordinate.top;

 

            var height = $(".box").css("height");

            height = parseInt(height);

            if(parseInt(top) >= parseInt(height + box_coordinate.top +1) ) {

                a -= c;

                $(".content_ul").css("margin-top", a)

            } else {

                alert("已经到底了哦")

            }

 

        })

 

    })

</script>

转载于:https://my.oschina.net/u/3010603/blog/825060

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值