0
votes

I'm designing a one-page scrolling website. When I click on a page link from the nav bar at screen widths less than 780px (when the hamburger icon appears), the hamburger icon disappears. I can't get it back unless I refresh the page. The nav bar also disappears at full screen width after clicking on a page link once. I would like to keep the hamburger icon and the top navigation in view at all times. The javascript I'm using to collapse the full-screen menu that shows up at 780px is causing this problem, but I need it, or the menu will not disappear when a link is clicked. Thank you to anyone who can help!

$(document).ready(function() {
    $('a').click(function() {
        $('#menu').slideToggle();
    });
});
@media screen and (max-width: 780px) {

    nav {
        width: 100%;
        margin: 0;
        padding: 0;
        position: relative;
        left: 0;
        display: block;
        opacity: 1.0 !important;
        filter: alpha(opacity=100); /* For IE8 and earlier */
    }

    nav ul {
        width: 100%;
        margin: 0 auto;
        padding: 0;
        display: none;
        float: none;
    }

    nav ul li {
        font-size: 1.3em;
        font-weight: normal;
        line-height: 40px;
        width: 100% !important;
        margin: 0;
        padding: 0;
    }

    nav ul li:nth-of-type(1) { margin-top: 20%; }

    nav ul li:hover { background: #565758; }
    
    nav ul li a {
        color: white !important;
        font-family: "Lato", sans-serif;
        border-bottom: none !important;
        display: inline-block;
    }

    nav ul li a.active-link {
        color: white !important;
        font-size: 1.3em;
    }

    nav ul li a:hover {
        color: white;
        width: 100%;
    }
        
    /*Display 'show menu' link*/
    .show-menu {
        margin: 0 !important;
        padding: 1em !important;
        text-align: right;
        display: block;
        float: right;
    }

    /*Show menu when invisible checkbox is checked*/
    input[type=checkbox]:checked ~ #menu { background-color: #747475 !important; display: block; height: 100vh; }
}​
<header>
    <nav>
        <label for="show-menu" class="show-menu"><img src="hamburger.png" alt="Hamburger Menu Icon" style="width: 15%;"></label>
        <input type="checkbox" id="show-menu" role="button">
        <ul id="menu">
            <li><a href="#choco">HOME</a>
            <li><a href="#about-page">ABOUT</a></li>
            <li><a href="#portfolio-page">PORTFOLIO</a></li>
            <li><a href="#contact.html">CONTACT</a></li>
        </ul>
    </nav>
    <div id="logo"><img src="logo-grey.png" alt="Logo" style="max-width:100%; height:auto;"></div>
</header>​
1

1 Answers

0
votes

You need to add the toggle to the checkbox as well. It is an jQuery function, that uses specific animations and styles.

$('#show-menu').click(function() {
    $('#menu').slideToggle();
});

EDIT


I added a working example. I did not used the toggle here, for a better design. Now the menu also toggels with the click on the checkbox :-)

$(document).ready(function() {
    $('a').click(function() {
        $('#menu').slideToggle();
    });
  $('#show-menu').change(function() {
      if(this.checked)
        $('#menu').slideDown();
      else
        $('#menu').slideUp();
  });
});
@media screen and (max-width: 780px) {

    nav {
        width: 100%;
        margin: 0;
        padding: 0;
        position: relative;
        left: 0;
        display: block;
        opacity: 1.0 !important;
        filter: alpha(opacity=100); /* For IE8 and earlier */
    }

    nav ul {
        width: 100%;
        margin: 0 auto;
        padding: 0;
        display: none;
        float: none;
    }

    nav ul li {
        font-size: 1.3em;
        font-weight: normal;
        line-height: 40px;
        width: 100% !important;
        margin: 0;
        padding: 0;
    }

    nav ul li:nth-of-type(1) { margin-top: 20%; }

    nav ul li:hover { background: #565758; }
    
    nav ul li a {
        color: white !important;
        font-family: "Lato", sans-serif;
        border-bottom: none !important;
        display: inline-block;
    }

    nav ul li a.active-link {
        color: white !important;
        font-size: 1.3em;
    }

    nav ul li a:hover {
        color: white;
        width: 100%;
    }
        
    /*Display 'show menu' link*/
    .show-menu {
        margin: 0 !important;
        padding: 1em !important;
        text-align: right;
        display: block;
        float: right;
    }

    /*Show menu when invisible checkbox is checked*/
    input[type=checkbox]:checked ~ #menu { background-color: #747475 !important; display: block; height: 100vh; }
}​
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
    <nav>
        <label for="show-menu" class="show-menu"><img src="hamburger.png" alt="Hamburger Menu Icon" style="width: 15%;"></label>
        <input type="checkbox" id="show-menu" role="button">
        <ul id="menu">
            <li><a href="#choco">HOME</a>
            <li><a href="#about-page">ABOUT</a></li>
            <li><a href="#portfolio-page">PORTFOLIO</a></li>
            <li><a href="#contact.html">CONTACT</a></li>
        </ul>
    </nav>
    <div id="logo"><img src="logo-grey.png" alt="Logo" style="max-width:100%; height:auto;"></div>
</header>​