ui-js-component-tabs.md 7.3 KB
Newer Older
G
ge-yafang 已提交
1
# <tabs> Development
Z
zengyawen 已提交
2 3


G
ge-yafang 已提交
4 5 6 7 8 9
The <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).


## Creating Tabs

Create a <tabs> component in the .hml file under pages/index.
Z
zengyawen 已提交
10 11 12 13 14 15


```
<!-- index.hml -->
<div class="container" >
  <tabs>
G
ge-yafang 已提交
16
   <tab-bar>
Z
zengyawen 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
      <text>item1</text>
      <text>item2</text>
    </tab-bar>
    <tab-content>
      <div class="text">
        <text>content1</text>
      </div>
      <div class="text">
        <text>content2</text>
      </div>
    </tab-content>
  </tabs>
</div>
```

G
ge-yafang 已提交
32

Z
zengyawen 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
```
/* xxx.css */
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.text{
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
}
```

G
ge-yafang 已提交
49 50
![en-us_image_0000001223287676](figures/en-us_image_0000001223287676.gif)

Z
zengyawen 已提交
51

G
ge-yafang 已提交
52 53 54
## Setting the Tabs Orientation

By 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.
Z
zengyawen 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76


```
<!-- index.hml -->
<div class="container" style="background-color:#F1F3F5;">
  <tabs index="1"  vertical="true">
    <tab-bar >
      <text>item1</text>
      <text style="margin-top: 50px;">item2</text>
    </tab-bar>
    <tab-content>
      <div>
        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
      </div>
      <div>
        <image src="common/images/img1.jpg" style="object-fit: contain;"> </image>
      </div>
    </tab-content>
  </tabs>
</div>
```

G
ge-yafang 已提交
77 78 79
![en-us_image_0000001222967756](figures/en-us_image_0000001222967756.gif)

Set 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>.
Z
zengyawen 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101


```
<!-- index.hml -->
<div class="container" style="background-color:#F1F3F5;">
  <tabs style="margin-top: 30px;">
    <tab-bar mode="fixed">
      <text>item1</text>
      <text>item2</text>
    </tab-bar>
    <tab-content scrollable="false">
      <div>
        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
      </div>
      <div>
        <image src="common/images/img2.jpg" style="object-fit: contain;"> </image>
      </div>
    </tab-content>
  </tabs>
</div>
```

G
ge-yafang 已提交
102 103
![en-us_image_0000001267647857](figures/en-us_image_0000001267647857.gif)

Z
zengyawen 已提交
104

G
ge-yafang 已提交
105
## Setting the Style
Z
zengyawen 已提交
106

G
ge-yafang 已提交
107
  Set the background color, border, and tab-content layout of the &lt;tabs&gt; component.
Z
zengyawen 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

```
<!-- index.hml -->
<div class="container">
  <tabs class="tabs">
    <tab-bar class="tabBar">
      <text class="tabBarItem">item1</text>
      <text class="tabBarItem">item2</text>
    </tab-bar>
    <tab-content class="tabContent">
      <div class="tabContent">
        <text>content1</text>
      </div>
      <div class="tabContent" >
        <text>content2</text>
      </div>
    </tab-content>
  </tabs>
</div>
```

G
ge-yafang 已提交
129

Z
zengyawen 已提交
130 131 132 133 134 135
```
/* xxx.css */
.container {
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
G
ge-yafang 已提交
136
 background-color:#F1F3F5;
Z
zengyawen 已提交
137 138 139
}
.tabs{
  margin-top: 20px;
G
ge-yafang 已提交
140
 border: 1px solid #2262ef;
Z
zengyawen 已提交
141 142 143 144 145 146 147 148 149 150 151 152
  width: 99%;
  padding: 10px;
}
.tabBar{
  width: 100%;
  border: 1px solid #78abec;
}
.tabContent{
  width: 100%;
  margin-top: 10px;
  height: 300px;
  color: blue;   
G
ge-yafang 已提交
153
  justify-content: center;  align-items: center;
Z
zengyawen 已提交
154 155 156
}
```

G
ge-yafang 已提交
157 158 159 160
![en-us_image_0000001267767857](figures/en-us_image_0000001267767857.gif)


## Displaying the Tab Index
Z
zengyawen 已提交
161

G
ge-yafang 已提交
162
Add the change event for the &lt;tabs&gt; component to display the index of the current tab after tab switching.
Z
zengyawen 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184


```
<!-- index.hml -->
<div class="container" style="background-color:#F1F3F5;">
  <tabs class="tabs" onchange="tabChange">
    <tab-bar class="tabBar">
      <text class="tabBarItem">item1</text>
      <text class="tabBarItem">item2</text>
    </tab-bar>
    <tab-content class="tabContent">
      <div>
        <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
      </div>
      <div>
        <image src="common/images/img1.jpg" style="object-fit: contain;"> </image>
      </div>
    </tab-content>
  </tabs>
</div>
```

G
ge-yafang 已提交
185

Z
zengyawen 已提交
186 187 188 189 190 191 192 193 194 195 196 197
```
/* index.js */
import prompt from '@system.prompt';
export default {
  tabChange(e){
    prompt.showToast({
      message: "Tab index: " + e.index
    })
  }
}
```

G
ge-yafang 已提交
198 199
![en-us_image_0000001222807772](figures/en-us_image_0000001222807772.gif)

Z
zengyawen 已提交
200

G
ge-yafang 已提交
201 202
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> - A &lt;tabs&gt; 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).
Z
zengyawen 已提交
203

G
ge-yafang 已提交
204 205

## Example Scenario
Z
zengyawen 已提交
206 207 208

In this example, you can switch between tabs and the active tab has the title text in red with an underline below.

G
ge-yafang 已提交
209 210
Use the &lt;tabs&gt;, <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.

Z
zengyawen 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

```
<!-- index.hml -->
<div class="container">
  <tabs onchange="changeTabactive">
    <tab-content>
      <div class="item-container" for="datas.list">
        <div if="{{$item.title=='List1'?true:false}}">
          <image src="common/images/bg-tv.jpg" style="object-fit: contain;"> </image>
        </div>
        <div if="{{$item.title=='List2'?true:false}}">
          <image src="common/images/img1.jpg" style="object-fit: none;"> </image>
        </div>
        <div if="{{$item.title=='List3'?true:false}}">
          <image src="common/images/img2.jpg" style="object-fit: contain;"> </image>
        </div>
      </div>
    </tab-content>
    <tab-bar class="tab_bar mytabs" mode="scrollable">
      <div class="tab_item" for="datas.list">
        <text style="color: {{$item.color}};">{{$item.title}}</text>
        <div class="underline-show" if="{{$item.show}}"></div>
        <div class="underline-hide" if="{{!$item.show}}"></div>
      </div>
    </tab-bar>
  </tabs>
</div>
```

G
ge-yafang 已提交
240

Z
zengyawen 已提交
241 242 243
```
/* xxx.css */
.container{
G
ge-yafang 已提交
244
background-color:#F1F3F5;
Z
zengyawen 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
}
.tab_bar {
  width: 100%;
}
.tab_item {
  flex-direction: column;
  align-items: center;
}
.tab_item text {
  font-size: 32px;
}
.item-container {
  justify-content: center;
  flex-direction: column;
}
.underline-show {
  height: 2px;
  width: 160px;
  background-color: #FF4500;
  margin-top: 7.5px;
}
.underline-hide {
  height: 2px;
  margin-top: 7.5px;
  width: 160px;
}
```

G
ge-yafang 已提交
273

Z
zengyawen 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
```
/* index.js */
import prompt from '@system.prompt';
export default {
  data() {
    return {
      datas: {
        color_normal: '#878787',
        color_active: '#ff4500',
        show: true,
        list: [{
          i: 0,
          color: '#ff4500',
          show: true,
          title: 'List1'
        }, {
          i: 1,
          color: '#878787',
          show: false,
          title: 'List2'
        }, {
           i: 2,
           color: '#878787',
           show: false,
           title: 'List3'
        }]
      }
    }
  },
  changeTabactive (e) {
    for (let i = 0; i < this.datas.list.length; i++) {
      let element = this.datas.list[i];
      element.show = false;
      element.color = this.datas.color_normal;
      if (i === e.index) {
        element.show = true;
        element.color = this.datas.color_active;
      }
    }
  }
}
```

G
ge-yafang 已提交
317
![en-us_image_0000001267607885](figures/en-us_image_0000001267607885.gif)