简体   繁体   English

单击“ jQuery Toggle”下拉菜单

[英]Jquery Toggle Dropdown Menu On Click

I have a menu with three links. 我有一个带有三个链接的菜单。 Upon clicking "One", the first link, a dropdown menu appears. 单击第一个链接“一个”后,将显示一个下拉菜单。 Same goes for clicking "Two", and "Three". 单击“两个”和“三个”也是如此。

How can I get "sub-one" to fade out when "sub-two" is active? 当“ sub-two”处于活动状态时,如何使“ sub-one”逐渐消失?

<div id="navi">

<div id="one"> 
    <a href="#">one</a> 
</div>    

 <div id="two"> 
    <a href="#">two</a> 
</div>    

 <div id="three"> 
    <a href="#">three</a> 
  </div>    
 </div>

  <div id="sub-one">
      <a href="#"> <p> one </p> </a>
      <a href="#"> <p> two </p> </a>
  </div>

  <div id="sub-two">
      <a href="#"> <p> thre </p> </a>
      <a href="#"> <p> four </p> </a>
  </div>

  <div id="sub-three">
      <a href="#"> <p> five </p> </a>
      <a href="#"> <p> six </p> </a>
  </div>

Complete code - jsfiddle.net 完整的代码 -jsfiddle.net

Change your approach to fade all them out by default. 更改您的方法以在默认情况下淡出所有颜色。 Then fade in your target 然后淡入目标

$(document).ready(
      function() {
        $("#one, #two, #three").click(function() {
          var id = $(this).attr('id');
          $('[id^="sub"]').fadeOut();
          $("#sub-" + id).fadeIn();
        });
      });

 $(document).ready( function() { $("#one, #two, #three").click(function() { var id = $(this).attr('id'); $('[id^="sub"]').fadeOut(); if($("#sub-" + id).is(':visible')){ $("#sub-" + id).fadeOut(); } else{ $("#sub-" + id).fadeIn(); } }); }); 
 #navi { font-family: 'Roboto', Helvetica, Sans-serif; font-size: 12px; letter-spacing: 4px; color: black; font-weight: 600; position: fixed; height: 100px; float: right; text-align: right; right: 10%; top: 11%; min-width: 10%; } #navi a { color: black; padding: 0 0 0 5px; } #one, #two, #three { float: left; padding-left: 5px; } #sub-one, #sub-two, #sub-three { display: none; font-family: 'Roboto', Helvetica, Sans-serif; font-size: 12px; letter-spacing: 2px; color: black; font-weight: 400; position: absolute; float: right; text-align: right; right: 10%; top: 15%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="navi"> <div id="one"> <a href="#">one</a> </div> <div id="two"> <a href="#">two</a> </div> <div id="three"> <a href="#">three</a> </div> </div> <div id="sub-one"> <a href="#"> <p>one</p> </a> <a href="#"> <p>two</p> </a> </div> <div id="sub-two"> <a href="#"> <p>thre</p> </a> <a href="#"> <p>four</p> </a> </div> <div id="sub-three"> <a href="#"> <p>five</p> </a> <a href="#"> <p>six</p> </a> </div> 

Check out this updated fiddle 看看这个更新的小提琴

HTML HTML

<div class="sub" id="sub-one">
    <a href="#"> <p> one </p> </a>
    <a href="#"> <p> two </p> </a>
</div>

<div class="sub" id="sub-two">
    <a href="#"> <p> thre </p> </a>
    <a href="#"> <p> four </p> </a>
</div>

<div class="sub" id="sub-three">
    <a href="#"> <p> five </p> </a>
    <a href="#"> <p> six </p> </a>
</div>

JS JS

function fadeOutAll(){
    $(".sub").fadeOut();    
}


$(document).ready(
    function() {
        $("#one").click(function() {
            fadeOutAll()
            $("#sub-one").fadeToggle();
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM