【问题标题】:Bootstrap: Collapsing/expading with icons issueBootstrap:使用图标问题折叠/展开
【发布时间】:2013-10-29 16:45:42
【问题描述】:

我使用 bootstrap 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>

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

怎么做?

【问题讨论】:

    标签: twitter-bootstrap toggle


    【解决方案1】:

    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/

    【讨论】:

    • 引导库中没有切换箭头类。
    • bootstrap.css中有2个类: .icon-arrow-up { background-position: -289px -96px; } 和 .icon-arrow-down { 背景位置:-312px -96px; }
    • @tesicg 我知道没有。我做了那个类,然后用它来根据 .collapse 的存在来切换箭头图像
    • 当我使用你的代码时发生了什么?当我使用引导代码打开页面时,标题“E-TICKET”和向下箭头图标在那里,这很好。之后,当我单击标题“E-TICKET”时,文本会展开,但图标保持向下箭头,而不是向上箭头。当我再次单击标题“E-TICKET”将其折叠时,文本折叠,并且图标变为向上箭头,这是不正确的。应该有向下箭头图标。
    • 所以,图标正在切换,但方式不正确,因为它在第一次点击时没有改变。
    【解决方案2】:

    凯文的回答效果很好!他的答案的好处在于它也适用于 Bootstrap 2,这与我发现的其他一些答案不同(例如 Bootstrap 3 Collapse show state with Chevron icon)。

    为了回答您的问题 tesicg,Kevin Bowersox 正在做的是一种 css sprite 技术,利用了 bootstrap sprite。

    查看http://css-tricks.com/css-sprites/ 以获得一个很棒的工具和精灵解释的链接。

    我个人想改用 V 形图标,所以我修改了 Kevin 的答案,如下所示:

    /** 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;
    }
    

    基本上,我所做的是移动“视口”像素(如果您愿意的话)以显示精灵的人字形部分而不是向上/向下箭头。

    我还添加了 pull-right 类,以便图标位于手风琴标题的最右侧,如下所示:

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

    感谢凯文,这帮助了很多人!

    【讨论】:

      猜你喜欢
      • 2013-10-28
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 2019-05-15
      • 2014-03-30
      相关资源
      最近更新 更多