简体   繁体   English

给定慢速参数时,jquery隐藏和显示方法无法正常工作

[英]jquery hide and show method not working properly when given the slow parameter

What i want to achieve is, when i click one of the three tabs, it should fade in a description div on the clicked div, and hide it on the other divs(the description div has the same class) 我想要实现的是,当我单击三个选项卡之一时,它应在所单击的div上的说明div中淡出,并在其他div上隐藏它(说明div具有相同的类)

now, if i use .show() and .hide() without a parameter(example .hide("slow")), it works perfectly, and if i click the active description div, nothing happens(as expected). 现在,如果我使用.show().hide()不带参数(例如.hide(“慢”)),它完美的作品,如果我单击活动描述DIV,什么都不会发生(如预期)。 while if i use .hide("slow") and .show("slow"), or instead of "slow" i use millisecond parameter(1000), it fades in and fades out just like it should, but if i click on an active description div, it will fade out and fade in back again, which i don't want it to happen. 而如果我使用.hide(“ slow”)和.show(“ slow”),或者我使用毫秒参数(1000)而不是“ slow”,它会像应该的那样淡入淡出,但是如果我单击一个活跃的description div,它会逐渐淡出并再次淡入,这是我不希望发生的情况。 JS fiddle below for reference. 下面的JS小提琴供参考。

https://jsfiddle.net/k00s1fek/ https://jsfiddle.net/k00s1fek/

Just add a simple check to see if the current selector has a div with display : none . 只需添加一个简单的检查,看看当前选择器是否具有display : none为div的div即可。 Below is the JavaScript code needed. 以下是所需的JavaScript代码。

$('.col-lg-2').click(function() {

  if ($(this).children('.desc').css("display") == "none") {
    $('.desc').not(this).hide("slow");
    $('.desc', this).show("slow");
  }

})

This works fine, hope it helps: 效果很好,希望对您有所帮助:

$('.col-lg-2').click(function(){
     var current = $(this).find(".desc")
     $(current).show("slow");
     $(".desc").not(current).hide("slow");
})

You can do it like this 你可以这样

 $('.col-lg-2').click(function(){ if(!$(this).hasClass('active')){ $('.col-lg-2').not(this).removeClass('active').find('.desc').hide('slow'); $(this).addClass('active').find('.desc').show('slow'); } }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="col-xs-4 col-lg-2" data-toggle="popover" title="" data-content=" Një filxhan çaji cool nga Electronic Beats. " data-original-title=" Filxhan caji " data-trigger="focus" tabindex="0" role="button" data-placement="top"> <div style="background-color:#d0d0d0;"><img src="https://i.imgur.com/XQeCxxh.png"> </div><div style="position: absolute; background-color: rgb(226, 0, 116); color: white; top: 0px; text-align: center; margin: 0px; right: 10px; left: 10px;" class="desc"> a stupid bag </div> </div> <div class="col-xs-4 col-lg-2" data-toggle="popover" title="" data-content="Some content inside the popover" data-original-title=""> <div style="background-color:#d0d0d0;"><img src="https://i.imgur.com/QJOzklY.png"> </div><div style="position: absolute; background-color: rgb(216, 0, 116); color: white; top: 0px; text-align: center; margin: 0px; right: 10px; left: 10px; line-height: 18px; display: none;" class="desc">laka laka lagu iku miku dhe taku ke shpia lakut noren 2 e gjys tdarkes </div> </div> <div class="col-xs-4 col-lg-2"> <div style="background-color:#d0d0d0;"><img src="https://i.imgur.com/6gAPzHW.png"> </div><div class="desc" style="position: absolute; background-color: rgb(216, 0, 116); color: white; top: 0px; text-align: center; margin: 0px; right: 10px; left: 10px; display: none;"> a stupid bag </div></div> 

 $('.col-lg-2').click(function(){ $('.desc').not($(this).find('.desc')).hide(); $(this).find('.desc').show("slow"); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row"> <div class="col-xs-4 col-lg-2" data-toggle="popover" title="" data-content=" Një filxhan çaji cool nga Electronic Beats. " data-original-title=" Filxhan caji " data-trigger="focus" tabindex="0" role="button" data-placement="top"> <div style="background-color:#d0d0d0;"><img src="https://i.imgur.com/XQeCxxh.png"> </div><div style="position: absolute; background-color: rgb(226, 0, 116); color: white; top: 0px; text-align: center; margin: 0px; right: 10px; left: 10px;" class="desc">first tab </div> </div> <div class="col-xs-4 col-lg-2" data-toggle="popover" title="" data-content="Some content inside the popover" data-original-title=""> <div style="background-color:#d0d0d0;"><img src="https://i.imgur.com/QJOzklY.png"> </div><div style="position: absolute; background-color: rgb(216, 0, 116); color: white; top: 0px; text-align: center; margin: 0px; right: 10px; left: 10px; line-height: 18px; display: none;" class="desc">second tab </div> </div> <div class="col-xs-4 col-lg-2"> <div style="background-color:#d0d0d0;"><img src="https://i.imgur.com/6gAPzHW.png"> </div><div class="desc" style="position: absolute; background-color: rgb(216, 0, 116); color: white; top: 0px; text-align: center; margin: 0px; right: 10px; left: 10px; display: none;"> third tab </div> </div> </div> 

First hide .desc div and then show current div desc 首先隐藏.desc div,然后显示当前div desc

$('.col-lg-2').click(function(){
  $('.desc').not($(this).find('.desc')).hide();
  $(this).find('.desc').show("slow");
})

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

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