首页 文章

单击页面链接一次,导航/汉堡包图标消失

提问于
浏览
0

我正在设计一个单页滚动网站 . 当我在屏幕宽度小于780px的情况下点击导航栏中的页面链接时(当汉堡包图标出现时),汉堡包图标消失 . 除非我刷新页面,否则我无法取回它 . 单击页面链接一次后,导航栏也会以全屏宽度消失 . 我想随时查看汉堡图标和顶部导航 . 我用来折叠显示在780px的全屏菜单的javascript导致了这个问题,但我需要它,或者点击链接时菜单不会消失 . 感谢任何可以提供帮助的人!

$(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 回答

  • 0

    您还需要将切换添加到复选框 . 它是一个jQuery函数,它使用特定的动画和样式 .

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

    EDIT


    我添加了一个工作示例 . 我没有在这里使用切换,以获得更好的设计 . 现在菜单也点击了复选框:-)

    $(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>​
    

相关问题