tesicg
tesicg

Reputation: 4053

Bootstrap: Collapsing/expading with icons issue

I use bootstrap JavaScript library and made it work collapse/expand element. The code looks as following:

<div id="e_ticket_info" class="e_ticket_font" runat="server" Visible="False">
    <div class="panel-group" id="accordion">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h1 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" style="color:firebrick; font-size: large;">
                        E-TICKET...
                    </a>
                    <i class="icon-large icon-arrow-down"></i>
                </h1>
            </div>
            <div id="collapseOne" class="panel-collapse collapse">
                <div class="panel-body">
                    Some text...
                </div>
            </div>
        </div>
    </div>
</div>

As you can see, I was able to show the arrow-down icon. When I click accordion element the text expands down what I wanted, but I need to toggle arrow-down icon to arrow-up icon at that moment and vice versa, when click accordion element to collapse text to show arrow-down icon again.

How to do it?

Upvotes: 4

Views: 11788

Answers (2)

user2213334
user2213334

Reputation: 21

Kevin's answer works great! The great thing about his answer is that it also works with Bootstrap 2, unlike some other answers I have found (e.g. Bootstrap 3 Collapse show state with Chevron icon).

To answer your question tesicg, what Kevin Bowersox is doing is a css sprite technique, taking advantage of the bootstrap sprite.

Check out http://css-tricks.com/css-sprites/ for a link to a great tool and explanation of sprites.

I personally, wanted to use chevron icons instead, so I modified Kevin's answer like so:

/** Created new class toggle arrow that uses the sprite 
    coordinates for the up and down arrows in bootstrap. **/
.toggle-arrow{
    background-position: -312px -116px;
}

/** This specific selector will cause the toggling
    bootstrap adds and removes the collapsed class on the accordian. **/
.collapsed .toggle-arrow{
    background-position: -454px -72px;
}

Basically, what I did was shift the "viewport" pixels, if you will, to expose the chevron portion of the sprite instead of the up/down arrows.

I also added the pull-right class so that the icons are on the far-right of the accordian heading, like so:

<i class="icon-large toggle-arrow pull-right"></i>

Thanks Kevin, this helped a bunch!

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

CSS

/** 
    Created new class toggle arrow that uses the sprite 
    coordinates for the up and down arrows in bootstrap.
**/

.toggle-arrow{
    background-position: -289px -96px;
}

/** 
    This specific selector will cause the toggling
    bootstrap adds and removes the collapsed class on the accordian.
**/

.collapsed .toggle-arrow{
    background-position: -312px -96px;
}

HTML

<!-- Nested the i tag within the a toggle by collapsed -->
<div class="panel-heading">
    <h1 class="panel-title">
        <!-- Initialized a with class="collapsed" -->
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"
              style="color:firebrick; font-size: large;" class="collapsed">
            E-TICKET...
            <i class="icon-large toggle-arrow"></i>
        </a>
    </h1>
</div>

JS Fiddle: http://jsfiddle.net/uBzeZ/1/

Upvotes: 4

Related Questions