Skip to content

Instantly share code, notes, and snippets.

@alexalannunes
Created January 17, 2021 14:43
Show Gist options
  • Select an option

  • Save alexalannunes/25a85eda9839fc220f5c5b3a7141d954 to your computer and use it in GitHub Desktop.

Select an option

Save alexalannunes/25a85eda9839fc220f5c5b3a7141d954 to your computer and use it in GitHub Desktop.
study
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
flex: 1;
width: 300px;
height: 100%;
overflow-y: auto;
/* height:500px; */
height: 100%;
}
.items {
padding: 10px;
border-bottom: 1px solid #ccc;
}
body,
html {
height: 100%;
padding: 0;
margin: 0;
}
* {
box-sizing: border-box;
}
.yep {
border: 1px solid red;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
const box = document.querySelector(".box");
const params = {
page: 0,
size: 100,
};
let items = [];
const populate = () => {
const render = (i) => {
return `
<div class="items" id="${btoa(i._id)}">
<div>
${i.name}
</div>
</div>
`;
};
let html = items.map(render).join("");
return html;
};
const request = () => {
const _params = new URLSearchParams(params).toString();
fetch("https://api.instantwebtools.net/v1/passenger?" + _params)
.then((r) => r.json())
.then((r) => {
if (r.data.length) {
items.push(...r.data);
box.innerHTML = populate();
} else {
box.removeEventListener("scroll", onScroll);
}
});
};
box.addEventListener("scroll", onScroll);
function onScroll(event) {
const { scrollHeight, clientHeight, scrollTop } = this;
console.log({ scrollHeight, scrollTop, clientHeight });
if (scrollTop + clientHeight == scrollHeight) {
params.page += 1;
request();
}
}
request();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment