• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# tabs开发指导
2
3tabs是一种常见的界面导航结构。通过页签容器,用户可以快捷地访问应用的不同模块。具体用法请参考[tabs API](../reference/arkui-js/js-components-container-tabs.md)。
4
5
6## 创建tabs
7
8pages/index目录下的hml文件中创建一个tabs组件。
9
10```html
11<!-- xxx.hml -->
12<div class="container">
13    <tabs>
14        <tab-bar>
15            <text>item1</text>
16            <text>item2</text>
17        </tab-bar>
18        <tab-content class="tabContent">
19            <div class="text">
20                <text>content1</text>
21            </div>
22            <div class="text">
23                <text>content2</text>
24            </div>
25        </tab-content>
26    </tabs>
27</div>
28```
29
30```css
31/* xxx.css */
32.container {
33  flex-direction: column;
34  justify-content: center;
35  align-items: center;
36  background-color: #F1F3F5;
37}
38.tabContent{
39  width: 100%;
40  height: 100%;
41}
42.text{
43  width: 100%;
44  height: 100%;
45  justify-content: center;
46  align-items: center;
47}
48```
49
50![zh-cn_image_0000001165191390](figures/zh-cn_image_0000001165191390.gif)
51
52
53## 设置tabs方向
54
55tabs默认展示索引为index的标签及内容。通过设置vertical属性使组件纵向展示。
56
57```html
58<!-- xxx.hml -->
59<div class="container" style="background-color:#F1F3F5;">
60  <tabs index="1"  vertical="true">
61    <tab-bar >
62      <text>item1</text>
63      <text style="margin-top: 50px;">item2</text>
64    </tab-bar>
65    <tab-content>
66      <div>
67        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
68      </div>
69      <div>
70        <image src="common/images/img1.jpg" style="object-fit: contain;"> </image>
71      </div>
72    </tab-content>
73  </tabs>
74</div>
75```
76
77![zh-cn_image_0000001208908643](figures/zh-cn_image_0000001208908643.gif)
78
79设置mode属性使tab-bar的子组件均分,设置scrollable属性使tab-content不可进行左右滑动切换内容。
80
81```html
82<!-- xxx.hml -->
83<div class="container" style="background-color:#F1F3F5;">
84  <tabs style="margin-top: 30px;">
85    <tab-bar mode="fixed">
86      <text>item1</text>
87      <text>item2</text>
88    </tab-bar>
89    <tab-content scrollable="false">
90      <div>
91        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
92      </div>
93      <div>
94        <image src="common/images/img2.jpg" style="object-fit: contain;"> </image>
95      </div>
96    </tab-content>
97  </tabs>
98</div>
99```
100
101![zh-cn_image_0000001209028575](figures/zh-cn_image_0000001209028575.gif)
102
103
104## 设置样式
105
106设置tabs背景色及边框和tab-content布局。
107```html
108<!-- xxx.hml -->
109<div class="container">
110  <tabs class="tabs">
111    <tab-bar class="tabBar">
112      <text class="tabBarItem">item1</text>
113      <text class="tabBarItem">item2</text>
114    </tab-bar>
115    <tab-content class="tabContent">
116      <div class="tabContent">
117        <text>content1</text>
118      </div>
119      <div class="tabContent" >
120        <text>content2</text>
121      </div>
122    </tab-content>
123  </tabs>
124</div>
125```
126
127```css
128/* xxx.css */
129.container {
130  flex-direction: column;
131  justify-content: flex-start;
132  align-items: center;
133 background-color:#F1F3F5;
134}
135.tabs{
136  margin-top: 20px;
137 border: 1px solid #2262ef;
138  width: 99%;
139  padding: 10px;
140}
141.tabBar{
142  width: 100%;
143  border: 1px solid #78abec;
144}
145.tabContent{
146  width: 100%;
147  margin-top: 10px;
148  height: 300px;
149  color: blue;
150  justify-content: center;
151  align-items: center;
152}
153```
154
155![zh-cn_image_0000001163388642](figures/zh-cn_image_0000001163388642.gif)
156
157
158## 显示页签索引
159
160开发者可以为tabs添加change事件,实现页签切换后显示当前页签索引的功能。
161
162```html
163<!-- xxx.hml -->
164<div class="container" style="background-color:#F1F3F5;">
165  <tabs class="tabs" onchange="tabChange">
166    <tab-bar class="tabBar">
167      <text class="tabBarItem">item1</text>
168      <text class="tabBarItem">item2</text>
169    </tab-bar>
170    <tab-content class="tabContent">
171      <div>
172        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
173      </div>
174      <div>
175        <image src="common/images/img1.jpg" style="object-fit: contain;"> </image>
176      </div>
177    </tab-content>
178  </tabs>
179</div>
180```
181
182```js
183// xxx.js
184import promptAction from '@ohos.promptAction';
185export default {
186  tabChange(e){
187    promptAction.showToast({
188      message: "Tab index: " + e.index
189    })
190  }
191}
192```
193
194![zh-cn_image_0000001163228638](figures/zh-cn_image_0000001163228638.gif)
195
196
197> **说明:**
198>
199> tabs子组件仅支持一个[\<tab-bar>](../reference/arkui-js/js-components-container-tab-bar.md)和一个[\<tab-content>](../reference/arkui-js/js-components-container-tab-content.md)。
200
201
202## 场景示例
203
204在本场景中,开发者可以点击标签切换内容,选中后标签文字颜色变红,并显示下划线。
205
206用tabs、tab-bar和tab-content实现点击切换功能,再定义数组,设置属性。使用change事件改变数组内的属性值实现变色及下划线的显示。
207
208```html
209<!-- xxx.hml -->
210<div class="container">
211  <tabs onchange="changeTabactive">
212    <tab-content>
213      <div class="item-container" for="datas.list">
214        <div if="{{$item.title=='List1'?true:false}}">
215          <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
216        </div>
217        <div if="{{$item.title=='List2'?true:false}}">
218          <image src="common/images/img1.jpg" style="object-fit: none;"> </image>
219        </div>
220        <div if="{{$item.title=='List3'?true:false}}">
221          <image src="common/images/img2.jpg" style="object-fit: contain;"> </image>
222        </div>
223      </div>
224    </tab-content>
225    <tab-bar class="tab_bar mytabs" mode="scrollable">
226      <div class="tab_item" for="datas.list">
227        <text style="color: {{$item.color}};">{{$item.title}}</text>
228        <div class="underline-show" if="{{$item.show}}"></div>
229        <div class="underline-hide" if="{{!$item.show}}"></div>
230      </div>
231    </tab-bar>
232  </tabs>
233</div>
234```
235
236```css
237/* xxx.css */
238.container{
239width: 100%;
240height: 100%;
241background-color:#F1F3F5;
242}
243.tab_bar {
244  width: 100%;
245  height: 150px;
246}
247.tab_item {
248  height: 30%;
249  flex-direction: column;
250  align-items: center;
251}
252.tab_item text {
253  font-size: 32px;
254}
255.item-container {
256  justify-content: center;
257  flex-direction: column;
258}
259.underline-show {
260  height: 2px;
261  width: 160px;
262  background-color: #FF4500;
263  margin-top: 7.5px;
264}
265.underline-hide {
266  height: 2px;
267  margin-top: 7.5px;
268  width: 160px;
269}
270```
271
272```js
273// xxx.js
274export default {
275  data() {
276    return {
277      datas: {
278        color_normal: '#878787',
279        color_active: '#ff4500',
280        show: true,
281        list: [{
282          i: 0,
283          color: '#ff4500',
284          show: true,
285          title: 'List1'
286        }, {
287          i: 1,
288          color: '#878787',
289          show: false,
290          title: 'List2'
291        }, {
292           i: 2,
293           color: '#878787',
294           show: false,
295           title: 'List3'
296        }]
297      }
298    }
299  },
300  changeTabactive (e) {
301    for (let i = 0; i < this.datas.list.length; i++) {
302      let element = this.datas.list[i];
303      element.show = false;
304      element.color = this.datas.color_normal;
305      if (i === e.index) {
306        element.show = true;
307        element.color = this.datas.color_active;
308      }
309    }
310  }
311}
312```
313
314![zh-cn_image_tab.gif](figures/zh-cn_image_tab.gif)
315
316
317## 相关实例
318
319针对tabs开发,有以下相关实例可供参考:
320
321- [`Tabs`:页签容器(JS)(API8)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-3.2-Release/UI/Tabs)