<!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>
* {
padding: 0;
margin: 0;
}
.box{
width: 100%;
height: 360px;
line-height: 360px;
text-align: center;
font-size: 26px;
font-weight: bold;
}
.listData{
width: 90%;
height: 40px;
line-height: 40px;
text-align: center;
border: 1px dashed red;
margin: 0 auto;
margin-top: 10px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
margin-bottom: 30px;
}
.loadings{
width: 100%;
height: 40px;
text-align: center;
line-height: 40px;
background: skyblue;
}
</style>
</head>
<body>
<div id="app">
<div class="box">这是一个标题</div>
<div class="box"></div>
<p class="listData" v-for="item in arr"> <a href="https://baidu.com">{{ item.title }}</a> </p>
<p class="loadings" v-if="loads">正在加载。。。。</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js"></script>
<script>
var vm = new Vue({
el: '#app',
data: {
arr: [], //需要循环的数据
scrolls: false, //用于判断是否触底
page: 3, //定义传入的page
loads:false //是否显示加载标签
},
methods: {
scroll() {
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
var client = document.documentElement.clientHeight;
var overall = document.body.clientHeight;
//在距离底部 30px 位置的时候就进行调用接口(可按需调动)
if(scrollTop + client >= (overall - 30) && this.scrolls) {
this.scrolls = false;
this.page++;
this.page <= 10 && this.ajaxdata()
if(this.page >= 10){
this.loads = false
}else{
this.loads = true;
}
}
},
//请求数据
async ajaxdata() {
//接口处理
const {data} = await axios.post('https://bj.qhtest.site/byzz/v1.home/analysis', {page: this.page})
var listdata = data.data.list[0].value;
if(data.code == 200) {
this.scrolls = true;
this.loads = false;
this.arr = this.arr.concat(data.data.list[0].value);
}
}
},
created() {
this.ajaxdata();
},
mounted() {
window.addEventListener('scroll', this.scroll)
}
})
</script>
</body>
</html>