案例說明
-
效果圖
父組件
<template>
<div class="scroll_wrap" ref="wrapper" id="scroll_wrap">
<mt-loadmore :bottomDistance="5" :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore" :autoFill="isAutoFill">
<oneColum :liveList="liveList" :handleScroll="handleScroll"></oneColum>
</mt-loadmore>
</div>
</template>
export default {
name: "Learn",
data(){
return{
page: 1,
allLoaded: false, //可以進(jìn)行上拉
isAutoFill: false, //是否自動(dòng)觸發(fā)上拉函數(shù)
scroll:0,
}
},
created() {
this.getLearnLIst(1);
},
methods: {
// 下拉刷新
loadTop() {
this.$refs.loadmore.onTopLoaded();
this.getLearnLIst(1)
},
//上拉加載
loadBottom() {
this.getLearnLIst(this.page);
this.$refs.loadmore.onBottomLoaded();
},
getLearnLIst(page){
this.page=page
this.option={ }
this.$http.post('api/studySchedule/getStudyScheduleList',{page:page})
.then((res)=>{
console.log(res.body);
if(res.body.code==1){
let datas = res.body.data.list
this.page==1?this.liveList = datas: this.liveList=this.liveList.concat(datas);
if (this.page == res.body.data.lastPage) {
this.allLoaded = true; // 若數(shù)據(jù)已全部獲取完畢
}
this.page++;
}
})
},
// 獲取滾動(dòng)條向上滾動(dòng)的高度
handleScroll(scroll) {
scroll = this.$refs.wrapper.scrollTop;
this.scroll = scroll;
},
// 定位保存的scroll(即從詳情頁返回列表頁返回時(shí)的位置)
activated() {
this.$refs.wrapper.scrollTop = this.scroll;
},
// 判斷進(jìn)入詳情頁之前的地址
beforeRouteLeave(to, from, next) {
// this.handleScroll(); //如果沒有子組件
if (to.name == "coursedetail") {
from.meta.keepAlive = true;
} else {
from.meta.keepAlive = false;
}
next();
},
}
</script>
子組件
<template>
<div class="one_colum">
<div v-for="(item,i) in courseList" :key="i" >
<img v-lazy="item.img_url" >
</div>
</div>
</template>
<script>
export default {
name: "OneColum",
props: ['courseList','handleScroll', ],
data() {
return {
scroll:0,
}
},
methods: {
//進(jìn)入詳情
goDetail(id,item_type){
//存儲(chǔ) scrollTop 的值
if(this.handleScroll != undefined){
this.handleScroll(this.scroll)
}
}
}
}
</script>