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