I'm building a Wordpress site with Ajaxify
to create smooth transition between pages.
In my custom template page-about.php
I have a JS script to create a sticky horizontal scroll. The script works fine. But when I change page by navigating on the AJAX
site. It releases the following error:
TypeError: null is not an object (evaluating 'sticky.offsetTop')
Here is the JS script:
if ($('.wrapper-horizontal').length) {
const spaceHolder = document.querySelector('.space-holder');
const horizontal = document.querySelector('.horizontal');
spaceHolder.style.height = `${calcDynamicHeight(horizontal)}px`;
function calcDynamicHeight(ref) {
const vw = window.innerWidth;
const vh = window.innerHeight;
const objectWidth = ref.scrollWidth;
return objectWidth - vw + vh + 150;
}
window.addEventListener('scroll', () => {
const sticky = document.querySelector('.is-sticky-h');
horizontal.style.transform = `translateX(-${sticky.offsetTop}px)`;
});
window.addEventListener('resize', () => {
spaceHolder.style.height = `${calcDynamicHeight(horizontal)}px`;
});
//# sourceURL=pen.js
}
I tried this solution but it doesn't work in my case:
document.addEventListener('readystatechange', function() {
if (document.readyState === "complete") {
// Do something
}
});
Any help would be much appreciated. Thanks!