单击子链接时如何停止垂直菜单的手风琴。

时间:2015-02-13 10:32:27

标签: javascript jquery html css menu

我正在尝试使用手风琴效果构建垂直导航菜单。我几乎创建了菜单。但问题是我的所有链接都指向同一个本身。当我点击任何子导航链接时,页面将被重新加载,但父菜单正在折叠。我想在页面重新加载后显示活动的子链接。请建议我解决这个问题。

以下是Demo

HTML:

 <!doctype html>
    <html lang="en-US">
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
      <title>Vertical jQuery Accordion Nav Menu</title>
      <meta name="author" content="Jake Rocheleau">
      <link rel="shortcut icon" href="http://vandelaydesign.com/favicon.ico">
      <link rel="icon" href="http://vandelaydesign.com/favicon.ico">
      <link rel="stylesheet" type="text/css" href="styles.css">
      <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Merienda:400,700">
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
      <script type="text/javascript" language="javascript" charset="utf-8" src="nav.js"></script>
    <!--[if lt IE 9]>
      <script type="text/javascript" src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    </head>

    <body>
    <div id="w">
         <h1>CSS3/jQuery Accordion Nav Menu</h1>

        <nav>
            <ul id="nav">
                <li><a href="#">Animation</a>

                    <ul>
                        <li><a href="#">Cartoons</a>
                        </li>
                        <li><a href="#">Comic Strips</a>
                        </li>
                        <li><a href="#">Video Clips</a>
                        </li>
                        <li><a href="#">Web GIFs</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Graphic Design</a>

                    <ul>
                        <li><a href="#">Adobe Photoshop</a>
                        </li>
                        <li><a href="#">Branding &amp; Logos</a>
                        </li>
                        <li><a href="#">Digital Marketing</a>
                        </li>
                        <li><a href="#">Illustrations</a>
                        </li>
                        <li><a href="#">Infographics</a>
                        </li>
                        <li><a href="#">Product Design</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Digital Photography</a>

                    <ul>
                        <li><a href="http:/www.google.com/search?q=cityscape+photography">Cityscapes</a>
                        </li>
                        <li><a href="#">Oceans</a>
                        </li>
                        <li><a href="#">Wide-Angle Lens</a>
                        </li>
                        <li><a href="#">Wildlife</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Print &amp; Identity</a>

                    <ul>
                        <li><a href="#">Branding</a>
                        </li>
                        <li><a href="#">Business Cards</a>
                        </li>
                        <li><a href="#">Letterpress</a>
                        </li>
                        <li><a href="#">Poster Art</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Programming</a>

                    <ul>
                        <li><a href="#">CSS3</a>
                        </li>
                        <li><a href="#">HTML5</a>
                        </li>
                        <li><a href="#">JavaScript &amp; jQuery</a>
                        </li>
                        <li><a href="#">MySQL Databases</a>
                        </li>
                        <li><a href="#">Wordpress CMS</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Web Design</a>

                    <ul>
                        <li><a href="#">Icons</a>
                        </li>
#">Tutorials</a>
                        </li>
                        <li><a href="#">User Interfaces</a>
                        </li>
                        <li><a href="#">Website Layouts</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </nav>
    </div>
</body>
    </html>

CSS:

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
  outline: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
html { height: 101%; }
body { font-size: 62.5%; line-height: 1; padding-bottom: 65px; font-family: Arial, Tahoma, sans-serif; background: #c5bde5 url('images/bg.png'); }

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; }
ol, ul { list-style: none; }

blockquote, q { quotes: none; }
blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; }
strong { font-weight: bold; } 

table { border-collapse: collapse; border-spacing: 0; }
img { border: 0; max-width: 100%; }

h1 { font-family: 'Merienda', 'Trebuchet MS', Verdana, sans-serif; font-size: 2.95em; line-height: 1.7em; margin-bottom: 20px; font-weight: bold; letter-spacing: -0.03em; color: #675d90; text-shadow: 2px 2px 0px rgba(255,255,255,0.65); text-align: center; }


#w { display: block; width: 740px; margin: 0 auto; padding-top: 45px; }


/* nav menu styles */
#nav { 
  display: block; 
  width: 280px; 
  margin: 0 auto; 
  -webkit-box-shadow: 3px 2px 3px rgba(0,0,0,0.7);
  -moz-box-shadow: 3px 2px 3px rgba(0,0,0,0.7);
  box-shadow: 3px 2px 3px rgba(0,0,0,0.7);
}

#nav li { }

#nav > li > a { 
  display: block; 
  padding: 16px 18px;
  font-size: 1.3em;
  font-weight: bold;
  color: #d4d4d4;
  text-decoration: none;
  border-bottom: 1px solid #212121;
  background-color: #343434;
  background: -webkit-gradient(linear, left top, left bottom, from(#343434), to(#292929));
  background: -webkit-linear-gradient(top, #343434, #292929);
  background: -moz-linear-gradient(top, #343434, #292929);
  background: -ms-linear-gradient(top, #343434, #292929);
  background: -o-linear-gradient(top, #343434, #292929);
  background: linear-gradient(top, #343434, #292929);
}
#nav > li > a:hover, #nav > li > a.open { 
  color: #e9e9e9;
  border-bottom-color: #384f76;
  background-color: #6985b5;
  background: -webkit-gradient(linear, left top, left bottom, from(#6985b5), to(#456397));
  background: -webkit-linear-gradient(top, #6985b5, #456397);
  background: -moz-linear-gradient(top, #6985b5, #456397);
  background: -ms-linear-gradient(top, #6985b5, #456397);
  background: -o-linear-gradient(top, #6985b5, #456397);
  background: linear-gradient(top, #6985b5, #456397);
}

#nav li ul { display: none; background: #4a5b78; }

#nav li ul li a { 
  display: block; 
  background: none;
  padding: 10px 0px;
  padding-left: 30px;
  font-size: 1.1em;
  text-decoration: none;
  font-weight: bold;
  color: #e3e7f1;
  text-shadow: 1px 1px 0px #000;
}
#nav li ul li a:hover {
  background: #394963;
}

Java脚本:

$(document).ready(function(){
  $("#nav > li > a").on("click", function(e){
    if($(this).parent().has("ul")) {
      e.preventDefault();
    }

    if(!$(this).hasClass("open")) {
      // hide any open menus and remove all other classes
      $("#nav li ul").slideUp(350);
      $("#nav li a").removeClass("open");

      // open our new menu and add the open class
      $(this).next("ul").slideDown(350);
      $(this).addClass("open");
    }

    else if($(this).hasClass("open")) {
      $(this).removeClass("open");
      $(this).next("ul").slideUp(350);
    }
  });
});

1 个答案:

答案 0 :(得分:1)

首先,链接会将用户引导至Google搜索页面。如果这些只是占位符链接,并且您打算在自己的网站中指导用户,那么您可以使用许多选项。你有没有想过使用一种AJAX技术,它允许在没有页面加载的情况下加载内容? Angular JS是一个很棒的框架,可用于快速创建允许与服务器进行异步交互的单页应用程序。

如果您需要重新加载页面,则可以再次使用多个选项。一个选项是将请求中的参数传递给服务器,您可以使用它以编程方式设置菜单的状态。