如果导航菜单有子子菜单,则应向左移动

时间:2017-08-11 11:37:18

标签: jquery html css menu navigation

我正在创建一个具有四个深度级别的导航菜单,我一次显示3个深度级别,当用户进入深度级别4时,第一个深度级别应向左移动并仅显示第二个,第3,第4深度水平。

但我在这里遇到的问题是,点击深度级别3中的菜单项(没有孩子),菜单也向左移动。

即,点击菜单项1>菜单项目1.2>菜单item1.2.2>菜单item1.2.2.1  这里菜单项1.2.2.1没有孩子,但菜单也向左移动

特别是,如果只有儿童子菜单,如何移动我的菜单?

这是我的代码:



$(document).ready(function(){
  
  $('.menu-item').click(function(event){
    event.stopPropagation();
    $('.dropdown',this).slideToggle(300);
    $('.col ul li').removeClass('active');
    $(this).siblings().find(".dropdown").hide();     
  });
  
  /*sub-col-1.2*/
  $('.col ul li').click(function(event){
    event.stopPropagation();
    $(this).toggleClass('active');
    $(this).siblings().find("li").removeClass('active');
     //$('.sub-col li').removeClass('active');
    //$(this).children().find(".sub-sub-sub-col").hide();
    
  });
  
  /*sub-sub-col*/
  $('.col ul li ul').click(function(event){
     event.stopPropagation();
    $(this).toggleClass('active');
  });
  

   $('.sub-sub-col > ul > li').click(function() {
  var clicks = $(this).data('clicks');
      if (clicks) {
        $('.dropdown').animate({
             'left' : '0px'
        })   
      }
      else {
        $('.dropdown').animate({
             'left' : '-510px'
        })
      }
  $(this).data("clicks", !clicks);
});


  
  
  /*sub-sub-sub-col*/
   $('.col ul ul li ul').click(function(event){
    event.stopPropagation();
    $(this).toggleClass('active');
  });
 
});

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

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;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
  font-family: 'Mada', sans-serif;
  font-weight: 600;
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}

/*Container
===============*/

.container {
  position: relative;
  width: 100%;
  margin: 0 auto;
}

.inner {
  position: relative;
  width: 1500px;
  margin: 0 auto;
}

.menu {
  text-decoration: none;
  text-align: center;
}
.menu li a {
  text-decoration: none;
  font-size: 23px;
  color: #00A0F0;
   transition: all 0.3s linear;
}
.dropdown li a {
  text-decoration: none;
  font-size: 18px;
  color: #00A0F0;
  transition: all 0.3s linear;
}
li a:hover {
  color: #000;
}
.menu>li {
  display: inline-block;
  padding: 15px 20px;
}

.dropdown {
    position: absolute;
    top: 55px;
    left: 0;   
    width: 100%;
    text-align: left;
    display: none;
}

.dropdown > .col {
  display: block;  
}


.visible {
  display: block;
}

.col {  
  position: relative;
  top: 0;
  display: block;
  width: 33%;
  background-color: transparent;
  z-index: 99;
}

.col > ul {

}
.col > ul > li {
  padding: 20px 0px;
  border-bottom: 1px dotted #ccc;
}
.sub-col > ul > li {
  padding: 20px 0px;
  border-bottom: 1px dotted #ccc;
}
.sub-sub-col > ul > li {
  padding: 20px 0px;
  border-bottom: 1px dotted #ccc;
}
.sub-sub-sub-col > ul > li {
  padding: 20px 0px;
  border-bottom: 1px dotted #ccc;
}
.col> ul > li:last-child { 
}



.sub-col {
  position: absolute;
  left: 510px;
  top: 0;
  display: none;
  width: 100%;
  
  
}

.sub-sub-col {
  position: absolute;
  left: 510px;
  top: 0;
  display: none;
  width: 100%;

}
.sub-sub-sub-col {
  position: absolute;
  left: 510px;
  top: 0;
  display: none;
  width: 100%;
}

.active .col {
  display: block;
}
.col .active .sub-col{
  display: block;
}

.col .sub-col .active .sub-sub-col {
  display: block;
}
 .col .sub-col .sub-sub-col .active .sub-sub-sub-col{
  display: block;
}

.col,.sub-col,.sub-sub-col,.sub-sub-sub-col {
   
}

.iva-children-indenter {
    position: absolute;
    right: 0;
    top: 30%;
    margin-top: 7px;
    color: #00A0F0;
    font-size: 20px;
}
.iva-children-indenter-2{
    position: absolute;
    right: 0;
    top: 30%;
    font-size: 20px;
    margin-top: 40px;
    color: #00A0F0;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://use.fontawesome.com/a2e210f715.js"></script>
<link href="https://fonts.googleapis.com/css?family=Mada:400,500,600" rel="stylesheet">
<div class="container">
  <div class="inner">
    <ul class="menu">
        <li class="menu-item"><a href="#">Menu-item-1</a>   
          
          <div class="dropdown">
            <div class="col">
              <ul>
                <li><a href="#">Menu-item: 1.1</a></li>
                <li class="deeper-nav"><a href="#">Menu-item: 1.2<span class="iva-children-indenter"><i class="fa fa fa-angle-right"></i></span></a>
                    <div class="sub-col">
                      <ul>
                        <li><a href="#">Menuitem1.2.1</a></li>
                        <li><a href="#">Menuitem1.2.2<span class="iva-children-indenter-2"><i class="fa fa fa-angle-right"></i></span>
                            <div class="sub-sub-col">
                              <ul>
                                <li><a href="#">Menuitem1.2.2.1</a></li>
                                <li><a href="#">Menuitem1.2.2.2<span class="iva-children-indenter-2"><i class="fa fa fa-angle-right"></i></span>
                                  <div class="sub-sub-sub-col">
                                    <ul>
                                      <li><a href="#">menuitem1.2.2.2.1</a></li>
                                      <li><a href="#">menuitem1.2.2.2.2</a></li>
                                    </ul>
                                  </div>
                                  </a></li>
                               </ul>
                            </div><!--sub-sub-col-->
                          </a></li>   
                        </ul>
                      </div><!--sub-col-->
                  </a></li>
                <li><a href="#">Menu-item-1.3</a></li>
                <li><a href="#">Menu-item-1.4</a></li>
               </ul>
              </div>  <!--col-->  
             </div><!--dropdown-->
            </li>
        <li class="menu-item"><a href="#">Menu-item-2</a>
          <div class="dropdown">
            <div class="col">
              <ul>
                <li><a href="#">Menu-item: 2.1</a></li>
                <li><a href="#">Menu-item: 2.2 <span class="iva-children-indenter-2"><i class="fa fa fa-angle-right"></i></span>       
                  <div class="sub-col">
                    <ul>
                      <li><a href="#">Menuitem2.2.1</a></li>
                      <li><a href="#">menuitem2.2.2<span class="iva-children-indenter-2"><i class="fa fa fa-angle-right"></i></span>
                        <div class="sub-sub-col">
                          <ul>
                            <li><a href="#">Menuitem2.2.2.1</a></li>
                            <li><a href="#">Menuitem2.2.2.2
                              <!--<div class="sub-sub-sub-col">
                                <ul>
                                  <li><a href="#">menuitem1.2.2.2.1</a></li>
                                  <li><a href="#">menuitem1.2.2.2.2</a></li>
                                </ul>
                              </div>-->
                              </a></li>
                          </ul>
                        </div><!--sub-sub-col-->
                        </a></li>   
                    </ul>
                  </div><!--sub-col-->
                  </a></li>
              </ul>
            </div>  <!--col-->  
           </div><!--dropdown-->
        </li>
        <li class="menu-item">
          <a href="#"><a href="#">Menu-item-3</a></a>
        </li>
        <li class="menu-item">
          <a href="#"><a href="#">Menu-item-4</a></a>
        </li>
    </ul>
  </div><!--inner-->
</div><!--container-->
&#13;
&#13;
&#13;

Codepen Link Here

1 个答案:

答案 0 :(得分:0)

试试这个:

    $('.sub-sub-col > ul > li').click(function() {
     if(!$(this).find('.sub-sub-sub-col').length){
      return false;
     }
  var clicks = $(this).data('clicks'); //continue your code
});

或者:

 if(!$(this).children('.sub-sub-sub-col').length){
  return false;
 }

<强> ============================================ ====

//   if click on sub-col
  $('.sub-col > ul > li').on('click', function(){
      if (!$(this).hasClass('active')) {
        $('.dropdown').animate({
             'left' : '10px'
        })  
      }
  });

   $('.sub-sub-col > ul > li').click(function() {
  // var clicks = $(this).data('clicks');
      if (!$(this).hasClass('active')) {
        $('.dropdown').animate({
             'left' : '10px'
        })   
      }
      else {
        $('.dropdown').animate({
             'left' : '-510px'
        })
      }
  // $(this).data("clicks", !clicks);
});