Jquery close toggle when other toggle is open

2019-05-27 11:59发布

问题:

I have two sliding panels, panel1 is sliding down, while panel2 is sliding from left. I want to close panel1 when opening panel2. I know there's many similar question already been answered but I couldn't figure it out how do I apply those in my site.

$(document).ready(function(){
  $(".slide-btn1").click(function(){
    $("#panel1").toggle("slow");
    $(this).toggleClass("active");
    return false;
  });
  $(".slide-btn2").click(function(){
    $("#panel2").slideToggle("slow");
    $(this).toggleClass("active");
    return false;
  });
});

Edit: this is what I have done so far http://jsfiddle.net/Pukau/9CFgR/2/

回答1:

$(document).ready(function(){
    $(".slide-btn1").click(function(){
           $("#panel2").slideUp("fast");
            $('.active').removeClass('active');
        $("#panel1").toggle("fast");
        $(this).toggleClass("active");
        return false;
    });
        $(".slide-btn2").click(function(){
             $("#panel1").slideUp("fast");
           $('.active').removeClass('active');
        $("#panel2").slideToggle("slow");
        $(this).toggleClass("active");
        return false;
    });
});​

for live demo see this link: http://jsfiddle.net/nanoquantumtech/HT9Bx/



标签: jquery toggle