Last active
August 14, 2020 12:11
-
-
Save sgpinkus/8e90360ea9e13349806835ec77b020c8 to your computer and use it in GitHub Desktop.
Example of how "infinite scroll" works out of the box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Simplified Infinite Scroll</title> | |
<meta charset="utf-8" /> | |
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> | |
<style type='text/css'> | |
* { border: 0; padding: 0; margin: 0; box-sizing: border-box; } | |
html, body { height: 100%; } | |
body { display: flex; flex-flow: column wrap; } | |
header, footer { background-color: #888; max-height: 40px; min-height: 40px; flex: 1 } | |
header, footer { text-align: center; } | |
#message-scroll-container { | |
flex: 1; | |
margin: 0 auto; | |
border: solid 1px #000; | |
width: 400px; | |
overflow-y: scroll; | |
} | |
#message-container { min-height: 101%; } | |
.message-block { border: solid 1px #000; } | |
.loader { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); visibility: hidden; } | |
</style> | |
<script> | |
let bounds = { top: 0, bottom: 0 }; | |
let messageId = 0; | |
jQuery(document).ready(function() { | |
console.log("Ready"); | |
addMessages(5); | |
bounds['bottom'] = 5; | |
let scrollContainer = jQuery('#message-scroll-container') | |
scrollContainer.scrollTop = scrollContainer.scrollTopMax / 2; | |
scrollContainer.on('scroll', scrollHandler); | |
}) | |
function scrollHandler(e, f) { | |
console.log(this.scrollTop, this.scrollTopMax); | |
let edge = undefined; | |
if(this.scrollTop === this.scrollTopMax) { | |
edge = 'bottom'; | |
} | |
else if(this.scrollTop === 0) { | |
edge = 'top'; | |
} | |
if(edge) { | |
let scrollTopMax = this.scrollTopMax | |
console.log(`Scoll is at ${edge}. Loading more.`) | |
new Promise(resolve => { | |
jQuery('.loader').css('visibility', 'visible'); | |
setTimeout(() => { | |
if(bounds[edge] < 15) { | |
addMessages(5, edge); | |
bounds[edge] += 5; | |
diff = this.scrollTopMax - scrollTopMax; | |
this.scrollTop += (edge === 'bottom' ? 0 : diff); | |
} | |
else { | |
console.log('No more messages'); | |
this.scrollTop += (edge === 'bottom' ? -20 : 20); | |
} | |
resolve(); | |
}, 700); | |
}) | |
.finally(() => jQuery('.loader').css('visibility', 'hidden')); | |
} | |
} | |
function addMessages(n = 2, edge = 'bottom') { | |
let messages = jQuery('#message-scroll-container > div'); | |
for(let i = 1; i <= n; i++) { | |
id = (bounds[edge] + i) * (edge === 'bottom' ? 1 : -1); | |
let message = `<div class="message-block"><p><strong>${id}</strong> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p><br /></div>`; | |
if(edge === 'bottom') { | |
messages.append(message); | |
} else if(edge === 'top') { | |
messages.prepend(message); | |
} | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<img class='loader', src='data:image/png;base64,R0lGODlhgAAPAJEAAAAAALOzs////wAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQECgAAACwAAAAAgAAPAAACo4wfooK33NKKUtF3Z8RbN/55CEiNonMaJGp1bfiaMQvBtXzTpZuradUDZmY+opA3DK6KwaQTCbU9pVHc1LrDUrfarq765Ya9u+VRzLyO12lwG10yy39zY11Jz9t/6jf5/HfXB8hGWKaHt6eYyDgo6BaH6CgJ+QhnmWWoiVnI6ddJmbkZGkgKujhplNpYafr5OooqGst66Uq7OpjbKmvbW/p7UAAAIfkEBAoAAAAsAAAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACwLAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALBYAAAAHAA8AAAIIhI+py+0PoywAIfkEBAoAAAAsIQAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACwsAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALDcAAAAHAA8AAAIIhI+py+0PoywAIfkEBAoAAAAsQgAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACxNAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALFgAAAAHAA8AAAIIhI+py+0PoywAIfkEBAoAAAAsYwAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACxuAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALG4AAAASAA8AAAIojB+iAu2AzmKvRTOrvVnftGgQF3qkIn5dpZbsSb3R+rSoObs1nPJaAQA7' alt='Loading...'> | |
<header> | |
<p>Header</p> | |
</header> | |
<div id='message-scroll-container'> | |
<h2>Messages</h2> | |
<div id='message-container'></div> | |
</div> | |
<footer> | |
<p>Footer</p> | |
</footer> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Simplified Infinite Scroll</title> | |
<meta charset="utf-8" /> | |
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> | |
<style type='text/css'> | |
* { border: 0; padding: 0; margin: 0; box-sizing: border-box; } | |
html, body { height: 100%; } | |
body { display: flex; flex-flow: column wrap; } | |
header, footer { background-color: #888; max-height: 40px; min-height: 40px; flex: 1 } | |
header, footer { text-align: center; } | |
#message-scroll-container { | |
flex: 1; | |
margin: 0 auto; | |
border: solid 1px #000; | |
width: 400px; | |
overflow-y: scroll; | |
} | |
#message-container { min-height: 101%; } | |
.message-block { border: solid 1px #000; } | |
.loader { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); visibility: hidden; } | |
</style> | |
<script> | |
jQuery(document).ready(function() { | |
console.log("Ready"); | |
addMessages(1); | |
jQuery('#message-scroll-container')[0].scrollTop = 0; | |
jQuery('#message-scroll-container').on('scroll', scrollHandler ); | |
}) | |
function scrollHandler(e, f) { | |
console.log(this.scrollTop, this.scrollTopMax); | |
if(this.scrollTop === this.scrollTopMax) { | |
const messageCount = jQuery('.message-block').length; | |
console.log(`${messageCount} of 15`); | |
new Promise(resolve => { | |
jQuery('.loader').css('visibility', 'visible'); | |
setTimeout(() => { | |
if(messageCount > 15) this.scrollTop -= 20; | |
else addMessages(5); | |
resolve(); | |
}, 700); | |
}) | |
.finally(() => jQuery('.loader').css('visibility', 'hidden')); | |
} | |
} | |
function addMessages(n = 2) { | |
for(let i = 0; i < n; i++) { | |
jQuery('#message-scroll-container > div').append(`<div class="message-block"><p><strong></strong> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p><br /> | |
</div>`); | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<img class='loader', src='data:image/png;base64,R0lGODlhgAAPAJEAAAAAALOzs////wAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQECgAAACwAAAAAgAAPAAACo4wfooK33NKKUtF3Z8RbN/55CEiNonMaJGp1bfiaMQvBtXzTpZuradUDZmY+opA3DK6KwaQTCbU9pVHc1LrDUrfarq765Ya9u+VRzLyO12lwG10yy39zY11Jz9t/6jf5/HfXB8hGWKaHt6eYyDgo6BaH6CgJ+QhnmWWoiVnI6ddJmbkZGkgKujhplNpYafr5OooqGst66Uq7OpjbKmvbW/p7UAAAIfkEBAoAAAAsAAAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACwLAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALBYAAAAHAA8AAAIIhI+py+0PoywAIfkEBAoAAAAsIQAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACwsAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALDcAAAAHAA8AAAIIhI+py+0PoywAIfkEBAoAAAAsQgAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACxNAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALFgAAAAHAA8AAAIIhI+py+0PoywAIfkEBAoAAAAsYwAAAAcADwAAAgiEj6nL7Q+jLAAh+QQECgAAACxuAAAABwAPAAACCISPqcvtD6MsACH5BAQKAAAALG4AAAASAA8AAAIojB+iAu2AzmKvRTOrvVnftGgQF3qkIn5dpZbsSb3R+rSoObs1nPJaAQA7' alt='Loading...'> | |
<header> | |
<p>Header</p> | |
</header> | |
<div id='message-scroll-container'> | |
<h2>Messages</h2> | |
<div id='message-container'></div> | |
</div> | |
<footer> | |
<p>Footer</p> | |
</footer> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment