jQuery complex hide show子菜单

时间:2017-10-13 06:39:22

标签: jquery hide show submenu

我无法找到完成所需逻辑的正确方法。由于布局目的,子菜单需要位于其菜单链接根目录之外。需要的逻辑是:

当子菜单打开时:

  1. 触摸设备中,当用户点按链接时
  2. 当用户悬停链接时指针决定
  3. 当菜单打开时,链接和子菜单必须获得“活动”类。
  4. 当子菜单关闭时:

    1. 单击“关闭”按钮(位于子菜单内)
    2. 当另一个菜单打开时,第一个菜单应该关闭
    3. 触摸设备中,单击子菜单外的任何位置
    4. 指针设备中,当鼠标离开子菜单区域时
    5. 已编辑:这是最终版本100%工作 在@SilverSurfer awnser和ajusts之后

      $(".sub-menu").hide()
      
      $("a.close-bt").click(function(){
          $(this).closest(".sub-menu").hide()
          $(this).closest(".sub-menu").removeClass("active")
          $(this).closest(document).find("a.menu-item").removeClass("active")
      });
      
      $(document).on('click mouseover', "ul.menu > a", function () {
          $("ul.menu > a").removeClass("active")
          $(".sub-menu").hide()
          var target = "."+$(this).attr("id")
          $(target).addClass("active")
          $(this).addClass("active")
          $(target).show()
      });
      $(".sub-menu").mouseleave(function() {
          $(this).hide()
          $(this).removeClass("active")
          $(this).closest(document).find("a.menu-item").removeClass("active")
      });
      // FUNCTIONAL STYLES
      
      .menu {
      	z-index: 2;
      }
      .menu a {
      }
      .sub-menu {
      	//visibility: hidden;
      	//visibility: show;
      }
      .close-bt {
      	
      }
      
      
      // VISUAL STYLES, PLZ IGNORE
      .menu {
      }
      .menu a {
      	margin-right: 30px;
      	background: green;
      	display: inline-block;
      	padding: 20px;
      	color: white;
      }
      .menu a.active {
      	background: red;
      }
      .sub-menu {
      	width: 100px;
      	height: 100px;
      	background: lightblue;
      	border: 2px solid black;
      	z-index: 1;
      	position: relative;
      }
      .sub-menu.active {
      	visibility: show;
      	background: blue;
      }
      .close-bt {
      	background: red;
      	color: white;
      	padding: 10px;
      	position: absolute;
      	right: 10px;
      	bottom: 10px;
      }
      ul { margin: 0; left: 0; padding: 0; }
      
      
      .container  { position: absolute; top: 50px; left: 0; } 
      .item1 { position: absolute; top: 10px; left: 0; }
      .item2 { position: absolute; top: 10px; left: 110px; }
      .item3 { position: absolute; top: 10px; left: 220px; }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      
      <ul class="menu">
      	<a class="menu-item" id="item1" href="javascript:void(0);">Link 1</a>
      	<a class="menu-item" id="item2" href="javascript:void(0);">Link 2</a>
      	<a class="menu-item" id="item3" href="javascript:void(0);">Link 3</a>
      </ul>
      
      
      <div class="container">
      	<div class="sub-menu item1">
          <a class="close-bt" href="javascript:void(0);">Close</a>
      		Sub-menu 1
      	</div>
      	<div class="sub-menu item2">
          <a class="close-bt" href="javascript:void(0);">Close</a>
      		Sub-menu 2
      	</div>
      	<div class="sub-menu item3">
          <a class="close-bt" href="javascript:void(0);">Close</a>
      		Sub-menu 2
      	</div>
      </div>

1 个答案:

答案 0 :(得分:0)

这是你在寻找什么?

&#13;
&#13;
$(".sub-menu").hide()

$("a.close-bt").click(function(){
    $(this).closest(".sub-menu").hide()
    $(this).closest(".sub-menu").removeClass("active")
    $(this).closest(document).find("ul").removeClass("active")
});

$(document).on('click mouseover', "ul > a", function () {
    $(".sub-menu").hide()
    var target = "."+$(this).attr("id")
    $(target).addClass("active")
    $(this).closest("ul.menu").addClass("active")
    $(target).show()
});
&#13;
// FUNCTIONAL STYLES

.menu {
	z-index: 2;
}
.menu a {
}
.sub-menu {
	//visibility: hidden;
	//visibility: show;
}
.close-bt {
	
}


// VISUAL STYLES, PLZ IGNORE
.menu {
}
.menu a {
	margin-right: 30px;
	background: green;
	display: inline-block;
	padding: 20px;
	color: white;
}
.menu a.active {
	background: red;
}
.sub-menu {
	width: 100px;
	height: 100px;
	background: lightblue;
	border: 2px solid black;
	z-index: 1;
	position: relative;
}
.sub-menu.active {
	visibility: show;
	background: blue;
}
.close-bt {
	background: red;
	color: white;
	padding: 10px;
	position: absolute;
	right: 10px;
	bottom: 10px;
}
ul { margin: 0; left: 0; padding: 0; }


.container  { position: absolute; top: 50px; left: 0; } 
.item1 { position: absolute; top: 10px; left: 0; }
.item2 { position: absolute; top: 10px; left: 110px; }
.item3 { position: absolute; top: 10px; left: 220px; }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="menu">
	<a id="item1" href="javascript:void(0);">Link 1</a>
	<a id="item2" href="javascript:void(0);">Link 2</a>
	<a id="item3" href="javascript:void(0);">Link 3</a>
</ul>


<div class="container">
	<div class="sub-menu item1">
    <a class="close-bt" href="javascript:void(0);">Close</a>
		Sub-menu 1
	</div>
	<div class="sub-menu item2">
    <a class="close-bt" href="javascript:void(0);">Close</a>
		Sub-menu 2
	</div>
	<div class="sub-menu item3">
    <a class="close-bt" href="javascript:void(0);">Close</a>
		Sub-menu 2
	</div>
</div>
&#13;
&#13;
&#13;