简体   繁体   中英

How to close all open panels when another one is clicked

I've got a search box running here:

http://codepen.io/h0rhay/pen/Kgqwt

Code:

$('.findNavL1').click(function () {
    $(this).children('.slideContainer').toggleClass('show').css('z-index', +1);
});

$('.slideContainer').click(function () {
    if ($(this).hasClass('show')) {
        $(this).toggleClass('show');
    }
});

Markup:

<div class="row">
    <div class="twelve columns width33">
        <div class="navContainer rel">
             <h2 class="navHeading">Find a holiday</h2>
             <ul class="findHolNav">
                <li class="findNavL1" onClick="return true"> <span>When</span>
                    <div class="slideContainer">
                         <h2 class="internalContent">This is some content 1</h2>
                    </div>
                </li>
                <li class="findNavL1" onClick="return true"> <span>How many Days </span>
                    <div class="slideContainer">
                         <h2 class="internalContent">This is some content 2</h2>
                    </div>
                </li>
                <li class="findNavL1" onClick="return true"> <span>Where</span>
                    <div class="slideContainer">
                         <h2 class="internalContent">This is some content 3</h2>
                    </div>
                </li>
                <li class="findNavL1" onClick="return true"> <span>Departing</span>
                    <div class="slideContainer">
                         <h2 class="internalContent">This is some content 4</h2>
                    </div>
                </li>
                <li class="findNavL1" onClick="return true"> <span>No. of People</span>
                    <div class="slideContainer">
                         <h2 class="internalContent">This is some content 5</h2>
                    </div>
                </li>
            </ul>
        </div>

CSS:

.rel {
    position:relative;
}

.staticP {
    position:static !important;
}

.show {
    display:block !important;
}

.hide {
    display:none;
}

/* -----------------------------------------
   Shared Styles
----------------------------------------- */

.width33 {
    width:33.3% !important;
}

.navContainer {
    background:#ccc;
}

.findHolNav {
    list-style-type: none;
    padding:0;
}

.findNavL1{
    cursor:pointer;

}

.findNavL1 span {
    padding:20px;
    display:block;
}

.findNavL1:nth-of-type(odd){
    background-color:#c6c6c6;
}

.findNavL1:hover{
    background-color:#c2c2c2;
}

.findNavL1:hover > .slideContainer, .findNavL1:active > .slideContainer {

}

.slideContainer {
    display:none;
    background-color:lime;
    min-height:100%;
    position:absolute;
    top:0;
    left:100%;
    width:208%;
}

At the moment, the UI is nearly right. When you click on a link the corresponding panel opens up.

However if you click on another link, its corresponding panel opens but the previous one stays open.

I want it to close.

Which would leave only the active one open.

//////////////////////////////////////

I should have said.. The UI is a bit more complex than maybe I said before.

1st click: Slides open the container.

Click same link: Slides shut

Click inside open container: Container remains open.

(I've got all this working ok)

If you click different link, then open container shuts and new one opens..

Thats the bit I'm stuck on.

Use the following JS instead of the one, which you have written ( codepen ):

$('.findNavL1').click(function(){
  $('.slideContainer').hide();
  $(this).find('.slideContainer').show();
});

change your block as below :

if ($(this).hasClass('show')){
    $(".slideContainer").removeClass("show");
    $(this).toggleClass('show');
    }
});

You can try following...

$('.findNavL1').click(function(){
    $('.slideContainer').each(function( index ){
        $(this).removeClass('show');
        $(this).addClass('hide');
    });
    $(this).children('.slideContainer').addClass('show');
});

See this js fiddle... example

Yes! Got there in the end:

$('.findNavL1').click(function(){
var searchBoxLink = $(this);
var inactiveLink = $('.findNavL1').not(searchBoxLink);

if ( $(inactiveLink).children('.slideContainer').hasClass('show') ){
  $(inactiveLink).children('.slideContainer').removeClass('show');
}

$(searchBoxLink).children('.slideContainer').toggleClass('show');
});


$('.slideContainer').click(function(){
if ( $(this).hasClass('show') ){
 $(this).toggleClass('show');
        }
});

http://codepen.io/h0rhay/pen/Kgqwt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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