上下滚动实现

来源:互联网 发布:ui设计师与美工的区别 编辑:程序博客网 时间:2024/05/20 11:34

<style>
        body, ul, li, p {
            padding: 0;
            margin: 0;
        }


        ul, li {
            list-style: none;
        }


        .main-wrap {
            margin-top: 50px;
            width: 100%;


        }


        .scroll-wrap {
            width: 500px;
            height: 300px;
            margin: 0px auto;
            overflow: hidden;
            position: relative;
        }


        .scroll-show-wrap {
            width: 500px;
            height: 300px;
            overflow: hidden;
        }


        .scroll-show1 {
            width: 500px;
            height: 300px;
            background-color: red;
        }


        .scroll-show2 {
            width: 500px;
            height: 300px;
            background-color: gold;
        }


        .scroll-show3 {
            width: 500px;
            height: 300px;
            background-color: green;
        }


        .scroll-item-warp{
            position: absolute;
            bottom: 20px;
            right: 20px;
        }
        .scroll-item-warp ul li{
            float:left;
            width: 30px;
            height: 30px;
            background-color: #31b0d5;
            color: white;
            font-weight: 600;
            text-align: center;
            margin-right: 5px;
            border: 1px solid #808080;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="main-wrap">
    <div class="scroll-wrap">
        <div class="scroll-show-wrap">
            <div class="scroll-show1"></div>
            <div class="scroll-show2"></div>
            <div class="scroll-show3"></div>
        </div>
        <div class="scroll-item-warp">
            <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
            </ul>
        </div>
    </div>
</div>
<script src="../js/libs/jquery-1.11.3.js">
</script>
<script>
    var height = $(".scroll-wrap").height();
    /*  var scrollMaxHeight = ($(".scroll-item-warp ul li").length-1)*height;*/
    //也可以
    var scrollMaxHeight = $(".scroll-show-wrap").get(0).scrollHeight-$(".scroll-show-wrap").get(0).clientHeight;
    $(".scroll-item-warp ul li").first().css({"background-color":"#C27310"});


    //定时
    setInterval("scrollFun()",3000);
    var scrollFun = function () {
        $(".scroll-item-warp ul li").css({"background-color":"#31b0d5"});
        var currentScrollTop = $(".scroll-show-wrap").get(0).scrollTop;


        if( $(".scroll-show-wrap").get(0).scrollTop >= scrollMaxHeight){
            $(".scroll-item-warp ul li").eq(0).css({"background-color":"#C27310"});
            $(".scroll-show-wrap").animate({
                "scrollTop":0
            });
        }else{
            var index = currentScrollTop/height+1;
            $(".scroll-item-warp ul li").eq(index).css({"background-color":"#C27310"});
            $(".scroll-show-wrap").animate({
                "scrollTop":'+='+height
            });
        }


    };
    $("li").click(function () {
      var index = $(".scroll-item-warp ul li").index($(this));
        $(".scroll-item-warp ul li").css({"background-color":"#31b0d5"});
        $(this).css({"background-color":"#C27310"});
        var scrollTop = index*height;


        $(".scroll-show-wrap").animate({
            "scrollTop":scrollTop
        });


    });

0 0
原创粉丝点击