<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style>
#table_content {
width: 80%;
height: 300px;
border: 1px solid #ccc;
overflow-y: scroll;
}
</style>
</head>
<body>
<div id="app">
<div id="table_content" @scroll="scroll">
<table id="table" border="1">
<tr v-for="item in list">
<td>{{item.id}}</td>
<td>{{item.title}}</td>
</tr>
</table>
</div>
</div>
</body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script>
var vm = new Vue({
el: '#app',
data: {
list: [],//列表数据
page: 1,//分页,默认为第一页
status: 1,//状态,为0不在请求数据
},
methods: {
getData() {
var that = this;
if(that.status==0){
console.log("没有更多啦");
return false;
}
$.ajax({
url: "http://jsonplaceholder.typicode.com/todos",
data: {
_page: that.page,
_limit: 15
},
success: function(result) {
if (that.page == 1) {
that.list = result;
setTimeout(function(){
that.scroll();
});
} else {
if (result.length > 0) {
that.list = that.list.concat(result);
setTimeout(function(){
that.scroll();
});
} else {
that.status = 0;//没有数据将状态改为0
console.log("没有更多啦");
}
}
}
});
},
scroll() {
var that = this;
var scrollHeight = document.getElementById("table_content").scrollHeight;
var scrollTop = document.getElementById("table_content").scrollTop;
var clientHeight = document.getElementById("table_content").clientHeight;
console.log(scrollHeight+"----"+scrollTop+"----"+clientHeight);
if (scrollHeight - clientHeight == scrollTop) {
that.page++;
that.getData();
}
}
}
})
// 首次请求数据
vm.getData();
</script>
</html>
网友评论