简体   繁体   English

jQuery的fadeIn之一,fadeOut多个div

[英]JQuery fadeIn one, fadeOut multiple divs

I have a function like this: 我有一个这样的功能:

$(document).ready(function(){
  $("#showt").click(function(){
    $(".t").fadeIn("fast");
  });
  $("#showt").click(function(){
    $(".m").fadeOut(0);
  });
  $("#showt").click(function(){
    $(".a").fadeOut(0);
  });
});

$(document).ready(function(){
  $("#showm").click(function(){
    $(".m").fadeIn("fast");
  });
  $("#showm").click(function(){
    $(".t").fadeOut(0);
  });
  $("#showm").click(function(){
    $(".a").fadeOut(0);
  });
});

$(document).ready(function(){
  $("#showa").click(function(){
    $(".a").fadeIn("fast");
  });
  $("#showa").click(function(){
    $(".t").fadeOut(0);
  });
  $("#showa").click(function(){
    $(".m").fadeOut(0);
  });
});

So on a click of a link with a certain ID it shows one, but hides other blocks, everything works perfectly but I'm just interested is there a way to reduce the function's code? 因此,在单击具有特定ID的链接时,它会显示一个,但隐藏其他块,一切正常,但是我只是想知道是否有一种减少函数代码的方法?

Thank you. 谢谢。

HTML: HTML:

<a href="#" class="tag" id="showt">ttt</a>
<a href="#" class="tag" id="showm">mmm</a> 
<a href="#" class="tag" id="showa">aaa</a>          

<div class="row">
  <div class="col-1"></div>
  <div class="col-2">
    <div class="t">
        Ttt
    </div>
    <div class="m">
        Mmm
    </div>
    <div class="a">
        Aaa
    </div>
</div>
<div class="col-3">
    <div class="t">
        Ttt
    </div>
    <div class="m">
        Mmm
    </div>
    <div class="a">
        Aaa
    </div>
</div>
<div class="col-4"></div>

You could use toggle() to toggle the current state. 您可以使用toggle()切换当前状态。 here is the link for api 这是api的链接

Also to reduce, you can make a function which will work as callback function for similar work. 同样为了减少,您可以制作一个函数,该函数将用作类似工作的回调函数。 Like 喜欢

$('.bar').click(callback); $( '巴')点击(回调)。

You can considerably consolidate your code by using custom attributes; 您可以使用自定义属性来大大整合代码。 I chose the name show-class to denote the class the link wants to show. 我选择名称show-class来表示链接要显示的类。 If you plan to add more classes than just .t , .m , and .a you could add a secondary class that all of them ( .t, .m, .a ) have in common, and fade that out. 如果您计划添加更多的类,而不仅仅是.t.m.a ,则可以添加所有它们( .t, .m, .a )共有的第二类,并将其淡出。 Additionally, you can use the .hide() function to make your code a little simpler (rather than .fadeOut(0) ). 另外,您可以使用.hide()函数使您的代码更简单(而不是.fadeOut(0) )。

 $(document).ready(function(){ $(".showStuff").click(function (){ $(".t, .m, .a").hide(); $("." + $(this).attr("show-class")).fadeIn("fast"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" class="tag showStuff" show-class="t">ttt</a> <a href="#" class="tag showStuff" show-class="m">mmm</a> <a href="#" class="tag showStuff" show-class="a">aaa</a> <div class="row"> <div class="col-1"></div> <div class="col-2"> <div class="t"> Ttt </div> <div class="m"> Mmm </div> <div class="a"> Aaa </div> </div> <div class="col-3"> <div class="t"> Ttt </div> <div class="m"> Mmm </div> <div class="a"> Aaa </div> </div> <div class="col-4"></div> 

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

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