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 **<tabs>** component is the one with the specified **index**. To show the **<tabs>** vertically, set the **vertical** attribute to **true**. 57 58```html 59<!-- xxx.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 **<tab-bar>** to be evenly distributed. Set the **scrollable** attribute to disable scrolling of the **<tab-content>**. 81 82```html 83<!-- xxx.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 **<tabs>** component. 108 109```html 110<!-- xxx.hml --> 111<div class="container"> 112 <tabs class="tabs"> 113 <tab-bar class="tabBar"> 114 <text class="tabBarItem">item1</text> 115 <text class="tabBarItem">item2</text> 116 </tab-bar> 117 <tab-content class="tabContent"> 118 <div class="tabContent"> 119 <text>content1</text> 120 </div> 121 <div class="tabContent" > 122 <text>content2</text> 123 </div> 124 </tab-content> 125 </tabs> 126</div> 127``` 128 129```css 130/* xxx.css */ 131.container { 132 flex-direction: column; 133 justify-content: flex-start; 134 align-items: center; 135 background-color:#F1F3F5; 136} 137.tabs{ 138 margin-top: 20px; 139 border: 1px solid #2262ef; 140 width: 99%; 141 padding: 10px; 142} 143.tabBar{ 144 width: 100%; 145 border: 1px solid #78abec; 146} 147.tabContent{ 148 width: 100%; 149 margin-top: 10px; 150 height: 300px; 151 color: blue; 152 justify-content: center; 153 align-items: center; 154} 155``` 156 157![en-us_image_0000001267767857](figures/en-us_image_0000001267767857.gif) 158 159 160## Displaying the Tab Index 161 162Add the **change** event for the **<tabs>** component to display the index of the current tab after tab switching. 163 164```html 165<!-- xxx.hml --> 166<div class="container" style="background-color:#F1F3F5;"> 167 <tabs class="tabs" onchange="tabChange"> 168 <tab-bar class="tabBar"> 169 <text class="tabBarItem">item1</text> 170 <text class="tabBarItem">item2</text> 171 </tab-bar> 172 <tab-content class="tabContent"> 173 <div> 174 <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image> 175 </div> 176 <div> 177 <image src="common/images/img1.jpg" style="object-fit: contain;"> </image> 178 </div> 179 </tab-content> 180 </tabs> 181</div> 182``` 183 184```js 185// xxx.js 186import promptAction from '@ohos.promptAction'; 187export default { 188 tabChange(e){ 189 promptAction.showToast({ 190 message: "Tab index: " + e.index 191 }) 192 } 193} 194``` 195 196![en-us_image_0000001222807772](figures/en-us_image_0000001222807772.gif) 197 198 199> **NOTE** 200> 201> A **<tabs>** can wrap at most one [**<tab-bar>**](../reference/arkui-js/js-components-container-tab-bar.md) and at most one [**<tab-content>**](../reference/arkui-js/js-components-container-tab-content.md). 202 203 204## Example Scenario 205 206In this example, you can switch between tabs and the active tab has the title text in red with an underline below. 207 208Use the **<tabs>**, **<tab-bar>**, and **<tab-content>** 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. 209 210```html 211<!-- xxx.hml --> 212<div class="container"> 213 <tabs onchange="changeTabactive"> 214 <tab-content> 215 <div class="item-container" for="datas.list"> 216 <div if="{{$item.title=='List1'?true:false}}"> 217 <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image> 218 </div> 219 <div if="{{$item.title=='List2'?true:false}}"> 220 <image src="common/images/img1.jpg" style="object-fit: none;"> </image> 221 </div> 222 <div if="{{$item.title=='List3'?true:false}}"> 223 <image src="common/images/img2.jpg" style="object-fit: contain;"> </image> 224 </div> 225 </div> 226 </tab-content> 227 <tab-bar class="tab_bar mytabs" mode="scrollable"> 228 <div class="tab_item" for="datas.list"> 229 <text style="color: {{$item.color}};">{{$item.title}}</text> 230 <div class="underline-show" if="{{$item.show}}"></div> 231 <div class="underline-hide" if="{{!$item.show}}"></div> 232 </div> 233 </tab-bar> 234 </tabs> 235</div> 236``` 237 238```css 239/* xxx.css */ 240.container{ 241width: 100%; 242height: 100%; 243background-color:#F1F3F5; 244} 245.tab_bar { 246 width: 100%; 247 height: 150px; 248} 249.tab_item { 250 height: 30%; 251 flex-direction: column; 252 align-items: center; 253} 254.tab_item text { 255 font-size: 32px; 256} 257.item-container { 258 justify-content: center; 259 flex-direction: column; 260} 261.underline-show { 262 height: 2px; 263 width: 160px; 264 background-color: #FF4500; 265 margin-top: 7.5px; 266} 267.underline-hide { 268 height: 2px; 269 margin-top: 7.5px; 270 width: 160px; 271} 272``` 273 274```js 275// xxx.js 276import promptAction from '@ohos.promptAction'; 277export default { 278 data() { 279 return { 280 datas: { 281 color_normal: '#878787', 282 color_active: '#ff4500', 283 show: true, 284 list: [{ 285 i: 0, 286 color: '#ff4500', 287 show: true, 288 title: 'List1' 289 }, { 290 i: 1, 291 color: '#878787', 292 show: false, 293 title: 'List2' 294 }, { 295 i: 2, 296 color: '#878787', 297 show: false, 298 title: 'List3' 299 }] 300 } 301 } 302 }, 303 changeTabactive (e) { 304 for (let i = 0; i < this.datas.list.length; i++) { 305 let element = this.datas.list[i]; 306 element.show = false; 307 element.color = this.datas.color_normal; 308 if (i === e.index) { 309 element.show = true; 310 element.color = this.datas.color_active; 311 } 312 } 313 } 314} 315``` 316 317![en-us_image_tab.gif](figures/en-us_image_tab.gif)