Slider滑动输入条

允许用户在一个区间中选择特定值,eg:控制屏幕的显示亮度。

规则

  • 默认状态下,左边为最小值,右边为最大值。
  • 一般水平放置。

Common API

参数说明类型默认值
[min]最小值number0
[max]最大值number100
[step]步长,取值必须大于 0,并且可被 (max - min) 整除。当 marks 不为空对象时,可以设置 stepnull,此时 Slider 的可选值仅有 marks 标出来的部分number | null1
[value]设置当前取值number-
[defaultValue]设置初始取值number0
[disabled]值为 true 时,滑块为禁用状态booleanfalse
[marks]刻度标记,key 的类型必须为 number 且取值在闭区间 [min, max] 内{ [_: number]: string }{ }
[handleStyle]滑块的样式object-
[trackStyle]选中部分滑动条的样式object-
[railStyle]未选中部分object-
[(ngModel)]设置当前取值number-
(ngModelChange)当 Slider 的值发生改变时,会触发 ngModelChange 事件,并把改变后的值作为参数传入EventEmitter<number>-
(onChange)当 Slider 的值发生改变时,会触发 onChange 事件,并把改变后的值作为参数传入EventEmitter<number>-
(onAfterChange)ontouchend 触发时机一致,把当前值作为参数传入EventEmitter<number>-

代码演示

基本用法

最简单的用法。

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'demo-slider-basic',
  4. template: `
  5. <div class="am-demo-page">
  6. <div style="padding: 15px;font-size: 16px;">步骤条</div>
  7. <div class="am-demo-bd am-wingblank am-wingblank-lg">
  8. <div>
  9. <div class="sub-title">Small size ngModel</div>
  10. </div>
  11. <div>
  12. <Slider
  13. [ngModel]="value"
  14. [min]="-10"
  15. [max]="100"
  16. (ngModelChange)="change($event)"
  17. (onAfterChange)="afterChange($event)"
  18. ></Slider>
  19. </div>
  20. </div>
  21. <div class="am-demo-bd am-wingblank am-wingblank-lg">
  22. <div>
  23. <div class="sub-title">Small size</div>
  24. </div>
  25. <div>
  26. <Slider
  27. [defaultValue]="26"
  28. [min]="-10"
  29. [max]="100"
  30. (onChange)="change($event)"
  31. (onAfterChange)="afterChange($event)"
  32. ></Slider>
  33. </div>
  34. </div>
  35. <div class="am-demo-bd am-wingblank am-wingblank-lg">
  36. <div>
  37. <div class="sub-title">Disabled slider</div>
  38. </div>
  39. <div>
  40. <Slider [defaultValue]="26" [min]="0" [max]="30" [disabled]="true"></Slider>
  41. </div>
  42. </div>
  43. <div class="am-demo-bd am-wingblank am-wingblank-lg">
  44. <div>
  45. <div class="sub-title">slider with customized color</div>
  46. </div>
  47. <div>
  48. <Slider
  49. [defaultValue]="26"
  50. [min]="0"
  51. [max]="30"
  52. [trackStyle]="trackStyle"
  53. [railStyle]="railStyle"
  54. [handleStyle]="handleStyle"
  55. ></Slider>
  56. </div>
  57. </div>
  58. </div>
  59. `,
  60. styles: [
  61. `
  62. .am-wingblank,
  63. .am-wingblank-lg {
  64. margin-left: 15px;
  65. margin-right: 15px;
  66. margin-bottom: 30px;
  67. }
  68. .sub-title {
  69. color: #888;
  70. font-size: 14px;
  71. padding: 30px 0 18px 0;
  72. margin: 0;
  73. }
  74. `
  75. ]
  76. })
  77. export class DemoSliderBasicComponent implements OnInit {
  78. value = 3;
  79. trackStyle;
  80. railStyle;
  81. handleStyle;
  82. marks;
  83. constructor() {}
  84. change(event) {
  85. console.log(event, 'change');
  86. }
  87. afterChange(event) {
  88. console.log(event, 'afterChange');
  89. }
  90. ngOnInit() {
  91. this.trackStyle = {
  92. 'background-color': 'red',
  93. height: '5px'
  94. };
  95. this.railStyle = {
  96. 'background-color': 'blue',
  97. height: '5px'
  98. };
  99. this.handleStyle = {
  100. 'border-color': 'blue',
  101. height: '14px',
  102. width: '14px',
  103. 'margin-left': '-7px',
  104. 'margin-top': '-4.5px',
  105. 'background-color': 'blue'
  106. };
  107. this.marks = {
  108. '-10': '-10°C',
  109. 0: `<strong>0°C</strong>`,
  110. 26: '26°C',
  111. 37: '37°C',
  112. 50: '50°C',
  113. 100: {
  114. style: {
  115. color: 'red'
  116. },
  117. label: `<strong>100°C</strong>`
  118. }
  119. };
  120. }
  121. }

Slider滑动输入条 - 图1