Solution 1 :

You probably need something like this.
The trick is to add event listener for resize event, because otherwise your function will be called just once.

function toggle_visibility() {
  var screen_width = window.innerWidth;
  if (screen_width <= 1000) {
    // Screen is smaller than (or equal to) 1000px
    var e = document.getElementById('scroll-nav-down');
    if ( == 'block') { = 'none';
    } else { = 'block';
  } else {
    // do something if it's larger = 'none';
 document.addEventListner('resize', toggle_visibility);

Problem :

I am trying to add an additional menu bar underneath the existing header in a WordPress theme. I implemented my menu, also using the header tag, by inserting raw HTML code (some themes allow you to do so). But it should only be visible when the viewport width is less than 1000px.

I would love to do this with a media query, but the problem is that both my menu, as well as the existing menu use the header tag, so my CSS rule would hide both menus below a viewport width of 1000px.

Anyway, here’s the code I was working with. Does anyone notice anything, why this isn’t working?

function toggle_visibility(.scroll-nav-down) {
  var screen_width = window.innerWidth;
  if (screen_width <= 1000) {
    // Screen is smaller than (or equal to) 1000px
    var e = document.getElementById(.scroll-nav-down);
    if ( == 'block') { = 'none';
    } else { = 'block';
  } else {
    // do something if it's larger = 'none';

Edit: I’m really struggeling getting back to working with css. I’m trying to use the Media Query solution, but I’m facing problems. I guess it is the instead of only . But if I remove the and change it to , I’m messing up the JavaScript a friend wrote me some time ago. And yes, I’m not into JavaScript at all. So here’s the whole code, maybe it helps solving my problem…
-> I added the part only for the image, to demonstrate the effect.

Note: If you change the css for the “#headerdiv” to a simple “header” and in the last JavaScript line the ‘headerdiv’ to a simple ‘header’, the code is working as it should. But not disappearing under a 1000px widht.

<!DOCTYPE html>
body {
    padding-top: 40px;
#headerdiv {
  height: 40px;
  position: fixed;
  top: 0;
  transition: top 0.2s ease-in-out;
  width: 100%;
.scroll-nav-up {
  top: -40px;
@media (min-width: 1000px) {
  #headerdiv {
main {
  background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAPklEQVQYV2O8dOnSfwYg0NPTYwTRuAAj0QqxmYBNM1briFaIzRbi3UiRZ75uNgUHGbfvabgfsHqGaIXYPAMAD8wgC/DOrZ4AAAAASUVORK5CYII=
) repeat;
  height: 2000px;
<script src=""></script>
var ScrollNav = function(elem, delta) {
    this.m_Delta = delta;
    this.m_LastScrollTop = 0;
    this.m_Element = elem;
    $(window).on("scroll", null, {ScrollNav: this}, this.OnWindowScroll);

ScrollNav.prototype.OnWindowScroll = function(e) {

    var scrolledTop = $(window).scrollTop();
    var Nav =;

    if(Math.abs(Nav.m_LastScrollTop - scrolledTop) <= Nav.m_Delta) {

    if(scrolledTop > Nav.m_LastScrollTop && scrolledTop > $(Nav.m_Element).outerHeight()) {
    } else if(scrolledTop + $(window).height() < $(document).height()) {

    Nav.m_LastScrollTop = scrolledTop;
var scroll = null;
$(document).ready(function() {
    // new ScrollNav('ZielElement', Delta);
    scroll = new ScrollNav('headerdiv', 5);

  <header id="headerdiv" class="scroll-nav-down">
    This is your menu.
    This is your body.


Comment posted by domsson

When you say

Comment posted by mplungjan


Comment posted by Mehdi Dehghani

Why JavaScript? it could be done by CSS

Comment posted by AndrewL64

Comment posted by evolutionxbox

Consider using a media query listener to run JS at or below/above a certain width