Created
August 12, 2024 06:18
-
-
Save o-az/24e464e97fcc00f551f999bdb0e0957f to your computer and use it in GitHub Desktop.
less loc than https://github.com/jbanes/versus-js/blob/main/jQuery/index.html. Reference https://x.com/classicgamertwr/status/1822691525463347442
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>ES6 Demonstration</title> | |
<style> | |
.pager { margin: 5px 10px; user-select: none; font-family: sans-serif; } | |
.page { display: inline-block; padding: 0 5px; cursor: pointer; } | |
.page:active, .selected { color: red; } | |
.selected { font-weight: bold; } | |
#content { font: bold 250% sans-serif; padding: 25px 10px; } | |
</style> | |
<script> | |
const content = { page: 1, pages: 5 }; | |
const renderContent = () => { | |
document.getElementById('content').innerHTML = `Page ${content.page}`; | |
}; | |
const renderPager = () => { | |
const pagerHTML = 'Page: ' + Array(content.pages).fill() | |
.map((_, i) => `<span class="page${i + 1 === content.page ? ' selected' : ''}">${i + 1}</span>`) | |
.join(''); | |
document.querySelectorAll('.pager').forEach(pager => pager.innerHTML = pagerHTML); | |
}; | |
document.addEventListener('DOMContentLoaded', () => { | |
renderContent(); | |
renderPager(); | |
document.body.addEventListener('click', e => { | |
if (e.target.classList.contains('page')) { | |
content.page = parseInt(e.target.textContent); | |
renderContent(); | |
renderPager(); | |
} | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div class="pager"></div> | |
<div id="content"></div> | |
<div class="pager"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment