4

我使用引导 JavaScript 库并使其工作折叠/展开元素。代码如下所示:

<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>

如您所见,我能够显示向下箭头图标。当我单击手风琴元素时,文本会向下扩展我想要的内容,但是此时我需要将向下箭头图标切换为向上箭头图标,反之亦然,当单击手风琴元素以折叠文本以再次显示向下箭头图标时。

怎么做?

4

2 回答 2

4

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 小提琴:http: //jsfiddle.net/uBzeZ/1/

于 2013-10-21T09:29:15.193 回答
1

凯文的回答效果很好!关于他的答案的伟大之处在于它也适用于 Bootstrap 2,这与我发现的其他一些答案不同(例如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!

于 2014-05-13T15:29:45.923 回答