Created
April 1, 2023 07:27
-
-
Save crazy4groovy/14ec9242f51bc296f274d0d4eb6fb513 to your computer and use it in GitHub Desktop.
Put the Scroll Percentage in the Browser Title Bar Dynamically (JavaScript)
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
const percentLabel = document.querySelector("#percent"); | |
const originalTitle = document.title; | |
window.addEventListener("scroll", () => { | |
let scrollTop = window.scrollY; | |
let docHeight = document.body.offsetHeight; | |
let winHeight = window.innerHeight; | |
let scrollPercent = scrollTop / (docHeight - winHeight); | |
let scrollPercentRounded = Math.round(scrollPercent * 100); | |
percentLabel.innerHTML = scrollPercentRounded; | |
document.title = `(${scrollPercentRounded}%) ${originalTitle}`; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
credit: https://css-tricks.com/how-i-put-the-scroll-percentage-in-the-browser-title-bar/