In my website, I’ve a sticky container with a horizontal scroll direction.
The scroll works perfectly. However the issue is, when I tried to get a value from a CSS variable and assign it to Javascript I get this following error:
ReferenceError: Cannot access uninitialized variable.
const spaceHolder = document.querySelector('.js-scroll-wrapper');
const horizontal = document.querySelector('.js-scroll-direction');
spaceHolder.style.height = `${calcDynamicHeight(horizontal)}px`;
// Get existing CSS variable '--padding-right' of '.container'
let el = document.querySelector('.container');
let gcs = getComputedStyle(el);
let paddingRight = gcs.getPropertyValue('--padding-right');
// alert('The value of the css variable is : ' + (paddingRight?paddingRight:'undefined'));
function calcDynamicHeight(ref) {
const vw = window.innerWidth;
const vh = window.innerHeight;
const objectWidth = ref.scrollWidth;
return objectWidth - vw + vh + paddingRight; // '--padding-right' CSS value of '.container'
}
window.addEventListener('scroll', () => {
const sticky = document.querySelector('.js-h-scroll-sticky');
horizontal.style.transform = `translateX(-${sticky.offsetTop}px)`;
});
window.addEventListener('resize', () => {
spaceHolder.style.height = `${calcDynamicHeight(horizontal)}px`;
});
.container {
--padding-right: 100;
position: relative;
width: 100%;
height: 100%;
padding: 0 0 0 var(--padding-right)px;
}

