ts-basic-components-slider.md 6.8 KB
Newer Older
Z
zengyawen 已提交
1
# Slider
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

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

Z
zengyawen 已提交
7 8 9 10 11

The **&lt;Slider&gt;** component is used to quickly adjust settings, such as the volume and brightness.


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17 18 19

None

Z
zengyawen 已提交
20 21 22 23 24 25

## APIs

Slider(value:{value?: number, min?: number, max?: number, step?: number, style?: SliderStyle, direction?: Axis})

- Parameters
H
HelloCrease 已提交
26 27 28 29 30 31 32 33 34
    | Name                   | Type                                    | Mandatory | Default Value      | Description                              |
    | ---------------------- | --------------------------------------- | --------- | ------------------ | ---------------------------------------- |
    | value                  | number                                  | No        | 0                  | Current progress.                        |
    | min                    | number                                  | No        | 0                  | Minimum value.                           |
    | max                    | number                                  | No        | 100                | Maximum value.                           |
    | step                   | number                                  | No        | 1                  | Step of the slider. When the corresponding step is set, the slider slides intermittently. |
    | style                  | SliderStyle                             | No        | SliderStyle.OutSet | Style of the slider.                     |
    | direction<sup>8+</sup> | [Axis](ts-appendix-enums.md#axis-enums) | No        | Axis.Horizontal    | Whether the slider moves horizontally or vertically. |
    | reverse<sup>8+</sup>   | boolean                                 | No        | false              | Whether the slider values are reversed.  |
Z
zengyawen 已提交
35 36

- SliderStyle enums
H
HelloCrease 已提交
37 38 39 40
    | Name   | Description                       |
    | ------ | --------------------------------- |
    | OutSet | The slider is on the slider rail. |
    | InSet  | The slider is in the slider rail. |
Z
zengyawen 已提交
41 42 43


## Attributes
Z
zengyawen 已提交
44

Z
zengyawen 已提交
45 46
Touch target configuration is not supported.

47 48 49 50 51 52
| Name          | Type    | Default Value | Description                                                          |
| ------------- | ------- | ------------- | -------------------------------------------------------------------- |
| blockColor    | Color   | -             | Color of the slider.                                                 |
| trackColor    | Color   | -             | Background color of the slider.                                      |
| selectedColor | Color   | -             | Color of the slider rail that has been slid.                         |
| showSteps     | boolean | false         | Whether to display the current step.                                 |
H
HelloCrease 已提交
53
| showTips      | boolean | false         | Whether to display a bubble to indicate the percentage when sliding. |
Z
zengyawen 已提交
54 55 56 57 58 59


## Events

Among all the universal events, only **OnAppear** and **OnDisAppear** are supported.

H
HelloCrease 已提交
60 61
| Name                                     | Description                              |
| ---------------------------------------- | ---------------------------------------- |
E
esterzhou 已提交
62
| onChange(callback: (value: number, mode: SliderChangeMode) =&gt; void) | Callback invoked when the slider slides.<br/>**value**: current progress.<br/>**mode**: dragging state. |
Z
zengyawen 已提交
63 64

- SliderChangeMode enums
H
HelloCrease 已提交
65 66 67 68 69
    | Name   | Description                         |
    | ------ | ----------------------------------- |
    | Begin  | The user starts to drag the slider. |
    | Moving | The user is dragging the slider.    |
    | End    | The user stops dragging the slider. |
Z
zengyawen 已提交
70 71 72 73


## Example

Z
zengyawen 已提交
74 75 76 77 78 79 80

```
@Entry
@Component
struct SliderExample {
  @State outSetValue: number = 40
  @State inSetValue: number = 40
Z
zengyawen 已提交
81 82
  @State outVerticalSetValue: number = 40
  @State inVerticalSetValue: number = 40
Z
zengyawen 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  build() {
    Column({ space: 5 }) {
      Text('slider out set').fontSize(9).fontColor(0xCCCCCC).width('90%')
      Row() {
        Slider({
          value: this.outSetValue,
          min: 0,
          max: 100,
          step: 1,
          style: SliderStyle.OutSet
        })
        .blockColor(Color.Blue)
        .trackColor(Color.Gray)
        .selectedColor(Color.Blue)
        .showSteps(true)
        .showTips(true)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.outSetValue = value
          console.info('value:' + value + 'mode:' + mode.toString())
        })
        Text(this.outSetValue.toFixed(0)).fontSize(16)
      }
      .padding({ top: 50 })
      .width('80%')

      Text('slider in set').fontSize(9).fontColor(0xCCCCCC).width('90%')
      Row() {
        Slider({
          value: this.inSetValue,
          min: 0,
          max: 100,
          step: 1,
          style: SliderStyle.InSet
        })
        .blockColor(0xCCCCCC)
        .trackColor(Color.Black)
        .selectedColor(0xCCCCCC)
        .showSteps(false)
        .showTips(false)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.inSetValue = value
          console.info('value:' + value + 'mode:' + mode.toString())
        })
        Text(this.inSetValue.toFixed(0)).fontSize(16)
      }
      .width('80%')
Z
zengyawen 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

      Row() {
        Column() {
          Text('slider out direction set').fontSize(9).fontColor(0xCCCCCC).width('50%')
          Slider({
            value: this.outVerticalSetValue,
            min: 0,
            max: 100,
            step: 1,
            style: SliderStyle.OutSet,
            direction: Axis.Vertical
          })
          .blockColor(Color.Blue)
          .trackColor(Color.Gray)
          .selectedColor(Color.Blue)
          .showSteps(true)
          .showTips(true)
          .onChange((value: number, mode: SliderChangeMode) => {
            this.outVerticalSetValue = value
            console.info('value:' + value + 'mode:' + mode.toString())
          })
          Text(this.outVerticalSetValue.toFixed(0)).fontSize(16)
        }.width('50%').height(300)

        Column() {
          Text('slider in direction set').fontSize(9).fontColor(0xCCCCCC).width('50%')
          Slider({
            value: this.inVerticalSetValue,
            min: 0,
            max: 100,
            step: 1,
            style: SliderStyle.InSet,
            direction: Axis.Vertical
          })
          .blockColor(0xCCCCCC)
          .trackColor(Color.Black)
          .selectedColor(0xCCCCCC)
          .showSteps(false)
          .showTips(false)
          .onChange((value: number, mode: SliderChangeMode) => {
            this.inVerticalSetValue = value
            console.info('value:' + value + 'mode:' + mode.toString())
          })
          Text(this.inVerticalSetValue.toFixed(0)).fontSize(16)
        }.width('50%').height(300)
      }

Z
zengyawen 已提交
177 178 179 180 181
    }.width('100%').margin({ top: 5 })
  }
}
```

Z
zengyawen 已提交
182
![en-us_image_0000001211898492](figures/en-us_image_0000001211898492.gif)