• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <tabs> Development
2
3
4The **<tabs>** component is a common UI component for navigation. It allows quick access to different functions of an app. For details, see [tabs](../reference/arkui-js/js-components-container-tabs.md).
5
6
7## Creating Tabs
8
9Create a **<tabs>** component in the .hml file under **pages/index**.
10
11```html
12<!-- xxx.hml -->
13<div class="container">
14    <tabs>
15        <tab-bar>
16            <text>item1</text>
17            <text>item2</text>
18        </tab-bar>
19        <tab-content class="tabContent">
20            <div class="text">
21                <text>content1</text>
22            </div>
23            <div class="text">
24                <text>content2</text>
25            </div>
26        </tab-content>
27    </tabs>
28</div>
29```
30
31```css
32/* xxx.css */
33.container {
34  flex-direction: column;
35  justify-content: center;
36  align-items: center;
37  background-color: #F1F3F5;
38}
39.tabContent{
40  width: 100%;
41  height: 100%;
42}
43.text{
44  width: 100%;
45  height: 100%;
46  justify-content: center;
47  align-items: center;
48}
49```
50
51![en-us_image_0000001223287676](figures/en-us_image_0000001223287676.gif)
52
53
54## Setting the Tabs Orientation
55
56By default, the active tab of a **&lt;tabs&gt;** component is the one with the specified **index**. To show the **&lt;tabs&gt;** vertically, set the **vertical** attribute to **true**.
57
58```html
59<!-- index.hml -->
60<div class="container" style="background-color:#F1F3F5;">
61  <tabs index="1"  vertical="true">
62    <tab-bar >
63      <text>item1</text>
64      <text style="margin-top: 50px;">item2</text>
65    </tab-bar>
66    <tab-content>
67      <div>
68        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
69      </div>
70      <div>
71        <image src="common/images/img1.jpg" style="object-fit: contain;"> </image>
72      </div>
73    </tab-content>
74  </tabs>
75</div>
76```
77
78![en-us_image_0000001222967756](figures/en-us_image_0000001222967756.gif)
79
80Set the **mode** attribute to enable the child components of the **&lt;tab-bar&gt;** to be evenly distributed. Set the **scrollable** attribute to disable scrolling of the **&lt;tab-content&gt;**.
81
82```html
83<!-- index.hml -->
84<div class="container" style="background-color:#F1F3F5;">
85  <tabs style="margin-top: 30px;">
86    <tab-bar mode="fixed">
87      <text>item1</text>
88      <text>item2</text>
89    </tab-bar>
90    <tab-content scrollable="false">
91      <div>
92        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
93      </div>
94      <div>
95        <image src="common/images/img2.jpg" style="object-fit: contain;"> </image>
96      </div>
97    </tab-content>
98  </tabs>
99</div>
100```
101
102![en-us_image_0000001267647857](figures/en-us_image_0000001267647857.gif)
103
104
105## Setting Styles
106
107Set the background color, border, and tab-content layout of the **&lt;tabs&gt;** component.
108```html
109<!-- index.hml -->
110<div class="container">
111  <tabs class="tabs">
112    <tab-bar class="tabBar">
113      <text class="tabBarItem">item1</text>
114      <text class="tabBarItem">item2</text>
115    </tab-bar>
116    <tab-content class="tabContent">
117      <div class="tabContent">
118        <text>content1</text>
119      </div>
120      <div class="tabContent" >
121        <text>content2</text>
122      </div>
123    </tab-content>
124  </tabs>
125</div>
126```
127
128```css
129/* xxx.css */
130.container {
131  flex-direction: column;
132  justify-content: flex-start;
133  align-items: center;
134 background-color:#F1F3F5;
135}
136.tabs{
137  margin-top: 20px;
138 border: 1px solid #2262ef;
139  width: 99%;
140  padding: 10px;
141}
142.tabBar{
143  width: 100%;
144  border: 1px solid #78abec;
145}
146.tabContent{
147  width: 100%;
148  margin-top: 10px;
149  height: 300px;
150  color: blue;
151  justify-content: center;
152  align-items: center;
153}
154```
155
156![en-us_image_0000001267767857](figures/en-us_image_0000001267767857.gif)
157
158
159## Displaying the Tab Index
160
161Add the **change** event for the **&lt;tabs&gt;** component to display the index of the current tab after tab switching.
162
163```html
164<!-- index.hml -->
165<div class="container" style="background-color:#F1F3F5;">
166  <tabs class="tabs" onchange="tabChange">
167    <tab-bar class="tabBar">
168      <text class="tabBarItem">item1</text>
169      <text class="tabBarItem">item2</text>
170    </tab-bar>
171    <tab-content class="tabContent">
172      <div>
173        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
174      </div>
175      <div>
176        <image src="common/images/img1.jpg" style="object-fit: contain;"> </image>
177      </div>
178    </tab-content>
179  </tabs>
180</div>
181```
182
183```js
184/* index.js */
185import prompt from '@system.prompt';
186export default {
187  tabChange(e){
188    prompt.showToast({
189      message: "Tab index: " + e.index
190    })
191  }
192}
193```
194
195![en-us_image_0000001222807772](figures/en-us_image_0000001222807772.gif)
196
197
198> **NOTE**
199>
200> A **&lt;tabs&gt;** can wrap at most one [**&lt;tab-bar&gt;**](../reference/arkui-js/js-components-container-tab-bar.md) and at most one [**&lt;tab-content&gt;**](../reference/arkui-js/js-components-container-tab-content.md).
201
202
203## Example Scenario
204
205In this example, you can switch between tabs and the active tab has the title text in red with an underline below.
206
207Use the **&lt;tabs&gt;**, **&lt;tab-bar&gt;**, and **&lt;tab-content&gt;** components to implement tab switching. Then define the arrays and attributes. Add the **change** event to change the attribute values in the arrays so that the active tab has a different font color and an underline.
208
209```html
210<!-- index.hml -->
211<div class="container">
212  <tabs onchange="changeTabactive">
213    <tab-content>
214      <div class="item-container" for="datas.list">
215        <div if="{{$item.title=='List1'?true:false}}">
216          <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
217        </div>
218        <div if="{{$item.title=='List2'?true:false}}">
219          <image src="common/images/img1.jpg" style="object-fit: none;"> </image>
220        </div>
221        <div if="{{$item.title=='List3'?true:false}}">
222          <image src="common/images/img2.jpg" style="object-fit: contain;"> </image>
223        </div>
224      </div>
225    </tab-content>
226    <tab-bar class="tab_bar mytabs" mode="scrollable">
227      <div class="tab_item" for="datas.list">
228        <text style="color: {{$item.color}};">{{$item.title}}</text>
229        <div class="underline-show" if="{{$item.show}}"></div>
230        <div class="underline-hide" if="{{!$item.show}}"></div>
231      </div>
232    </tab-bar>
233  </tabs>
234</div>
235```
236
237```css
238/* xxx.css */
239.container{
240width: 100%;
241height: 100%;
242background-color:#F1F3F5;
243}
244.tab_bar {
245  width: 100%;
246  height: 150px;
247}
248.tab_item {
249  height: 30%;
250  flex-direction: column;
251  align-items: center;
252}
253.tab_item text {
254  font-size: 32px;
255}
256.item-container {
257  justify-content: center;
258  flex-direction: column;
259}
260.underline-show {
261  height: 2px;
262  width: 160px;
263  background-color: #FF4500;
264  margin-top: 7.5px;
265}
266.underline-hide {
267  height: 2px;
268  margin-top: 7.5px;
269  width: 160px;
270}
271```
272
273```js
274/* index.js */
275import prompt from '@system.prompt';
276export default {
277  data() {
278    return {
279      datas: {
280        color_normal: '#878787',
281        color_active: '#ff4500',
282        show: true,
283        list: [{
284          i: 0,
285          color: '#ff4500',
286          show: true,
287          title: 'List1'
288        }, {
289          i: 1,
290          color: '#878787',
291          show: false,
292          title: 'List2'
293        }, {
294           i: 2,
295           color: '#878787',
296           show: false,
297           title: 'List3'
298        }]
299      }
300    }
301  },
302  changeTabactive (e) {
303    for (let i = 0; i < this.datas.list.length; i++) {
304      let element = this.datas.list[i];
305      element.show = false;
306      element.color = this.datas.color_normal;
307      if (i === e.index) {
308        element.show = true;
309        element.color = this.datas.color_active;
310      }
311    }
312  }
313}
314```
315
316![en-us_image_tab.gif](figures/en-us_image_tab.gif)