Slider滑动输入条
当用户需要在数值区间/自定义区间内进行选择时,可为连续或离散值。
想要了解更多关于单独引入组件的内容,可以在快速上手页面进行查看。
基本滑动条。当 为 true
时,渲染为双滑块。当 nzDisabled
为 true
时,滑块处于不可用状态。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-slider-basic',
template: `
<nz-slider [(ngModel)]="value1" [nzDisabled]="disabled"></nz-slider>
<nz-slider nzRange [(ngModel)]="value2" [nzDisabled]="disabled"></nz-slider>
Disabled:
<nz-switch nzSize="small" [(ngModel)]="disabled"></nz-switch>
`
})
export class NzDemoSliderBasicComponent {
disabled = false;
value1 = 30;
value2 = [20, 50];
}
滑块左右可以设置图标来表达业务含义。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'nz-demo-slider-icon-slider',
template: `
<div class="icon-wrapper test-class">
<i nz-icon nzType="frown" [class.icon-highlight]="preHighLight"></i>
<nz-slider [nzMin]="0" [nzMax]="20" [(ngModel)]="sliderValue"></nz-slider>
<i nz-icon nzType="smile" [class.icon-highlight]="nextHighLight"></i>
</div>
`,
styles: [
`
.icon-wrapper {
position: relative;
padding: 0px 30px;
}
[nz-icon] {
position: absolute;
top: -2px;
width: 16px;
height: 16px;
line-height: 1;
font-size: 16px;
color: rgba(0, 0, 0, 0.25);
}
[nz-icon]:first-child {
left: 0;
}
[nz-icon]:last-child {
right: 0;
}
.icon-highlight {
color: rgba(0, 0, 0, 0.45);
}
`
]
})
export class NzDemoSliderIconSliderComponent implements OnInit {
min = 0;
max = 20;
mid = parseFloat(((this.max - this.min) / 2).toFixed(5));
preHighLight = false;
nextHighLight = false;
_sliderValue = 0;
set sliderValue(value: number) {
this._sliderValue = value;
this.highlightIcon();
}
get sliderValue(): number {
return this._sliderValue;
}
ngOnInit(): void {
this.sliderValue = 0;
}
highlightIcon() {
const lower = this._sliderValue >= this.mid;
this.preHighLight = !lower;
this.nextHighLight = lower;
}
}
当 Slider 的值发生改变时,会触发 nzOnChange
事件,并把改变后的值作为参数传入。在 onmouseup
时,会触发 nzOnAfterChange
事件,并把当前值作为参数传入。
垂直方向的 Slider。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-slider-vertical',
template: `
<div [ngStyle]="{ height: '300px' }">
<nz-slider nzVertical [ngModel]="30"></nz-slider>
</div>
<div [ngStyle]="style">
<nz-slider nzVertical nzRange [nzStep]="10" [ngModel]="[20, 50]"></nz-slider>
</div>
<div [ngStyle]="style">
<nz-slider nzVertical nzRange [nzMarks]="marks" [ngModel]="[26, 37]"></nz-slider>
</div>
</div>
`
})
export class NzDemoSliderVerticalComponent {
style = {
float: 'left',
height: '300px',
marginLeft: '70px'
};
marks = {
0: '0°C',
26: '26°C',
37: '37°C',
100: {
style: {
color: '#f50'
},
label: '<strong>100°C</strong>'
}
};
}
和 组件保持同步。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-slider-input-number',
template: `
<nz-row>
<nz-col nzSpan="12">
<nz-slider [nzMin]="1" [nzMax]="20" [(ngModel)]="value1"></nz-slider>
</nz-col>
<div nz-col nzSpan="4">
<nz-input-number
[nzMin]="1"
[nzMax]="20"
[ngStyle]="{ marginLeft: '16px' }"
[(ngModel)]="value1"
></nz-input-number>
</div>
</nz-row>
<nz-row>
<nz-col nzSpan="12">
<nz-slider [nzMin]="0" [nzMax]="1" [nzStep]="0.01" [(ngModel)]="value2"></nz-slider>
</nz-col>
<nz-col nzSpan="4">
[nzMin]="0"
[nzMax]="1"
[ngStyle]="{ marginLeft: '16px' }"
[nzStep]="0.01"
[(ngModel)]="value2"
></nz-input-number>
</nz-col>
</nz-row>
`
})
export class NzDemoSliderInputNumberComponent {
value1 = 1;
value2 = 0;
}
使用 nzTipFormatter
可以格式化 Tooltip
的内容,设置 nzTipFormatter = null
,则隐藏 Tooltip
。
使用 nzMarks
属性标注分段式滑块,使用 ngModel
指定滑块位置。当 nzIncluded = false
时,表明不同标记间为并列关系。当 nzStep = null
时,Slider 的可选值仅有 nzMarks
标出来的部分。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-slider-mark',
template: `
<div>
<h4>included=true</h4>
<nz-slider [nzMarks]="marks" [ngModel]="37"></nz-slider>
<nz-slider [nzMarks]="marks" nzIncluded nzRange [ngModel]="[26, 37]"></nz-slider>
<h4>included=false</h4>
<nz-slider [nzMarks]="marks" [nzIncluded]="false" [ngModel]="37"></nz-slider>
<h4>marks & step</h4>
<nz-slider [nzMarks]="marks" [nzStep]="10" [ngModel]="37"></nz-slider>
<h4>step=null || dots=true</h4>
<nz-slider [nzMarks]="marks" [nzStep]="null" [ngModel]="37"></nz-slider>
<nz-slider [nzMarks]="marks" nzDots [ngModel]="37"></nz-slider>
Change nzMarks dynamically: <button nz-button (click)="changeMarks()">Change nzMarks</button>
</div>
`,
`
h4 {
margin: 0 0 16px;
}
.ant-slider-with-marks {
margin-bottom: 44px;
}
`
]
})
export class NzDemoSliderMarkComponent {
marks: any = {
0: '0°C',
26: '26°C',
37: '37°C',
100: {
style: {
color: '#f50'
},
label: '<strong>100°C</strong>'
}
};
changeMarks() {
this.marks = {
20: '20%',
99: '99%'
};
}
}
当 nzTooltipVisible
为 always
时,将始终显示 ToolTip,为 never
时反之则始终不显示,即使在拖动、移入时也是如此。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-slider-tooltip',
template: `
<nz-slider [nzTooltipVisible]="'always'"></nz-slider>
<nz-slider [nzTooltipVisible]="'never'"></nz-slider>
`
})
export class NzDemoSliderTooltipComponent {}