SlideGroup 横向滑动容器
横向滚动数据列表,支持选择、活动项定位和边界操作插槽。
引入
uni-app 配置 easycom 后使用 <hui-slide-group>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-slide-group
v-model="selected"
:items="items"
item-key="id"
selectable
active-position="center"
@boundary-change="(value) => (lastEvent = 'boundary-change:' + JSON.stringify(value))"
@change="onChange"
@click="onClick"
>
<template #item="{ index, active }"
><hui-tag :type="active ? 'primary' : 'info'">{{ items[index].label }}</hui-tag></template
>
<template #suffix="{ atEnd, scrollNext }"
><hui-button :disabled="atEnd" @click="scrollNext">后移 / Next</hui-button></template
> </hui-slide-group
><text>{{ selected }} / {{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const selected = ref<string | number>(0)
const onChange = (key: string | number, context: { item: unknown; index: number }) => {
lastEvent.value = JSON.stringify({ key, context })
}
const onClick = (item: unknown, context: { index: number }) => {
lastEvent.value = JSON.stringify({ item, context })
}
const items = Array.from({ length: 12 }, (_, id) => ({ id, label: '栏目 / Section ' + id }))
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
items | unknown[] | [] | 每一项的数据,内容由 item 插槽渲染 |
itemKey | string | ((item: unknown, index: number) => string | number) | — | 列表 key 和当前项的标识;不传用下标 |
modelValue | string | number | — | 当前项的 key;变化时滚到它 |
defaultValue | string | number | — | defaultValue 属性。 |
selectable | boolean | false | 点项时改当前项(update:modelValue);false 时只发 click |
activePosition | SlideGroupActivePosition | 'center' | 当前项滚到哪:居中、靠左、刚好露出 |
gap | number | 8 | 项间距,px(margin-left,不用 flex gap) |
padding | number | 16 | 首尾留白,px |
fade | boolean | false | 没到头的一侧加渐隐遮罩 |
animated | boolean | true | 滚动是否带动画(scroll-with-animation) |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
update:modelValue | (key: string | number) |
change | (key: string | number, context: { item: unknown; index: number }) |
click | (item: unknown, context: { index: number }) |
boundary-change | (state: SlideGroupBoundary) |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
prefix | { atStart, atEnd, scrollPrev, scrollNext } |
item | { item, index, active } |
suffix | { atStart, atEnd, scrollPrev, scrollNext } |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
scrollToIndex | (index: number): Promise<void> | 滚动到条目下标 |
scrollPrev | (): void | 向前滚动视口 |
scrollNext | (): void | 向后滚动视口 |
refresh | (): Promise<void> | 重新测量并更新状态 |
类型
SlideGroupActivePosition、SlideGroupBoundary、SlideGroupProps、SlideGroupEmits、SlideGroupExpose.
平台说明
基于共享平台组件实现 H5、微信小程序、App-vue 与普通 Vue 3 Web。单元测试不代替设备验收;触摸、读屏、页面失活和原生组件表现按冒烟清单逐端核对。
设备冒烟清单.