Skip to content

Swipe 滑动手势

介绍

常用于单元格左右滑删除等手势操作

代码演示

基础用法

html
<template>
  <nut-swipe>
    <nut-cell title="左滑删除" round-radius="0"></nut-cell>

    <template #right>
      <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
    </template>
  </nut-swipe>
</template>

禁止滑动

html
<template>
  <nut-swipe disabled>
    <nut-cell title="禁止滑动" round-radius="0"></nut-cell>

    <template #right>
      <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
    </template>
  </nut-swipe>
</template>

左右滑动

html
<template>
  <nut-swipe>
    <template #left>
      <nut-button style="height: 100%" type="success" shape="square">选择</nut-button>
    </template>

    <nut-cell title="左滑右滑都可以哦" round-radius="0"></nut-cell>

    <template #right>
      <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
      <nut-button style="height: 100%" type="info" shape="square">收藏</nut-button>
    </template>
  </nut-swipe>
</template>

异步控制

html
<template>
  <nut-swipe ref="swipe" :close-on-click="['right']" @open="onOpen" @close="onClose">
    <nut-cell title="异步打开关闭">
      <template #link>
        <nut-switch v-model="checked" active-text="" inactive-text="" @change="onSwitchChange"></nut-switch>
      </template>
    </nut-cell>

    <template #right>
      <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
    </template>
  </nut-swipe>
</template>
typescript
import type { SwipeInst, SwipeToggleEvent } from 'nutui-uniapp'

const swipe = ref<SwipeInst | null>(null)

const checked = ref<boolean>(false)

function onSwitchChange(value: boolean) {
  if (value) {
    swipe.value?.open('left')
  }
  else {
    swipe.value?.close()
  }
}

function onOpen(info: SwipeToggleEvent) {
  console.log('打开了...', info)
}

function onClose(info: SwipeToggleEvent) {
  console.log('关闭了...', info)
}

自定义

html
<template>
  <nut-swipe>
    <template #left>
      <nut-button style="height: 100%" type="success" shape="square">选择</nut-button>
    </template>

    <nut-cell title="商品描述">
      <template #link>
        <nut-input-number v-model="number"></nut-input-number>
      </template>
    </nut-cell>

    <template #right>
      <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
      <nut-button style="height: 100%" type="info" shape="square">收藏</nut-button>
    </template>
  </nut-swipe>
</template>
typescript
const number = ref<number>(0)

使用 SwipeGroup 控制 Swipe 之间互斥

此时各个 Swipe 的 name 为必填项。

html
<template>
  <nut-swipe-group lock>
    <nut-swipe name="swipe1">
      <nut-cell title="左滑删除" round-radius="0"></nut-cell>

      <template #right>
        <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
      </template>
    </nut-swipe>

    <nut-swipe name="swipe2">
      <nut-cell title="左滑删除" round-radius="0"></nut-cell>

      <template #right>
        <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
      </template>
    </nut-swipe>

    <nut-swipe name="swipe3">
      <nut-cell title="左滑删除" round-radius="0"></nut-cell>

      <template #right>
        <nut-button style="height: 100%" type="danger" shape="square">删除</nut-button>
      </template>
    </nut-swipe>
  </nut-swipe-group>
</template>

Swipe

Props

参数说明类型默认值
name唯一标识string-
disabled是否禁用滑动stringfalse
touch-move-prevent-default是否阻止滑动事件行为booleanfalse
touch-move-stop-propagation是否阻止滑动事件冒泡booleanfalse
close-on-click 1.7.7点击自动关闭的部分,可选值为:leftcontentrightstring[]["left", "content", "right"]

Events

事件名说明回调参数
open开启时触发,left 指向左滑,right 指向右滑name, direction
close关闭时触发,同上name, direction

Slots

名称说明
left左侧滑动内容
default自定义内容
right右侧滑动内容

Methods

通过 ref 可以获取到 Swipe 实例并调用实例方法。

方法名说明参数
open打开单元格侧边栏,left 指向左滑,right 指向右滑direction
close收起单元格侧边栏-

SwipeGroup

Props

参数说明类型默认值
lock控制内部 Swipe 互斥,即滑动打开某一个 Swipe 时,触发其余 Swipe 的 close 方法booleanfalse

MIT Licensed