Uncaught TypeError: Cannot read properties of undefined (reading 'getBoundingClientRect')
Делаю плавную прокрутку страницы, консоль выдает ошибку Uncaught TypeError: Cannot read properties of undefined (reading 'getBoundingClientRect'). Подскажите в чем проблема? Спасибо
const body = document.body,
jsScroll = document.getElementsByClassName('.page')[0],
height = jsScroll.getBoundingClientRect().height - 1,
speed = 0.05
var offset = 0
body.style.height = Math.floor(height) + "px"
function smoothScroll() {
offset += (window.scrollY - offset) * speed
var scroll = "translateY(-" + offset + "px) translateZ(0)"
jsScroll.style.transform = scroll
raf = requestAnimationFrame(smoothScroll)
}
smoothScroll()
Ответы (1 шт):
Автор решения: Алексей Шиманский
→ Ссылка
Скорее всего надо дождаться загрузки страницы и только после этого вызывать функцию клика. Для этого можно воспользоваться событием DOMContentLoaded
document.addEventListener("DOMContentLoaded", function(event) {
... тут код
});
т.к. изначально при вызове getBoundingClientRect на странице ещё не успел появится элемент .page