add infinity scroll
This commit is contained in:
parent
cd4809f828
commit
f624109dea
59
src/App.vue
59
src/App.vue
@ -18,12 +18,12 @@
|
|||||||
|
|
||||||
<div v-if="isPostLoading" style="margin-top: 15px">Loading...</div>
|
<div v-if="isPostLoading" style="margin-top: 15px">Loading...</div>
|
||||||
<post-list v-else :posts="sortedAndSearchedPosts" @removePost="removePost" class="postList"/>
|
<post-list v-else :posts="sortedAndSearchedPosts" @removePost="removePost" class="postList"/>
|
||||||
|
<div ref="observer" class="observer"></div>
|
||||||
<my-pagination
|
<!-- <my-pagination-->
|
||||||
:currentPage="page"
|
<!-- :currentPage="page"-->
|
||||||
:totalPages="totalPages"
|
<!-- :totalPages="totalPages"-->
|
||||||
@changePage="changePage"
|
<!-- @changePage="changePage"-->
|
||||||
/>
|
<!-- />-->
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@ -80,14 +80,46 @@ export default {
|
|||||||
alert(e.message);
|
alert(e.message);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
changePage(newPage) {
|
async loadMorePost() {
|
||||||
this.page = newPage;
|
this.page += 1
|
||||||
|
try {
|
||||||
|
const { data, headers } = await axios.get(`https://jsonplaceholder.typicode.com/posts`, {
|
||||||
|
params: {
|
||||||
|
_limit: this.limit,
|
||||||
|
_page: this.page,
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
this.posts.push(...data);
|
||||||
|
this.totalPages = Math.ceil(headers['x-total-count'] / this.limit);
|
||||||
|
} catch (e) {
|
||||||
|
alert(e.message);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// changePage(newPage) {
|
||||||
|
// this.page = newPage;
|
||||||
|
// }
|
||||||
},
|
},
|
||||||
async mounted() {
|
async mounted() {
|
||||||
this.isPostLoading = true;
|
this.isPostLoading = true;
|
||||||
await this.fetchPosts()
|
await this.fetchPosts()
|
||||||
this.isPostLoading = false
|
this.isPostLoading = false
|
||||||
|
|
||||||
|
|
||||||
|
console.log();
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
rootMargin: "0px",
|
||||||
|
threshold: 1.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const callback = (entries) => {
|
||||||
|
if (entries[0].isIntersecting && this.page < this.totalPages) {
|
||||||
|
this.loadMorePost();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const observer = new IntersectionObserver(callback, options);
|
||||||
|
|
||||||
|
observer.observe(this.$refs.observer)
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
sortedPost() {
|
sortedPost() {
|
||||||
@ -112,9 +144,9 @@ export default {
|
|||||||
document.body.style.overflow = 'auto';
|
document.body.style.overflow = 'auto';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
page() {
|
// page() {
|
||||||
this.fetchPosts()
|
// this.fetchPosts()
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@ -147,4 +179,9 @@ export default {
|
|||||||
.search {
|
.search {
|
||||||
margin-top: 15px;
|
margin-top: 15px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.observer {
|
||||||
|
height: 0px;
|
||||||
|
background: teal;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user