I wrote this code all on one page and it works perfectly. However, I am unsure how to unbind this event when transitioning to another page.
private mounted () {
if (process.browser) {
const banner:any = document.querySelector('.banner img')
document.addEventListener('scroll', () => {
const offsetTop = window.scrollY
const INTOR_HEIGHT = document.querySelector('.intro-text').offsetHeight
if (offsetTop < INTOR_HEIGHT) {
banner.style.top = offsetTop + 'px'
}
})
}
}
Whenever I navigate to a different page from this one, an error is thrown:
javascript Uncaught TypeError: Cannot read property 'offsetHeight' of null at HTMLDocument.eval