Bootstrap 3 accordion styling on open/closed

我怕爱的太早我们不能终老 提交于 2019-11-30 13:53:57

Since I was unable to do anything in css only I went ahead and some jquery. For anyone interested you can add/remove an active class to the entire div then apply styling that way.

    $('.panel-faq').on('show.bs.collapse', function () {
         $(this).addClass('active');
    });

    $('.panel-faq').on('hide.bs.collapse', function () {
         $(this).removeClass('active');
    });

To do this the AngularJS way (no JQuery) you can put a conditional class on the accordion-group.

<div accordion-group is-open="first.open"
     ng-class="{'active': first.open}">
  <div accordion-heading>First Section</div>
  <div>First content</div>
</div>


.active[accordion-group] .panel-heading {
  background-color: black;
  color: white;
}

This way the active class is applied to the whole accordion-group when it is open and the panel-heading div (generated by bootstrap-ui) can be styled with a specific CSS selector.

Mehul Gohil

Try This:

For Hover Effect:

.panel-faq .panel-heading:hover {

   color: #333333;    
   background:#f5f5f5 url('/assets/img/faq-question.png') no-repeat 10px center;    
   padding-left: 45px;    
   border-color: #dddddd;

}

if background not getting overriden, you can also try:

background:#f5f5f5 url('/assets/img/faq-question.png') no-repeat 10px center !important;

Here is the CSS only solution I came up with...

The jQuery solution wasn't great because if a user clicks too quick the styles can get thrown off.

Just target your headers class when it's not collapsed:

.panel-heading:not(.collapsed) { 
/*Your styles*/
}

The HTML I have:

<div class="collapsed panel-header" data-toggle="collapse" data-parent="panelParent" data-target="targetPanel">Header</div>

Finally to answer the question about hover, it's as simple as:

.panel-heading:hover {
/*Your styles*/
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!