0

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!

bongerama
  • 3
  • 1

1 Answers1

1

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

Raxel21
  • 407
  • 2
  • 9