1.0.1 • Published 1 month ago

@tanzhenxing/zx-floating-panel v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

zx-floating-panel 浮动面板

浮动在页面底部的面板,可以上下拖动来浏览内容,常用于提供额外的功能或信息。

平台兼容性

H5小程序App
✔️✔️✔️

基本用法

FloatingPanel 的默认高度为 100px(或 anchors 第一个值),用户可以拖动来展开面板。

<template>
  <zx-floating-panel v-model="showPanel" :height="150">
    <view>面板内容</view>
  </zx-floating-panel>
</template>

<script setup>
import { ref } from 'vue';
const showPanel = ref(true);
</script>

自定义锚点

通过 anchors 属性来设置 FloatingPanel 的锚点位置(单位 px),并通过 v-model:height (或 :height@update:height) 来控制当前面板的显示高度。

例如,使面板的高度在 100px40% 屏幕高度和 70% 屏幕高度三个位置停靠:

<template>
  <zx-floating-panel v-model="showPanel" v-model:height="panelHeight" :anchors="panelAnchors">
    <view style="text-align: center; padding: 15px">
      <text>面板显示高度 {{ panelHeight.toFixed(0) }} px</text>
    </view>
  </zx-floating-panel>
</template>

<script setup>
import { ref, computed } from 'vue';

const showPanel = ref(true);
const panelAnchors = computed(() => {
  const windowHeight = uni.getSystemInfoSync().windowHeight;
  return [
    100,
    Math.round(0.4 * windowHeight),
    Math.round(0.7 * windowHeight),
  ].sort((a,b) => a - b);
});
const panelHeight = ref(panelAnchors.value[0]);
</script>

仅头部拖拽

默认情况下,FloatingPanel 的头部区域和内容区域都可以被拖拽,你可以通过 content-draggable 属性来禁用内容区域的拖拽。

<template>
  <zx-floating-panel v-model="showPanel" :content-draggable="false">
    <view style="text-align: center; padding: 15px">
      <text>内容不可拖拽</text>
    </view>
  </zx-floating-panel>
</template>

<script setup>
import { ref } from 'vue';
const showPanel = ref(true);
</script>

API

Props

参数说明类型默认值
v-model是否显示面板Booleantrue
v-model:height当前面板的显示高度 (或使用 :height@update:height)Number0 (实际会基于 anchors 初始化)
anchors设置自定义锚点, 单位 pxArray<Number>[100, windowHeight * 0.6] (近似值)
duration动画时长,单位秒,设置为 0 可以禁用动画Number \| String0.3
content-draggable允许拖拽内容容器Booleantrue
lock-scroll当不拖拽时,是否锁定背景滚动 (H5平台部分支持,小程序/App需页面配合)Booleanfalse
safe-area-inset-bottom是否开启底部安全区适配Booleantrue

Events

事件名说明回调参数
heightChange面板显示高度改变且结束拖动后触发{ height: number }
update:height面板高度更新时触发 (用于v-model:height)newHeight: number
update:modelValue面板显隐状态更新时触发 (用于v-model)newValue: boolean

Slots

NameDescription
default自定义面板内容
header自定义面板标头

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式。

名称默认值描述
--van-floating-panel-border-radius16px面板圆角
--van-floating-panel-header-height30px头部高度
--van-floating-panel-z-index999z-index 层级
--van-floating-panel-background#fff (或 var(--van-background-2))背景色
--van-floating-panel-bar-width20px拖动条宽度
--van-floating-panel-bar-height3px拖动条高度
--van-floating-panel-bar-color#c8c9cc (或 var(--van-gray-5))拖动条颜色

注意: 上述默认值参考了 Vant,实际组件内可能使用更通用的值或依赖项目主题配置。