滚动加载更多

浏览:3677 发布日期:2016/12/29 分类:用法示例 关键字: laytpl 滚动懒加载
用到了laytpl js模板引擎,当滚动到底部的时候加载更多数据。只是写了个简单的演。
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8"/>
    <script type="text/javascript"  src="./laytpl/laytpl.js"></script>
    <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

<script id="demo" type="text/html">
{{# for (var i=0,len=d.list.length;i<len;i++){ }}
<h1>{{ d.list[i].name }}</h1>
{{# } }}
</script>

<div id="view">
    正在加载中...
</div>

 <script type="text/javascript">
//滚动后加载
var data = {
  list: [{name: '2'}, {name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'}, {name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'}, {name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'},{name: '2'}]
};
//page 1
var data1 = {
  list: [{name: '1'}, {name: 'b'},{name: 'c'},{name: 'd'},{name: 'a'}, {name: 'b'},{name: 'c'},{name: 'd'},{name: 'a'}, {name: 'b'},{name: 'c'},{name: 'd'},{name: 'b'},{name: 'c'},{name: 'd'},{name: 'b'},{name: 'c'},{name: 'd'},{name: 'b'},{name: 'c'},{name: 'd'},{name: 'b'},{name: 'c'},{name: 'd'},{name: 'b'},{name: 'c'},{name: 'd'}]
};

</script>
<script type="text/javascript">
function getScrollHeight() {
    var scrollHeight = 0;
    var bodyScrollHeight = 0;
    var documentScrollHeight = 0;
    if (window.document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;
}
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
function getScrollTop() {
    var scrollTop = 0,
        bodyScrollTop = 0,
        documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
document.addEventListener('scroll', function() {
    //判段是否到达底部
        if (getScrollTop() + getWindowHeight() == getScrollHeight()) {
            // var newpage = parseInt(page.value) + 1;
        var gettpl = document.getElementById('demo').innerHTML;
        laytpl(gettpl).render(data, function(html){
          // 追加数据
          $('#view').append(html);
          console.log('我被加载了');

        });

            
        }
    });

//第一次加载数据
var gettpl = document.getElementById('demo').innerHTML;
laytpl(gettpl).render(data1, function(html){
  document.getElementById('view').innerHTML = html;
});
</script>

</body>
</html>

附件 demo.zip ( 4.36 KB 下载:173 次 )

评论( 相关
后面还有条评论,点击查看>>