Я использую HTML, CSS и JavaScript для создания веб -страницы с липкой и отзывчивой навигационной панелью. Я создал отзывчивую навигационную панель и стараюсь сделать ее липким. Проблема в том, что это не липкое и показывает ошибку:
unactught typeerror: не может прочитать свойство 'OffsetTop' of null < /p>
html -код: < /p>
Home
Careers
About Us
[url=javascript:void(0);]
[/url]
< /code>
код Javascript: < /p>
// When the user scrolls the page, execute myFunction
window.onscroll = function() {myFunctionForSticky()};
// Get the navbar
var navbar = document.getElementById("myTopnav");
// Get the offset position of the navbar
var sticky = navbar.offsetTop;
// Add the sticky class to the navbar when you reach its scroll position.
Remove "sticky" when you leave the scroll position
function myFunctionForSticky() {
if(window.pageYOffset >= sticky){
console.log("window.pageYOffset >= sticky");
}
else{
console.log("Not window.pageYOffset >= sticky");
}
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky");
} else {
navbar.classList.remove("sticky");
}
}
/*Toggle between adding and removing the "responsive" class to topnav
when the user clicks on the icon*/
function myFunctionForResponsive() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
< /code>
Если я беру класс вместо идентификатора, то он показывает ошибку:
unaught typeerror: не может прочитать свойство 'удалить' не определенного < /p>
Подробнее здесь: https://stackoverflow.com/questions/525 ... op-of-null