feat: add scroll indicator in blog posts (#249)

This commit is contained in:
Sat Naing
2024-02-09 11:43:51 +06:30
committed by GitHub
parent 556df2d21a
commit 8e022c7b5a
+44
View File
@@ -48,6 +48,11 @@ const layoutProps = {
<Layout {...layoutProps}>
<Header />
<!-- <div class="progress-container fixed top-0 z-10 h-1 w-full bg-skin-fill">
<div class="progress-bar h-1 w-0 bg-skin-accent" id="myBar"></div>
</div> -->
<div class="mx-auto flex w-full max-w-3xl justify-start px-2">
<button
class="focus-outline mb-2 mt-8 flex hover:opacity-75"
@@ -107,6 +112,45 @@ const layoutProps = {
</style>
<script is:inline>
/** Create a progress indicator
* at the top */
function createProgressBar() {
// Create the main container div
const progressContainer = document.createElement("div");
progressContainer.className =
"progress-container fixed top-0 z-10 h-1 w-full bg-skin-fill";
// Create the progress bar div
const progressBar = document.createElement("div");
progressBar.className = "progress-bar h-1 w-0 bg-skin-accent";
progressBar.id = "myBar";
// Append the progress bar to the progress container
progressContainer.appendChild(progressBar);
// Append the progress container to the document body or any other desired parent element
document.body.appendChild(progressContainer);
}
createProgressBar();
/** Update the progress bar
* when user scrolls */
function updateScrollProgress() {
const winScroll =
document.body.scrollTop || document.documentElement.scrollTop;
const height =
document.documentElement.scrollHeight -
document.documentElement.clientHeight;
const scrolled = (winScroll / height) * 100;
if (document) {
const myBar = document.getElementById("myBar");
if (myBar) {
myBar.style.width = scrolled + "%";
}
}
}
document.addEventListener("scroll", updateScrollProgress);
/** Attaches links to headings in the document,
* allowing sharing of sections easily */
function addHeadingLinks() {