当项目折叠或不折叠时,折叠按钮的类会发生变化

时间:2015-03-10 17:41:11

标签: jquery html collapse

对不起这段代码,但我刚开始开发,根本不知道触发我想要的东西。

所以我在寻找: 我已经实现了这一点,当我点击一个按钮时,连接到它的区域显示,另外两个崩溃。 我不知道的是,当我的区域显示时我是如何改变按钮的类别(从fa-angle-right到fa-angle-down)并在折叠时恢复到默认值...

这是我的HTML:

<section id="mes-creations" class="mes-creations text-center">
 <div class="mes-creations">
  <div class="container">
   <div class="row">
    <div class="col-lg-12">
     <h2 id="titre-section">section title</h2>
     <p>section description</p>
    </div>
   </div>
                <!--bouttons-->
   <div class="row">
    <p>
    <div class="col-sm-4 col-md-4 col-lg-4">
     <a class="btn btn-primary stylebtn collapsed" id="bouton-1" data-toggle="collapse" data-parent="#accordeon" href="#collapseGallery1" aria-expanded="false" aria-controls="collapseGallery1">
      <span class="fa fa-angle-right animated"> bouttontitle1</span>
     </a>
    </div>
    </p>
    <p>
    <div class="col-sm-4 col-md-4 col-lg-4">
     <a class="btn btn-primary stylebtn collapsed" id="bouton-2" data-toggle="collapse" data-parent="#accordeon" href="#collapseGallery2" aria-expanded="false" aria-controls="collapseGallery2">
      <span class="fa fa-angle-right animated"> boutontitle2</span>
     </a>
    </div>
    </p>
    <p>
    <div class="col-sm-4 col-md-4 col-lg-4">
     <a class="btn btn-primary stylebtn collapsed" id="bouton-3" data-toggle="collapse" data-parent="#accordeon" href="#collapseGallery3" aria-expanded="false" aria-controls="collapseGallery3">
      <span class="fa fa-angle-right animated"> boutontitle3</span>
     </a>
    </div>
    </p>                    
    </div>
    <!--end button-->
    <!--gallery-->
    <p>
    <div class="row" id="accordeon">
      <div class="gallery-custom row collapse" id="collapseGallery1">
       <div class="col-xs-6 col-sm-4 col-md-3"><img class="img-gallery-style" src="img/Banniere/11-fleurs.png" alt="Banniere1"></div>
       <div class="col-xs-6 col-sm-4 col-md-3"><img class="img-gallery-style" src="img/Banniere/Annees-80.png" alt="Banniere2"></div>
       <div class="col-xs-6 col-sm-4 col-md-3"><img class="img-gallery-style" src="img/Banniere/Banniere-robo.gif" alt="Banniere3"></div>
       <div class="col-xs-6 col-sm-4 col-md-3"><img class="img-gallery-style" src="img/Banniere/banniere-site-2012.png" alt="Banniere4"></div>
       <div class="col-xs-6 col-sm-4 col-md-3"><img class="img-gallery-style" src="img/Banniere/Collection-western.png" alt="Banniere5"></div>
       <div class="col-xs-6 col-sm-4 col-md-3"><img class="img-gallery-style" src="img/Banniere/Beyblade-Film-V1G.png" alt="Banniere6"></div>
      </div>
      <div class="gallery-custom row collapse" id="collapseGallery2">
       <div class="col-xs-4 col-sm-3 col-md-3"><img class="img-gallery-style" src="img/CP/Camelot-CP.png" alt="CP1"></div>
       <div class="col-xs-4 col-sm-3 col-md-3"><img class="img-gallery-style" src="img/CP/CP-Groupe-delite-Final.png" alt="CP2"></div>
       <div class="col-xs-4 col-sm-3 col-md-3"><img class="img-gallery-style" src="img/CP/CP-guerre-des-empires.png" alt="CP3"></div>
       <div class="col-xs-4 col-sm-3 col-md-3"><img class="img-gallery-style" src="img/CP/CP-GUERRIERE_ZYLO.png" alt="CP4"></div>
       <div class="col-xs-4 col-sm-3 col-md-3"><img class="img-gallery-style" src="img/CP/CP-MAntera-copie.png" alt="CP5"></div>
       <div class="col-xs-4 col-sm-3 col-md-3"><img class="img-gallery-style" src="img/CP/CP-OFF-WHITE-LIES_ZYLO2.png" alt="CP6"></div>
      </div>
      <div class="gallery-custom row collapse" id="collapseGallery3">
       <div class="col-xs-6 col-sm-6 col-md-6"><img class="img-gallery-style" src="img/Print/BLACKS-GAME-ANNEES-LASER.png" alt="Print1"></div>
       <div class="col-xs-6 col-sm-6 col-md-6"><img class="img-gallery-style" src="img/Print/BLACKS-GAME-MADMOVIES.png" alt="Print2"></div>
       <div class="col-xs-6 col-sm-6 col-md-6"><img class="img-gallery-style" src="img/Print/print-2013-final.png" alt="Print3"></div>
       <div class="col-xs-6 col-sm-6 col-md-6"><img class="img-gallery-style" src="img/Print/Print-Line-Up-Final.png" alt="Print4"></div>
      </div>
     </div>
     </p>
    </div>
   </div>
  </section>

和我的js:

$('#accordeon').on('show.bs.collapse', function () {
    $('#accordeon .in').collapse('hide');
});

Demo

1 个答案:

答案 0 :(得分:0)

这是一种有点hackish的方式来做到这一点。如果你使用更接近Bootstrap建议的标记结构会更简单。

$('.stylebtn').click(function() {
    $(this).find('.fa').toggleClass('fa-angle-down fa-angle-right')
    .closest('div').siblings().find('.fa')
    .removeClass('fa-angle-down').addClass('fa-angle-right');
});

<强> Demo