Some small fixes and optimizations.

This commit is contained in:
Bob Vandevliet 2024-07-13 18:09:28 +02:00
parent 39c2c3d8c4
commit 8c2cfa180d
5 changed files with 121 additions and 106 deletions

View file

@ -1,53 +1,56 @@
/**
* @type {NodeListOf<HTMLVideoElement>}
*/
const parallaxElems = document.querySelectorAll('.parallax');
// Handle scroll events.
window.addEventListener('scroll', () =>
document.addEventListener('DOMContentLoaded', () =>
{
/**
* @type {NodeListOf<HTMLVideoElement>}
*/
const parallaxElems = document.querySelectorAll('.parallax');
// Handle scroll events.
window.addEventListener('scroll', () =>
{
// Add class to body when scrolled.
document.body.classList.toggle('is-scrolled', window.scrollY > 0);
document.body.classList.toggle('is-scrolled', window.scrollY > 0);
// Parallax effect.
parallaxElems.forEach(parallaxElem =>
{
const parentElem = parallaxElem.parentElement;
// Parallax effect.
parallaxElems.forEach(parallaxElem =>
{
const parentElem = parallaxElem.parentElement;
const parentRect = parentElem.getBoundingClientRect();
const parentScroll = parentRect.top + (parentRect.height / 2);
const parentRect = parentElem.getBoundingClientRect();
const parentScroll = parentRect.top + (parentRect.height / 2);
const windowScroll = window.scrollY + (window.innerHeight / 2);
const windowScroll = window.scrollY + (window.innerHeight / 2);
parallaxElem.style.top = `${(windowScroll - parentScroll) / 4}px`;
parallaxElem.style.top = `${(windowScroll - parentScroll) / 4}px`;
});
});
});
/**
* @type {NodeListOf<HTMLElement>}
*/
const sections = document.querySelectorAll('.section');
/**
* @type {NodeListOf<HTMLElement>}
*/
const sections = document.querySelectorAll('.section');
sections.forEach(section =>
{
if (section.id)
sections.forEach(section =>
{
const anchor = document.createElement('a');
anchor.className = 'anchor-link';
anchor.href = `#${section.id}`;
anchor.innerHTML = ' #&nbsp;';
section.appendChild(anchor);
}
});
if (section.id)
{
const anchor = document.createElement('a');
anchor.className = 'anchor-link';
anchor.href = `#${section.id}`;
anchor.innerHTML = ' #&nbsp;';
section.appendChild(anchor);
}
});
document.querySelectorAll('#navbarNav li.nav-item>a').forEach(anchor =>
{
anchor.addEventListener('click', () =>
document.querySelectorAll('#navbarNav li.nav-item>a').forEach(anchor =>
{
/**
* @type {HTMLButtonElement}
*/
const navbarNav = document.querySelector('button.navbar-toggler[aria-expanded="true"]');
navbarNav?.click();
anchor.addEventListener('click', () =>
{
/**
* @type {HTMLButtonElement}
*/
const navbarNav = document.querySelector('button.navbar-toggler[aria-expanded="true"]');
navbarNav?.click();
});
});
});