Skip to content

Instantly share code, notes, and snippets.

@erikfig
Created December 23, 2018 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikfig/90964b668d2784455d2ae60f1cab53f5 to your computer and use it in GitHub Desktop.
Save erikfig/90964b668d2784455d2ae60f1cab53f5 to your computer and use it in GitHub Desktop.
<template></template>
<script>
export default {
props: {
paginationBox: {
required: true
},
src: {
default: ''
},
margin: {
default: 0
}
},
data() {
return {
currentPage: 1,
onLoad: false
}
},
methods: {
load(url, element) {
return fetch(url).then((res) => {
return res.text();
}).then((data) => {
let slicex = data.indexOf("<body");
slicex = data.indexOf(">", slicex);
const slicey = data.lastIndexOf("</body>");
const content = data.slice(slicex + 1, slicey);
const body = document.createElement("body");
body.innerHTML = content;
const response = body.querySelector(element);
if (response) {
return response.innerHTML;
}
return null;
})
},
getMore() {
this.currentPage++;
this.onLoad = true;
this.load(this.src + this.currentPage, this.paginationBox).then((data) => {
const item = document.querySelector(this.paginationBox);
item.innerHTML += data;
this.onLoad = false;
});
},
checkNeedMoreData() {
let bottom = this.$el.getBoundingClientRect().bottom;
bottom -= this.margin;
return bottom <= window.innerHeight && !this.onLoad;
}
},
mounted() {
if (this.checkNeedMoreData()) {
this.getMore();
}
window.addEventListener('scroll', () => {
if (this.checkNeedMoreData()) {
this.getMore();
}
});
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment