【问题标题】:Enabling two-column infinite scrolling with ISCroll使用 ISCroll 启用两列无限滚动
【发布时间】:2015-04-04 18:23:47
【问题描述】:

我想使用 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% 并将每个元素的定位更改为相对,但不断得到类似于:

|A    |
|乙|
|C    |
| D|

【问题讨论】:

标签: javascript html css infinite-scroll iscroll


【解决方案1】:

试试这个....

<!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>

【讨论】:

  • 您能指出使这项工作发挥作用的关键区别吗?当我确定大部分代码都是直接从演示中获取时,需要阅读大量代码。我们不必通过 diff 来查看是什么解决了问题。
  • 是的,你是对的。这是修改后的演示。你有一个带有 class"row" 的 div,它使你的列表的行 (infiniteElements: '#scroller .row', ) 在你注入两个 div 的行内带有 class "col" 。 (参见 requestData 方法)。 “col1”和“col2”是每一行的左右列。希望这对您有所帮助。 :) 再见
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
相关资源
最近更新 更多