dropdowns.html 10.8 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
2
3
4
5
6
7
8
9
10
11
<div class="bs-docs-section">
  <h1 id="dropdowns" class="page-header">Dropdowns <small>dropdown.js</small></h1>

  <h2 id="dropdowns-examples">Examples</h2>
  <p>Add dropdown menus to nearly anything with this simple plugin, including the navbar, tabs, and pills.</p>

  <h3>Within a navbar</h3>
  <div class="bs-example">
    <nav id="navbar-example" class="navbar navbar-default navbar-static" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
12
          <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target=".bs-example-js-navbar-collapse">
Mark Otto's avatar
Mark Otto committed
13
14
15
16
17
18
19
20
21
22
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project Name</a>
        </div>
        <div class="collapse navbar-collapse bs-example-js-navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="dropdown">
23
              <a id="drop1" href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
24
25
                Dropdown
                <span class="caret"></span>
Mark Otto's avatar
Mark Otto committed
26
              </a>
Mark Otto's avatar
Mark Otto committed
27
              <ul class="dropdown-menu" role="menu" aria-labelledby="drop1">
28
29
30
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Action</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Another action</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Something else here</a></li>
Mark Otto's avatar
Mark Otto committed
31
                <li role="presentation" class="divider"></li>
32
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Separated link</a></li>
Mark Otto's avatar
Mark Otto committed
33
34
35
              </ul>
            </li>
            <li class="dropdown">
36
              <a id="drop2" href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
Mark Otto's avatar
Mark Otto committed
37
                Dropdown
38
                <span class="caret"></span>
Mark Otto's avatar
Mark Otto committed
39
              </a>
Mark Otto's avatar
Mark Otto committed
40
              <ul class="dropdown-menu" role="menu" aria-labelledby="drop2">
41
42
43
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Action</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Another action</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Something else here</a></li>
Mark Otto's avatar
Mark Otto committed
44
                <li role="presentation" class="divider"></li>
45
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Separated link</a></li>
Mark Otto's avatar
Mark Otto committed
46
47
48
49
50
              </ul>
            </li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li id="fat-menu" class="dropdown">
51
              <a id="drop3" href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
Mark Otto's avatar
Mark Otto committed
52
                Dropdown
53
                <span class="caret"></span>
Mark Otto's avatar
Mark Otto committed
54
              </a>
Mark Otto's avatar
Mark Otto committed
55
              <ul class="dropdown-menu" role="menu" aria-labelledby="drop3">
56
57
58
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Action</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Another action</a></li>
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Something else here</a></li>
Mark Otto's avatar
Mark Otto committed
59
                <li role="presentation" class="divider"></li>
60
                <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Separated link</a></li>
Mark Otto's avatar
Mark Otto committed
61
62
63
64
65
66
67
68
69
70
              </ul>
            </li>
          </ul>
        </div><!-- /.nav-collapse -->
      </div><!-- /.container-fluid -->
    </nav> <!-- /navbar-example -->
  </div> <!-- /example -->

  <h3>Within pills</h3>
  <div class="bs-example">
71
    <ul class="nav nav-pills" role="tablist">
72
73
      <li role="presentation" class="active"><a href="#">Regular link</a></li>
      <li role="presentation" class="dropdown">
74
        <a id="drop4" href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
75
76
          Dropdown
          <span class="caret"></span>
Mark Otto's avatar
Mark Otto committed
77
        </a>
Mark Otto's avatar
Mark Otto committed
78
        <ul id="menu1" class="dropdown-menu" role="menu" aria-labelledby="drop4">
79
80
81
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Action</a></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Another action</a></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Something else here</a></li>
Mark Otto's avatar
Mark Otto committed
82
          <li role="presentation" class="divider"></li>
83
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Separated link</a></li>
Mark Otto's avatar
Mark Otto committed
84
85
        </ul>
      </li>
86
      <li role="presentation" class="dropdown">
87
        <a id="drop5" href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
Mark Otto's avatar
Mark Otto committed
88
          Dropdown
89
          <span class="caret"></span>
Mark Otto's avatar
Mark Otto committed
90
        </a>
Mark Otto's avatar
Mark Otto committed
91
        <ul id="menu2" class="dropdown-menu" role="menu" aria-labelledby="drop5">
92
93
94
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Action</a></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Another action</a></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Something else here</a></li>
Mark Otto's avatar
Mark Otto committed
95
          <li role="presentation" class="divider"></li>
96
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Separated link</a></li>
Mark Otto's avatar
Mark Otto committed
97
98
        </ul>
      </li>
99
      <li role="presentation" class="dropdown">
100
        <a id="drop6" href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
Mark Otto's avatar
Mark Otto committed
101
          Dropdown
102
          <span class="caret"></span>
Mark Otto's avatar
Mark Otto committed
103
        </a>
Mark Otto's avatar
Mark Otto committed
104
        <ul id="menu3" class="dropdown-menu" role="menu" aria-labelledby="drop6">
105
106
107
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Action</a></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Another action</a></li>
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Something else here</a></li>
Mark Otto's avatar
Mark Otto committed
108
          <li role="presentation" class="divider"></li>
109
          <li role="presentation"><a role="menuitem" tabindex="-1" href="https://twitter.com/fat">Separated link</a></li>
Mark Otto's avatar
Mark Otto committed
110
111
112
113
114
115
116
        </ul>
      </li>
    </ul> <!-- /pills -->
  </div> <!-- /example -->


  <h2 id="dropdowns-usage">Usage</h2>
117
  <p>Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the <code>.open</code> class on the parent list item.</p>
118
119
  <p>On mobile devices, opening a dropdown adds a <code>.dropdown-backdrop</code> as a tap area for closing dropdown menus when tapping outside the menu, a requirement for proper iOS support. <strong class="text-danger">This means that switching from an open dropdown menu to a different dropdown menu requires an extra tap on mobile.</strong></p>
  <p>Note: The <code>data-toggle="dropdown"</code> attribute is relied on for closing dropdown menus at an application level, so it's a good idea to always use it.</p>
Mark Otto's avatar
Mark Otto committed
120
121
122
123
124

  <h3>Via data attributes</h3>
  <p>Add <code>data-toggle="dropdown"</code> to a link or button to toggle a dropdown.</p>
{% highlight html %}
<div class="dropdown">
125
  <button id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
126
    Dropdown trigger
127
    <span class="caret"></span>
128
  </button>
Mark Otto's avatar
Mark Otto committed
129
130
131
132
133
  <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
    ...
  </ul>
</div>
{% endhighlight %}
Mark Otto's avatar
typos    
Mark Otto committed
134
  <p>To keep URLs intact with link buttons, use the <code>data-target</code> attribute instead of <code>href="#"</code>.</p>
Mark Otto's avatar
Mark Otto committed
135
136
{% highlight html %}
<div class="dropdown">
137
  <a id="dLabel" data-target="#" href="http://example.com" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
Mark Otto's avatar
typos    
Mark Otto committed
138
    Dropdown trigger
139
    <span class="caret"></span>
Mark Otto's avatar
typos    
Mark Otto committed
140
  </a>
Mark Otto's avatar
Mark Otto committed
141
142
143
144
145
146
147
148
149
150
151
152

  <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
    ...
  </ul>
</div>
{% endhighlight %}

  <h3>Via JavaScript</h3>
  <p>Call the dropdowns via JavaScript:</p>
{% highlight js %}
$('.dropdown-toggle').dropdown()
{% endhighlight %}
Chris Rebert's avatar
Chris Rebert committed
153
  <div class="bs-callout bs-callout-info" id="callout-dropdowns-data-required">
Mark Otto's avatar
Mark Otto committed
154
155
156
157
    <h4><code>data-toggle="dropdown"</code> still required</h4>
    <p>Regardless of whether you call your dropdown via JavaScript or instead use the data-api, <code>data-toggle="dropdown"</code> is always required to be present on the dropdown's trigger element.</p>
  </div>

158
  <h3 id="dropdowns-options">Options</h3>
Mark Otto's avatar
Mark Otto committed
159
160
  <p><em>None</em></p>

161
  <h3 id="dropdowns-methods">Methods</h3>
Mark Otto's avatar
Mark Otto committed
162
163
164
  <h4>$().dropdown('toggle')</h4>
  <p>Toggles the dropdown menu of a given navbar or tabbed navigation.</p>

165
  <h3 id="dropdowns-events">Events</h3>
Mark Otto's avatar
Mark Otto committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  <p>All dropdown events are fired at the <code>.dropdown-menu</code>'s parent element.</p>
  <div class="table-responsive">
    <table class="table table-bordered table-striped">
      <thead>
        <tr>
          <th style="width: 150px;">Event Type</th>
          <th>Description</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>show.bs.dropdown</td>
          <td>This event fires immediately when the show instance method is called. The toggling anchor element is available as the <code>relatedTarget</code> property of the event.</td>
        </tr>
        <tr>
          <td>shown.bs.dropdown</td>
          <td>This event is fired when the dropdown has been made visible to the user (will wait for CSS transitions, to complete).  The toggling anchor element is available as the <code>relatedTarget</code> property of the event.</td>
        </tr>
        <tr>
          <td>hide.bs.dropdown</td>
          <td>This event is fired immediately when the hide instance method has been called. The toggling anchor element is available as the <code>relatedTarget</code> property of the event.</td>
        </tr>
        <tr>
          <td>hidden.bs.dropdown</td>
          <td>This event is fired when the dropdown has finished being hidden from the user (will wait for CSS transitions, to complete). The toggling anchor element is available as the <code>relatedTarget</code> property of the event.</td>
        </tr>
      </tbody>
    </table>
  </div><!-- ./bs-table-responsive -->
{% highlight js %}
$('#myDropdown').on('show.bs.dropdown', function () {
  // do something…
})
{% endhighlight %}
</div>