Home  >  Article  >  Web Front-end  >  Code to implement folding menu using html, css and js

Code to implement folding menu using html, css and js

不言
不言Original
2018-08-09 17:08:262110browse

The content of this article is about the code for implementing the folding menu by combining HTML, CSS and JS. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Apply the template, put the relevant information of the menu directly in the script data, and use a loop to generate

<script id="templateNavBar" type="text/html">
    <p class="nav-bar-logo">

    </p>
    {{each $data as item i}}
        <p class="nav-item {{item.class}}">{{item.name}}</p>
        {{if item.child != null}}
            <p class="childgroup">
                {{each item.child as child i}}
                    <p class="nav-item {{child.class}} child">{{child.name}}</p>
                {{/each}}
            </p>
        {{/if}}
    {{/each}}
</script>

2. Implement the menu in js by adding the class open Folding and unfolding

               $(document).on(&#39;click&#39;,&#39;.nav-item:not(.child)&#39;,function () {
              console.log("choosing");
              var that = $(this);
              var next =that.next();
              if(next.hasClass(&#39;childgroup&#39;)){
                  if (that.hasClass(&#39;open&#39;))
                  {
                      // 收起当前菜单项
                      that.removeClass(&#39;open&#39;);
                      next.slideUp();
                  }
                  else{
                      // 将其他打开的菜单项收起来
                      if($(&#39;.nav-item:not(.child).open&#39;).next().hasClass(&#39;childgroup&#39;))
                      {
                          $(&#39;.nav-item:not(.child).open&#39;).next().slideUp();
                          $(&#39;.nav-item:not(.child).open&#39;).removeClass(&#39;open&#39;);
                      }
                      // 激活当前菜单项
                      that.addClass(&#39;open&#39;);
                      next.slideDown();
                  }
              }
              // 监听一级菜单结束

There are also some css usage skills in it, I hope you can remember

Related recommendations:

How are tables in HTML Made by manipulation? (Code example)

HTML object: Introduction to some object properties of html

The above is the detailed content of Code to implement folding menu using html, css and js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn