Skip to content

FloatFab 悬浮按钮

预览
01:38

组件名:rice-float-fab

可以上下左右拖动的悬浮球,在微信小程序端使用了wxs技术,可以达到流畅的拖动体验。

平台兼容性

uni-app x

AndroidiOS鸿蒙Next微信小程序h5

基础使用

可以通过 default-position 设置球的默认位置

html
<rice-float-fab default-position="top-left" icon="star-fill" />

限制拖动方向

通过设置 axis 属性可以限制拖动方向,默认不限制

html
<!-- 只能x轴拖动 -->
<rice-float-fab axis="x" />
<!-- 只能y轴拖动 -->
<rice-float-fab axis="y" />

磁吸

通过设置 adsorption 值可以实现拖动结束后会自动吸附到 x 轴方向最近一边。

html
<!-- 吸附x轴 -->
<rice-float-fab adsorption="x" />
<!-- 吸附y轴 -->
<rice-float-fab adsorption="y" />

双向绑定

可以通过 v-model 控制小球的位置

html
<rice-float-fab v-if="show" v-model:offset="offset" bg-color="#e6a23c">
  <text class="fab--text">拖我</text>
</rice-float-fab>
<script setup>
const show = ref(false)
const offset = ref<FloatFabOffset>({
  x: 0,
  y: 0
})

onReady(() => {
  const window = uni.getWindowInfo()
  offset.value.x = window.windowWidth / 2 - 26
  offset.value.y = window.windowHeight / 2 - 26
  show.value = true
})
</script>

API

Props

属性名类型说明默认值
offsetObject(FloatFabOffset)悬浮球的位置,{x:number,y:number} {-1,-1}
axisString拖动的方向,可选值:x、y、xyxy
adsorptionString拖动结束后吸附的方向,默认不吸附,可选值:x、y-
default-positionString默认的初始位置,只有第一次加载且offset的x或y的值小于0时才生效,可选值:center、top-left、top-right、bottom-right、bottom-leftbottom-right
gapString|Number悬浮球与窗口的最小距离,单位px;-
gap-topString|Number单独设置悬浮按钮与上窗口的最小距离;-
gap-bottomString|Number单独设置悬浮按钮与下窗口的最小距离;-
gap-leftString|Number单独设置悬浮按钮与左窗口的最小距离;-
gap-rightString|Number单独设置悬浮按钮与右窗口的最小距离;-
over-gapBoolean拖动的时候按钮是否可超出 gap 的距离,拖动结束会自动回弹到正确的位置,默认true-
durationNumber吸附时的动画时间 单位ms,默认300-
iconStringicon图标-
icon-colorString图标颜色-
icon-sizeString|Number图标大小-
heightString|Number悬浮球的高度60px
widthString|Number悬浮球的宽度60px
radiusString|Number圆角值 ,默认为100%-
bg-colorString背景颜色,支持渐变色-
disabledBoolean是否禁止拖动,默认false-
z-indexNumberz-index 层级,默认99-
custom-styleString自定义style样式-

Events

事件名说明回调参数
click点击小球时触发-
offsetChange小球位置改变时触发(position:FloatFabOffset)

类型定义

ts
type FloatFabOffset = {
	x : number,
	y : number
}

组件导出如下类型

ts
// 组件类型
import {FloatFabOffset} from "@/uni_modules/rice-ui"
const floatFabRef = ref<RiceFloatFabComponentPublicInstance | null>(null)