跳到主要内容
版本:v8

ion-range

shadow

范围滑块允许用户通过移动滑块旋钮从一系列值中进行选择。默认情况下,一个旋钮控制范围的值。此行为可以通过双旋钮进行自定义。

默认情况下,范围滑块的最小值为 0,最大值为 100。这可以通过 minmax 属性进行配置。

标签

应使用标签来描述范围。它们可以在视觉上使用,当用户聚焦到范围上时,屏幕阅读器也会读出它们。这使用户可以轻松理解范围的意图。Range 有多种分配标签的方式:

  • label 属性:用于纯文本标签
  • label 插槽:用于自定义 HTML 标签
  • aria-label:用于为屏幕阅读器提供标签,但不添加可见标签

标签位置

下面的演示展示了如何使用 labelPlacement 属性更改标签相对于范围的位置。虽然这里使用了 label 属性,但 labelPlacement 也可以与 label 插槽一起使用。

标签插槽

纯文本标签应通过 label 属性传入,但若需要自定义 HTML,则可以通过 label 插槽传入。

无可见标签

如果不需要可见标签,开发者仍应提供 aria-label,以便屏幕阅读器可以访问范围。

装饰

装饰性元素可以通过范围的 startend 插槽传入。这对于添加图标(如低音量或高音量图标)非常有用。由于这些元素是装饰性的,它们不应被屏幕阅读器等辅助技术播报。

如果文档的方向设置为从左到右,则放入 start 位置的插槽内容将显示在范围的左侧,而放入 end 位置的插槽内容将显示在范围的右侧。在从右到左(rtl)的方向中,放入 start 位置的插槽内容将显示在范围的右侧,而放入 end 位置的插槽内容将显示在范围的左侧。

双旋钮

双旋钮引入了两个旋钮控件,用户可以使用它们在上下边界之间选择值。选中后,Range 将发出一个 ionChange 事件,其中包含一个 RangeValue,包含选中的上限和下限值。

大头针

pin 属性将在拖动时在旋钮上方显示 Range 的值。这允许用户在范围内选择特定的值。

使用 pinFormatter 函数,开发者可以自定义向用户显示的范围值格式。

吸附和刻度

刻度显示 Range 上每个可用值的指示标记。为了使用刻度,开发者必须将 snapsticks 属性都设置为 true

启用吸附后,Range 旋钮在拖动和释放时将吸附到最近的可用值。

事件处理

使用 ionChange

ionChange 事件在 Range 旋钮值发生变化时触发。

控制台
控制台消息将在上方示例中调用 console.log 时显示在此处。

使用 ionKnobMoveStartionKnobMoveEnd

ionKnobMoveStart 事件在 Range 旋钮开始拖动时触发,无论是通过鼠标拖拽、触摸手势还是键盘交互。相反,ionKnobMoveEnd 在 Range 旋钮释放时触发。两个事件都以 RangeValue 类型触发,并与 dualKnobs 属性配合使用。

控制台
控制台消息将在上方示例中调用 console.log 时显示在此处。

主题

CSS 自定义属性

Range 包含 CSS 变量,可快速设置主题并自定义 Range 组件的外观以匹配您的应用设计。

CSS Shadow Parts

Range 包含 CSS 阴影部分,允许完全自定义 Range 组件中的特定元素节点。CSS 阴影部分 提供最大的自定义能力,是在需要对 Range 组件进行高级样式设计时的推荐方法。

当启用 dualKnobs 时,会暴露额外的 Shadow Parts,允许每个旋钮独立设置样式。这些有两种形式:静态标识部分AB)和动态位置部分lowerupper)。A 和 B 部分始终指向相同的物理旋钮,即使旋钮交叉。相比之下,lower 和 upper 部分反映当前值位置,并在旋钮交叉时自动交换。这样可以通过一致的标识或范围内的相对值进行样式设计。

接口

RangeChangeEventDetail

interface RangeChangeEventDetail {
value: RangeValue;
}

RangeKnobMoveStartEventDetail

interface RangeKnobMoveStartEventDetail {
value: RangeValue;
}

RangeKnobMoveEndEventDetail

interface RangeKnobMoveEndEventDetail {
value: RangeValue;
}

RangeCustomEvent

虽然不是必需的,但此接口可以替代 CustomEvent 接口,为此组件发出的 Ionic 事件提供更强的类型支持。

interface RangeCustomEvent extends CustomEvent {
detail: RangeChangeEventDetail;
target: HTMLIonRangeElement;
}

类型

RangeValue

type RangeValue = number | { lower: number, upper: number };

属性

activeBarStart

说明The start position of the range active bar. This feature is only available with a single knob (dualKnobs="false"). Valid values are greater than or equal to the min value and less than or equal to the max value.
属性active-bar-start
类型number | undefined
默认值undefined

color

说明The color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming.
属性color
类型"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined
默认值undefined

debounce

说明How long, in milliseconds, to wait to trigger the ionInput event after each change in the range value.
属性debounce
类型number | undefined
默认值undefined

disabled

说明If true, the user cannot interact with the range.
属性disabled
类型boolean
默认值false

dualKnobs

说明Show two knobs.
属性dual-knobs
类型boolean
默认值false

label

说明The text to display as the control's label. Use this over the label slot if you only need plain text. The label property will take priority over the label slot if both are used.
属性label
类型string | undefined
默认值undefined

labelPlacement

说明Where to place the label relative to the range. "start": The label will appear to the left of the range in LTR and to the right in RTL. "end": The label will appear to the right of the range in LTR and to the left in RTL. "fixed": The label has the same behavior as "start" except it also has a fixed width. Long text will be truncated with ellipses ("..."). "stacked": The label will appear above the range regardless of the direction.
属性label-placement
类型"end" | "fixed" | "stacked" | "start"
默认值'start'

max

说明Maximum integer value of the range.
属性max
类型number
默认值100

min

说明Minimum integer value of the range.

这是一个虚拟属性,在初始化时设置一次,之后更改其值不会更新组件。
属性min
类型number
默认值0

mode

说明The mode determines which platform styles to use.

这是一个虚拟属性,在初始化时设置一次,之后更改其值不会更新组件。
属性mode
类型"ios" | "md"
默认值undefined

name

说明The name of the control, which is submitted with the form data.
属性name
类型string
默认值this.rangeId

pin

说明If true, a pin with integer value is shown when the knob is pressed.
属性pin
类型boolean
默认值false

pinFormatter

说明A callback used to format the pin text. By default the pin text is set to Math.round(value).

See https://ionicframework.com/docs/troubleshooting/runtime#accessing-this if you need to access this from within the callback.
属性undefined
类型(value: number) => string | number
默认值(value: number): number => Math.round(value)

snaps

说明If true, the knob snaps to tick marks evenly spaced based on the step property value.
属性snaps
类型boolean
默认值false

step

说明Specifies the value granularity.
属性step
类型number
默认值1

ticks

说明If true, tick marks are displayed based on the step value. Only applies when snaps is true.
属性ticks
类型boolean
默认值true

value

说明the value of the range.
属性value
类型number | { lower: number; upper: number; }
默认值0

事件

Name说明冒泡
ionBlurEmitted when the range loses focus.true
ionChangeThe ionChange event is fired for <ion-range> elements when the user modifies the element's value: - When the user releases the knob after dragging; - When the user moves the knob with keyboard arrows

This event will not emit when programmatically setting the value property.
true
ionFocusEmitted when the range has focus.true
ionInputThe ionInput event is fired for <ion-range> elements when the value is modified. Unlike ionChange, ionInput is fired continuously while the user is dragging the knob.true
ionKnobMoveEndEmitted when the user finishes moving the range knob, whether through mouse drag, touch gesture, or keyboard interaction.true
ionKnobMoveStartEmitted when the user starts moving the range knob, whether through mouse drag, touch gesture, or keyboard interaction.true

方法

该组件没有可用的公共方法。

CSS Shadow Parts

Name说明
activatedAdded to the knob-handle, knob, and pin when the knob is active. Only one set has this part at a time when dualKnobs is true.
barThe inactive part of the bar.
bar-activeThe active part of the bar.
focusedAdded to the knob-handle, knob, and pin that currently has focus. Only one set has this part at a time when dualKnobs is true.
hoverAdded to the knob-handle, knob, and pin when the knob has hover. Only one set has this part at a time when dualKnobs is true.
knobThe visual knob element on the range track.
knob-aThe visual knob for the static A identity when dualKnobs is true. This identity does not change, even if the knobs cross and swap which one represents the lower or upper value.
knob-bThe visual knob for the static B identity when dualKnobs is true. This identity does not change, even if the knobs cross and swap which one represents the lower or upper value.
knob-handleThe container that wraps the knob and handles drag interactions.
knob-handle-aThe container for the knob with the static A identity when dualKnobs is true. This identity does not change, even if the knobs cross and swap which one represents the lower or upper value.
knob-handle-bThe container for the knob with the static B identity when dualKnobs is true. This identity does not change, even if the knobs cross and swap which one represents the lower or upper value.
knob-handle-lowerThe container for the knob whose current value is lower when dualKnobs is true. The lower and upper parts swap which knob handle they refer to when the knobs cross.
knob-handle-upperThe container for the knob whose current value is upper when dualKnobs is true. The lower and upper parts swap which knob handle they refer to when the knobs cross.
knob-lowerThe visual knob whose current value is lower when dualKnobs is true. The lower and upper parts swap which knob they refer to when the knobs cross.
knob-upperThe visual knob whose current value is upper when dualKnobs is true. The lower and upper parts swap which knob they refer to when the knobs cross.
labelThe label text describing the range.
pinThe value indicator displayed above a knob.
pin-aThe value indicator above the knob with the static A identity when dualKnobs is true. This identity does not change, even if the knobs cross and swap which one represents the lower or upper value.
pin-bThe value indicator above the knob with the static B identity when dualKnobs is true. This identity does not change, even if the knobs cross and swap which one represents the lower or upper value.
pin-lowerThe value indicator above the knob whose current value is lower when dualKnobs is true. The lower and upper parts swap which pin they refer to when the knobs cross.
pin-upperThe value indicator above the knob whose current value is upper when dualKnobs is true. The lower and upper parts swap which pin they refer to when the knobs cross.
pressedAdded to the knob-handle, knob, and pin that is currently being pressed to drag. Only one set has this part at a time when dualKnobs is true.
tickAn inactive tick mark.
tick-activeAn active tick mark.

CSS 自定义属性

Name说明
--bar-backgroundBackground of the range bar
--bar-background-activeBackground of the active range bar
--bar-border-radiusBorder radius of the range bar
--bar-heightHeight of the range bar
--heightHeight of the range
--knob-backgroundBackground of the range knob
--knob-border-radiusBorder radius of the range knob
--knob-box-shadowBox shadow of the range knob
--knob-sizeSize of the range knob
--pin-backgroundBackground of the range pin (only available in MD mode)
--pin-colorColor of the range pin (only available in MD mode)

插槽

Name说明
endContent is placed to the right of the range slider in LTR, and to the left in RTL.
labelThe label text to associate with the range. Use the "labelPlacement" property to control where the label is placed relative to the range.
startContent is placed to the left of the range slider in LTR, and to the right in RTL.