slider开发指导
==========

slider为滑动条组件，用来快速调节音量、亮度等。具体用法请参考[slider](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-components-basic-slider)。

#### 创建slider组件

在pages/index目录下的hml文件中创建一个slider组件。

<!-- xxx.hml -->
<div class="container">
  <slider></slider>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  background-color: #F1F3F5;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/24/v3/ay4ZmJHeTAGyEJxGVg2_VQ/zh-cn_image_0000002634008746.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=8F95421FEC50888F941EDC8795FFA35644179FBA2A389736E358DBA36B076DA9)

#### 设置样式和属性

slider组件通过color、selected-color、block-color样式分别为滑动条设置背景颜色、已选择颜色和滑块颜色。

<!-- xxx.hml -->
<div class="container">
  <slider class= "sli"></slider>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.sli{
  color: #fcfcfc;
  scrollbar-color: aqua;
  background-color: #b7e3f3;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/11/v3/YssaG4kiTP24zxETdpxPrA/zh-cn_image_0000002664207893.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=A1FB0CC5931F23FCC6472294CCE4425F01938E60A0726AAAD73B00AA6000E751)

通过添加min、max、value、step、mode属性分别为滑动条设置最小值、最大值、初始值、滑动步长和滑动条样式。

<!-- xxx.hml -->
<div class="container">
  <slider min="0" max="100" value="1" step="2" mode="inset" showtips="true"></slider>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/82/v3/U8fZxRSEScaqXkX-ZF5hIA/zh-cn_image_0000002664327953.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=8A221C23AB0DC605E527DF5F66F34FEA303B505DFF173F392685A078AA99CD9B)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/2a/v3/i1GsrVYLSLyW7X5RWM1WcQ/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=00BC38F00182477BE9816F34B3E0A92B1FCF96E43351161D87F38A23B2AE0B6B)

mode属性为滑动条样式，可选值为：

*   outset：滑块在滑杆上。
    
*   inset：滑块在滑杆内。
    

#### 绑定事件

向slider组件添加change事件，添加时需要传入ChangeEvent参数。

<!-- 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>
  <slider min="0" max="100" value="{{value}}" onchange="setValue"></slider>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}

// 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;
    }
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/70/v3/5JaJSH3iTcC0Mr2XUwWKwA/zh-cn_image_0000002633848844.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=6F83B4A24AF7F6C475EBCA698E19B07194473D156976942595999B476BFEE71E)

#### 场景示例

开发者可以通过调整滑动条的值来改变图片大小，并且动态打印当前图片的宽和高。

<!-- xxx.hml -->
<div class="container">
  <image src="common/landscape3.jpg" style=" width: {{WidthVal}}px;height:{{HeightVal}}px;margin-top: -150px;"></image>
  <div class="txt">
    <slider min="0" max="100" value="{{value}}" onchange="setValue"></slider>
    <text>The width of this picture is {{WidthVal}}</text>
    <text>The height of this picture is {{HeightVal}}</text>
  </div>
</div>

/\* xxx.css \*/
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
.text{
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 65%;
}
.text{
  margin-top: 30px;
}

// xxx.js
export default{
  data: {
    value: 0,
    WidthVal: 200,
    HeightVal: 200
  },
  setValue(e) {
    this.WidthVal = 200 + e.value;
    this.HeightVal = 200 + e.value
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/61/v3/x4FfueVjSXWjb9rXUZEr-g/zh-cn_image_0000002634008748.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132321Z&HW-CC-Expire=86400&HW-CC-Sign=9D36A1B492ED6BD7D91780895EC3D1D432B52D42DF4E8F3AD5C765526CAA9D81)