单击菜单项后关闭响应菜单

单击菜单项后关闭响应菜单

本文介绍了单击菜单项后关闭响应菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个用于基本(打开/关闭)菜单移动的自定义js代码,当我在多页网站上使用它时效果很好,但是只有在单击菜单符号时,它才会关闭菜单.现在,我需要将其实施到一页网站中,并且我需要在用户单击菜单项后将其关闭.我对javascript的了解很少,所以我需要帮助解决此问题.

I have this custom js code for basic (open/close) menu movement that was great when I used it on multi page websites, but it only closes the menu when you click the menu symbol. Now I need to implement it into a one page website and I need it to close after a user clicks on a menu item. I have very little experience in javascript so I need help solving this problem.

js:

$(document).ready(function() {
    var n = '#nav', no = 'nav-open';
    $('#nav-menu').click(function() {
        if ($(n).hasClass(no)) {
            $(n).animate({height:0},300);
            setTimeout(function() {
                $(n).removeClass(no).removeAttr('style');
            },320);
        }
        else {
            var newH = $(n).css('height','auto').height();
            $(n).height(0).animate({height:newH},300);
            setTimeout(function() {
                $(n).addClass(no).removeAttr('style');
            },320);
        }
    });
});

HTML:

<!-- Navigation Bar -->
<div class="nav-hold">
  <div class="nav-bar">
    <a href="#one" class="nav-logo"><img src="images/logo.png" alt="logo" /></a>
    <a href="#one" class="nav-logo-text">Company name</a>
    <a id="nav-menu" class="nav-menu-symbol">&#9776;<!-- menu symbol --></a>
    <a class="nav-menu">Menu</a>
    <ul class="nav-list" id="nav">
      <li><a href="#one">Top</a></li>
      <li><a href="#two">About us</a></li>
      <li><a href="#three">Services</a></li>
      <li><a href="#four">Portfolio</a></li>
      <li><a href="#five">Contact</a></li>
    </ul>
  </div>
</div>
</div>

推荐答案

更改

$('#nav-menu').click(function() {

如果您只希望通过单击li元素来关闭菜单

if you want that your menu close only by clicking on the li element

$('#nav li').click(function() {

或者如果您想同时关闭带有li和菜单图标的菜单

or if you want to close menu with both li and menu icon

$('#nav-menu, #nav li').click(function() {

这篇关于单击菜单项后关闭响应菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:47