mirror of
https://github.com/Radiquum/radiquum.github.io.git
synced 2025-05-07 21:39:34 +05:00
refactor: better header scroll handling
This commit is contained in:
parent
6c0f477436
commit
2c9fb22de2
3 changed files with 39 additions and 25 deletions
|
@ -7,8 +7,8 @@ const IMAGE_ANIMATION_DELAY = 1000;
|
|||
const IMAGE_TRANSITION_DELAY = 1000;
|
||||
|
||||
// Header percentages
|
||||
const HEADER_ACTIVATION_PERCENT = 30;
|
||||
const HEADER_DEACTIVATION_PERCENT = 20;
|
||||
const HEADER_ACTIVATION_PERCENT = 0.4;
|
||||
const HEADER_DEACTIVATION_PERCENT = 0.8;
|
||||
|
||||
// THIS IS NOT CONSTANTS, BUT A GLOBAL VAR FOR TIMERS IN SONG NAME CHANGE!!!
|
||||
let DELETE_INTERVAL = null;
|
||||
|
@ -95,36 +95,24 @@ async function updatePlayingTrack() {
|
|||
});
|
||||
}
|
||||
|
||||
function getBlockScrollPosition(block_id) {
|
||||
const blockHeight = document.getElementById(block_id).clientHeight;
|
||||
const fullPageHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
|
||||
const restPageHeight = fullPageHeight - blockHeight;
|
||||
const windowScroll = document.documentElement.scrollTop - restPageHeight;
|
||||
const scrolled = (windowScroll / fullPageHeight) * 100;
|
||||
if (0 < Math.floor(scrolled) ) {
|
||||
return 0
|
||||
} else {
|
||||
return Math.floor(scrolled) * -1;
|
||||
}
|
||||
}
|
||||
|
||||
const header = document.getElementById("header");
|
||||
let last_Y_pos = 0;
|
||||
let header_opacity = 0;
|
||||
|
||||
window.onscroll = () => {
|
||||
let scrollPosition = getBlockScrollPosition('section_landing');
|
||||
if (scrollPosition > HEADER_ACTIVATION_PERCENT) {
|
||||
let landingBlock = document.getElementById("section_landing").clientHeight;
|
||||
|
||||
if (window.scrollY < (landingBlock * HEADER_ACTIVATION_PERCENT)) {
|
||||
header.style.display = "none";
|
||||
} else {
|
||||
header.style.display = "block";
|
||||
}
|
||||
|
||||
if (window.scrollY > last_Y_pos && scrollPosition < HEADER_ACTIVATION_PERCENT) {
|
||||
if (window.scrollY > last_Y_pos && window.scrollY > (landingBlock * HEADER_ACTIVATION_PERCENT)) {
|
||||
header_opacity += 0.1;
|
||||
} else if (window.scrollY < last_Y_pos && scrollPosition > HEADER_DEACTIVATION_PERCENT)
|
||||
} else if (window.scrollY < last_Y_pos && window.scrollY < (landingBlock * HEADER_DEACTIVATION_PERCENT)) {
|
||||
header_opacity -= 0.1;
|
||||
last_Y_pos = window.scrollY;
|
||||
}
|
||||
|
||||
if (header_opacity > 1) {
|
||||
header_opacity = 1;
|
||||
|
@ -132,6 +120,8 @@ window.onscroll = () => {
|
|||
header_opacity = 0;
|
||||
}
|
||||
|
||||
last_Y_pos = window.scrollY;
|
||||
|
||||
header.style.setProperty("--header-opacity", `${header_opacity.toFixed(2)}`);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue