0

Делаю плавную прокрутку страницы, консоль выдает ошибку 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 ответ 1

1

Скорее всего надо дождаться загрузки страницы и только после этого вызывать функцию клика. Для этого можно воспользоваться событием DOMContentLoaded

document.addEventListener("DOMContentLoaded", function(event) {
    ... тут код
});

т.к. изначально при вызове getBoundingClientRect на странице ещё не успел появится элемент .page

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.