使用ISCroll启用两列无限滚动

时间:2015-02-05 01:37:30

标签: javascript html css infinite-scroll iscroll

我想使用IScroll无限滚动实现多列无限滚动。我想要我的内容为我的HTML:

<ul>
  <li>A</li>
  <li>B</li>
  <li>C</li>
  <li>D</li>
</ul>

被视为:

| A B |
| C D |

在我看来。我如何调整https://github.com/cubiq/iscroll/tree/master/demos/infinite以显示两列中的元素?我尝试过设置display:inline-block和width:45%并将每个元素的定位更改为relative,但是得到的结果类似于:

| A |
| C |
| C |
| d |

1 个答案:

答案 0 :(得分:0)

试试这个......

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">

    <title>iScroll demo: infinite scrolling</title>

    <script type="text/javascript" src="../../build/iscroll-infinite.js"></script>

    <script type="text/javascript">
        var myScroll;

        function loaded() {
            myScroll = new IScroll('#wrapper', {
                mouseWheel: true,
                infiniteElements: '#scroller .row',
                infiniteLimit: 2000,
                dataset: requestData,
                dataFiller: updateContent,
                cacheSize: 16
            });
        }

        function requestData(start, count) {
            setTimeout(function () {
                var data = [];
                if (start < 50) {
                    for (i = 0; i < count; i++) {
                        data[i] = '<div class="col1 col">Row ' + (start + i * 2) + '</div>';
                        data[i] += '<div class="col2 col">Row ' + (start + i * 2 + 1) + '</div>';
                    }
                }
                myScroll.updateCache(start, data);
            }, 500);
        }

        function updateContent(el, data) {
            if (data != undefined)
                el.innerHTML = data;
        }

        document.addEventListener('touchmove', function (e) {
            e.preventDefault();
        }, false);

    </script>

    <style type="text/css">
        * {
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }

        html {
            -ms-touch-action: none;
        }

        body, ul, li {
            padding: 0;
            margin: 0;
            border: 0;
        }

        body {
            font-size: 12px;
            font-family: ubuntu, helvetica, arial;
            overflow: hidden; /* this is important to prevent the whole page to bounce */
        }

        #header {
            position: absolute;
            z-index: 2;
            top: 0;
            left: 0;
            width: 100%;
            height: 45px;
            line-height: 45px;
            background: #CD235C;
            padding: 0;
            color: #eee;
            font-size: 20px;
            text-align: center;
            font-weight: bold;
        }

        #footer {
            position: absolute;
            z-index: 2;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 48px;
            background: #444;
            padding: 0;
            border-top: 1px solid #444;
        }

        #wrapper {
            position: absolute;
            z-index: 1;
            top: 45px;
            bottom: 48px;
            left: 0;
            width: 100%;
            background: #ccc;
            overflow: hidden;
        }

        #scroller {
            position: absolute;
            z-index: 1;
            -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
            width: 100%;
            -webkit-transform: translateZ(0);
            -moz-transform: translateZ(0);
            -ms-transform: translateZ(0);
            -o-transform: translateZ(0);
            transform: translateZ(0);
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            -webkit-text-size-adjust: none;
            -moz-text-size-adjust: none;
            -ms-text-size-adjust: none;
            -o-text-size-adjust: none;
            text-size-adjust: none;
        }

        #scroller ul {
            list-style: none;
            padding: 0;
            margin: 0;
            width: 100%;
            text-align: left;
            position: relative;
        }

        .row {
            width: 100%;
            height: 150px;
            position: absolute;
            -webkit-transform: translateZ(0);
            -moz-transform: translateZ(0);
            -ms-transform: translateZ(0);
            -o-transform: translateZ(0);
            transform: translateZ(0);
            padding: 0 10px;
            border-bottom: 1px solid #ccc;
            border-top: 1px solid #fff;
            background-color: #fafafa;
            font-size: 16px;
        }

        .col1, .col2 {
            float: left;
            width: 50%;
            height: 100%;
            border: 1px solid gray;
        }

        .col1 {
            left: 0%;
        }

        .col2 {
            left: 50%;
        }

    </style>
</head>
<body onload="loaded()">
<div id="header">iScroll</div>

<div id="wrapper">
    <div id="scroller">
        <div class="row">
            <div class="col1 col">Row 1</div>
            <div class="col2 col">Row 2</div>
        </div>
        <div class="row">
            <div class="col1 col">Row 3</div>
            <div class="col2 col">Row 4</div>
        </div>
        <div class="row">
            <div class="col1 col">Row 5</div>
            <div class="col2 col">Row 6</div>
        </div>
        <div class="row">
            <div class="col1 col">Row 7</div>
            <div class="col2 col">Row 8</div>
        </div>

    </div>
</div>

<div id="footer"></div>

</body>
</html>