下拉菜单正在关闭,点击下拉区域的任意位置

时间:2017-08-22 05:34:32

标签: jquery html css drop-down-menu

如果我点击下拉菜单的外部(即下拉区域)下拉菜单正在关闭,我有一个下拉菜单。但它不应该发生。

但如果我点击下拉列表的外部(即身体区域),下拉列表应该关闭。它应该会发生。

我不知道我的编码有什么问题,我从未写过这样的逻辑。



$(document).ready(function(){
  
  $('.afs-menu-item').click(function(event){
    event.stopPropagation();
   $('.afs-dropdown',this).slideToggle(300).animate({'left':'0px'});
    $('.afs-col ul li').removeClass('active');
    $(this).siblings().find(".afs-dropdown").hide(); 
    $('.afs-back-button').css('display','none');

    //Reset data attr here
    $(this).data('clicks',false);


  });
  
  /*sub-col-1.2*/
  $('.afs-col ul li').click(function(event){
    event.stopPropagation();
    $(this).toggleClass('active');
    $(this).siblings().find("li").removeClass('active');
    $('.afs-back-button').css('display','none'); 

     //$('.sub-col li').removeClass('active');
    //$(this).children().find(".sub-sub-sub-col").hide();
    
  });
 
  /*sub-sub-col*/
  $('.afs-col ul li ul').click(function(event){
     event.stopPropagation();
    $(this).toggleClass('active');
  });
  
  
   $('.afs-sub-sub-col > ul > li').click(function(event){
     event.stopPropagation();
     var move_left = -($('.afs-dropdown').width()/3);
     var clicks = $(this).parents('.afs-menu-item').data('clicks');
        $('.afs-back-button').css('display','none');

      if (clicks==true) {
        $('.afs-dropdown').animate({
             'left' : '0px'
        })     
      }
      else {
        $('.afs-back-button').css('display','block');
        $('.afs-dropdown').animate({
             'left' : move_left
        })
      }
  $(this).parents('.afs-menu-item').data("clicks", !clicks);
});
  
  /*sub-sub-sub-col*/
   $('.afs-col ul ul li ul').click(function(event){
    event.stopPropagation();
    $(this).toggleClass('active');
  });
  
  $('.afs-back-button').click(function(){    
    $('.afs-dropdown').animate({
      left: '0px'
    })
    $(this).css('display','none');
  })
   
 
});

/* 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;
}

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

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

.afs-dropdown > .afs-col {
  display: block;  
}


.visible {
  display: block;
}

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


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


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

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

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

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

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


.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;
}

/*Back Button*/
.afs-back-button {
  display: none;
  text-align: center;
  padding: 10px 20px;
  width: 100px;
  background-color: #00A0F0;
}
.afs-back-button a {
  color: #fff;
  text-decoration: none;
  font-size: 15px;
  
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="inner">
    <ul class="afs-menu">
        <li class="afs-menu-item"><a href="#">Menu-item-1</a> 
         
          <div class="afs-dropdown">
           
            <div class="afs-col">
              <ul>
                <li><a href="#">Menu-item: 1.1</a></li>
                <li><a href="#">Menu-item: 1.2<span class="iva-children-indenter"><i class="fa fa fa-angle-right"></i></span></a>
                    <div class="afs-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="afs-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="afs-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="afs-menu-item"><a href="#">Menu-item-2</a>
          <div class="afs-dropdown">
            <div class="afs-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="afs-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="afs-sub-sub-col">
                          <ul>
                            <li><a href="#">Menuitem2.2.2.1</a></li>
                            <li><a href="#">Menuitem2.2.2.2<span class="iva-children-indenter-2">
                              <div class="afs-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="afs-menu-item">
          <a href="#"><a href="#">Menu-item-3</a></a>
        </li>
        <li class="afs-menu-item">
          <a href="#"><a href="#">Menu-item-4</a></a>
        </li>
         <div class="afs-back-button">
              <a href="#">Fuel System</a>
        </div>
      
    </ul>
  </div><!--inner-->
</div><!--container-->
&#13;
&#13;
&#13; Codepen LInk

2 个答案:

答案 0 :(得分:0)

检查此示例。

   [$("#siteSelector").click(function( ) {
        $("a:first", this).toggleClass('siteSelect_anchor');
        $("ul:first", this).toggleClass('siteSelect_ul');        
        return false;
    });
    console.log($("#siteSelector li").length);
    $("#siteSelector li").click(function( e ) {
            var liTxt = $("a", this).text();
        console.log(liTxt, $("#siteSelector > a").length);
            $("#siteSelector > a").text(liTxt);
            e.stopPropagation();
            return false;
        });
    $(document).click(function( ) {
         console.log("a");
        $("a:first", $("#siteSelector")).removeClass('siteSelect_anchor');
        $("ul:first", $("#siteSelector")).removeClass('siteSelect_ul');
    });]

fiddle link

答案 1 :(得分:0)

可能你想要这样。

$(document).ready(function(){
  
  $('.afs-menu-item').click(function(event){
    event.stopPropagation();
   $('.afs-dropdown',this).slideToggle(300).animate({'left':'0px'});
    $('.afs-col ul li').removeClass('active');
    $(this).siblings().find(".afs-dropdown").hide(); 
    $('.afs-back-button').css('display','none');

    //Reset data attr here
    $(this).data('clicks',false);


  });
  
  /*sub-col-1.2*/
  $('.afs-col ul li').click(function(event){
    event.stopPropagation();
    $(this).toggleClass('active');
    $(this).siblings().find("li").removeClass('active');
    $('.afs-back-button').css('display','none'); 

     //$('.sub-col li').removeClass('active');
    //$(this).children().find(".sub-sub-sub-col").hide();
    
  });
 
  /*sub-sub-col*/
  $('.afs-col ul li ul').click(function(event){
     event.stopPropagation();
    $(this).toggleClass('active');
  });
  
  
   $('.afs-sub-sub-col > ul > li').click(function(event){
     event.stopPropagation();
     var move_left = -($('.afs-dropdown').width()/3);
     var clicks = $(this).parents('.afs-menu-item').data('clicks');
        $('.afs-back-button').css('display','none');

      if (clicks==true) {
        $('.afs-dropdown').animate({
             'left' : '0px'
        })     
      }
      else {
        $('.afs-back-button').css('display','block');
        $('.afs-dropdown').animate({
             'left' : move_left
        })
      }
  $(this).parents('.afs-menu-item').data("clicks", !clicks);
});
  
  /*sub-sub-sub-col*/
   $('.afs-col ul ul li ul').click(function(event){
    event.stopPropagation();
    $(this).toggleClass('active');
  });
  
  $('.afs-back-button').click(function(){    
    $('.afs-dropdown').animate({
      left: '0px'
    })
    $(this).css('display','none');
  })
   
 
});

$(document).click(function(){
  $(".afs-dropdown").hide();
});
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: 1000px;
  margin: 0 auto;
}

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

.afs-dropdown {
    position: absolute;
    top: 80px;
    left: 0;   
    width: 30%;
    text-align: left;
    display: none;
}

.afs-dropdown > .afs-col {
  display: block;  
}


.visible {
  display: block;
}

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


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


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

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

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

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

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


.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;
}

/*Back Button*/
.afs-back-button {
  display: none;
  text-align: center;
  padding: 10px 20px;
  width: 100px;
  background-color: #00A0F0;
}
.afs-back-button a {
  color: #fff;
  text-decoration: none;
  font-size: 15px;
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="inner">
    <ul class="afs-menu">
        <li class="afs-menu-item"><a href="#">Menu-item-1</a> 
         
          <div class="afs-dropdown">
           
            <div class="afs-col">
              <ul>
                <li><a href="#">Menu-item: 1.1</a></li>
                <li><a href="#">Menu-item: 1.2<span class="iva-children-indenter"><i class="fa fa fa-angle-right"></i></span></a>
                    <div class="afs-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="afs-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="afs-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="afs-menu-item"><a href="#">Menu-item-2</a>
          <div class="afs-dropdown">
            <div class="afs-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="afs-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="afs-sub-sub-col">
                          <ul>
                            <li><a href="#">Menuitem2.2.2.1</a></li>
                            <li><a href="#">Menuitem2.2.2.2<span class="iva-children-indenter-2">
                              <div class="afs-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="afs-menu-item">
          <a href="#"><a href="#">Menu-item-3</a></a>
        </li>
        <li class="afs-menu-item">
          <a href="#"><a href="#">Menu-item-4</a></a>
        </li>
         <div class="afs-back-button">
              <a href="#">Fuel System</a>
        </div>
      
    </ul>
  </div><!--inner-->
</div><!--container-->