js-components-basic-slider.md 3.2 KB
Newer Older
E
ester.zhou 已提交
1
# slider
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

E
ester.zhou 已提交
7 8 9 10
The **\<Slider>** component is used to quickly adjust settings, such as the volume and brightness.


## Child Components
Z
zengyawen 已提交
11 12 13 14

Not supported


E
ester.zhou 已提交
15
## Attributes
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
In addition to the [universal attributes](../arkui-js/js-components-common-attributes.md), the following attributes are supported.
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20 21 22 23 24 25 26 27
| Name| Type| Default Value| Mandatory| Description|
| -------- | -------- | -------- | -------- | -------- |
| min | number | 0 | No| Minimum value of the slider.|
| max | number | 100 | No| Maximum value of the slider.|
| step | number | 1 | No| Step of each slide.|
| value | number | 0 | No| Initial value of the slider.|
| mode<sup>5+</sup> | string | outset | No| Slider style. Available values are as follows:<br>- **outset**: The slider is on the sliding bar.<br>- **inset**: The slider is inside the sliding bar.|
| showsteps<sup>5+</sup> | boolean | false | No| Whether to display slider scales.|
| showtips<sup>5+</sup> | boolean | false | No| Whether a tooltip is displayed to show the percentage value on the slider.|
Z
zengyawen 已提交
28 29


E
ester.zhou 已提交
30
## Styles
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32
In addition to the [universal styles](../arkui-js/js-components-common-styles.md), the following styles are supported.
Z
zengyawen 已提交
33

E
ester.zhou 已提交
34 35 36 37 38
| Name| Type| Default Value| Mandatory| Description|
| -------- | -------- | -------- | -------- | -------- |
| color | &lt;color&gt; | #19000000 | No| Background color of the slider.|
| selected-color | &lt;color&gt; | #ff007dff | No| Selected color of the slider.|
| block-color | &lt;color&gt; | \#ffffff | No| Slider color.|
Z
zengyawen 已提交
39 40


E
ester.zhou 已提交
41
## Events
Z
zengyawen 已提交
42

E
ester.zhou 已提交
43
In addition to the [universal events](../arkui-js/js-components-common-events.md), the following events are supported.
Z
zengyawen 已提交
44

E
ester.zhou 已提交
45 46 47
| Name| Parameter| Description|
| -------- | -------- | -------- |
| change | ChangeEvent | Triggered when the value changes.|
Z
zengyawen 已提交
48

E
ester.zhou 已提交
49 50 51 52 53 54 55 56 57 58 59
**Table 1** ChangeEvent

| Attribute| Type| Description|
| -------- | -------- | -------- |
| value<sup>5+</sup> | number | Current value of the slider.|
| mode<sup>5+</sup> | string | Type of the change event. Available values are as follows:<br>- **start**: The **value** starts to change.<br>- **move**: The **value** is changing with users' dragging.<br>- **end**: The **value** stops changing.|


## Example

```html
Z
zengyawen 已提交
60 61 62 63 64
<!-- xxx.hml -->
<div class="container">
  <text>slider start value is {{startValue}}</text>
  <text>slider current value is {{currentValue}}</text>
  <text>slider end value is {{endValue}}</text>
Z
zengyawen 已提交
65
  <slider min="0" max="100" value="{{value}}" onchange="setvalue" ></slider>
Z
zengyawen 已提交
66 67 68
</div>
```

E
ester.zhou 已提交
69
```css
Z
zengyawen 已提交
70 71 72 73 74
/* xxx.css */
.container {
  flex-direction: column;
  justify-content: center;
  align-items: center;
Z
zengyawen 已提交
75
  
Z
zengyawen 已提交
76 77 78
}
```

E
ester.zhou 已提交
79
```js
Z
zengyawen 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
// xxx.js
export default {
  data: {
    value: 0,
    startValue: 0,
    currentValue: 0,
    endValue: 0,
  },
  setvalue(e) {
    if (e.mode == "start") {
      this.value = e.value;
      this.startValue = e.value;
    } else if (e.mode == "move") {
      this.value = e.value;
      this.currentValue = e.value;
    } else if (e.mode == "end") {
      this.value = e.value;
      this.endValue = e.value;
E
ester.zhou 已提交
98 99 100
    } else if (e.mode == "click) {
      this.value = e.value;
      this.currentValue = e.value;
Z
zengyawen 已提交
101 102 103 104 105
    }
  }
}
```

E
ester.zhou 已提交
106
![slider](figures/slider.png)