When I add
<!DOCTYPE html>
to the top of my html file, my linked script.js stops working. However, it works fine without the doctype tag. It should add a class to my nav when i scroll down and remove it when i scroll back up.
Here is my javascript:
var myNav = document.getElementById('header');
window.onscroll = function () {
"use strict";
if (document.body.scrollTop >= 50 ) {
myNav.classList.add("nav-colored");
myNav.classList.remove("nav-transparent");
}
else {
myNav.classList.add("nav-transparent");
myNav.classList.remove("nav-colored");
}
};
Any help is appreciated. Thanks!
The error is because document.body.scrollTop
in HTML 5 is deprecated instead use document.documentElement.scrollTop
var myNav = document.getElementById('header');
window.onscroll = function () {
'use strict';
if (document.documentElement.scrollTop >= 50) {
myNav.classList.add('nav-colored');
myNav.classList.remove('nav-transparent');
} else {
myNav.classList.add('nav-transparent');
myNav.classList.remove('nav-colored');
}
};
more information document.body.scrollTop Firefox returns 0 : ONLY JS
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.