buddhism/components/custom-navbar/README.md

172 lines
4.2 KiB
Markdown
Raw Normal View History

2025-08-07 17:30:36 +08:00
# Custom Navbar 组件使用说明
## 功能特性
2025-08-07 17:30:36 +08:00
- 自动监听页面滚动
- 滚动时导航栏变为纯色背景
- 回到顶部时导航栏变为透明
- 支持自定义背景色和滚动阈值
- 提供滚动状态事件回调
2025-08-07 17:30:36 +08:00
## 基本用法
```vue
<template>
2025-08-07 17:30:36 +08:00
<view>
2025-07-31 09:27:55 +08:00
<custom-navbar title="页面标题" />
2025-08-07 17:30:36 +08:00
<!-- 页面内容 -->
</view>
</template>
2025-08-07 17:30:36 +08:00
```
2025-08-07 17:30:36 +08:00
## 高级用法
2025-08-07 17:30:36 +08:00
```vue
<template>
<view>
2025-08-14 11:22:53 +08:00
<custom-navbar
2025-08-07 17:30:36 +08:00
title="古刹巡礼"
:backgroundColor="'#FAF8F3'"
:scrollThreshold="100"
:enableScrollEffect="true"
@scroll-change="onScrollChange"
@scroll="onScroll"
>
<template #right>
<view class="right-button">更多</view>
</template>
</custom-navbar>
2025-08-14 11:22:53 +08:00
2025-08-07 17:30:36 +08:00
<!-- 页面内容 -->
</view>
</template>
<script>
2025-08-14 11:22:53 +08:00
export default {
methods: {
onScrollChange(data) {
console.log('滚动状态变化:', data.isScrolled)
},
onScroll(data) {
console.log('滚动位置:', data.scrollTop)
},
2025-08-07 17:30:36 +08:00
},
}
</script>
```
2025-08-07 17:30:36 +08:00
## Props 参数
2025-08-14 11:22:53 +08:00
| 参数 | 类型 | 默认值 | 说明 |
| ------------------ | ------- | ---------------------- | ---------------- |
| title | String | '' | 导航栏标题 |
| backIcon | String | CommonEnum.BACK_BUTTON | 返回按钮图标 |
| showBack | Boolean | true | 是否显示返回按钮 |
| backgroundColor | String | '#ffffff' | 滚动时的背景色 |
| scrollThreshold | Number | 50 | 滚动阈值(像素) |
| enableScrollEffect | Boolean | true | 是否启用滚动效果 |
2025-08-07 17:30:36 +08:00
## Events 事件
2025-08-14 11:22:53 +08:00
| 事件名 | 参数 | 说明 |
| ------------- | ------------------------- | ------------------ |
| back | - | 点击返回按钮时触发 |
2025-08-07 17:30:36 +08:00
| scroll-change | { isScrolled, scrollTop } | 滚动状态变化时触发 |
2025-08-14 11:22:53 +08:00
| scroll | { scrollTop, isScrolled } | 页面滚动时触发 |
2025-08-07 17:30:36 +08:00
## 方法
2025-08-14 11:22:53 +08:00
| 方法名 | 参数 | 说明 |
| -------------- | --------- | ---------------- |
2025-08-07 17:30:36 +08:00
| setScrollState | scrollTop | 手动设置滚动状态 |
2025-08-07 17:30:36 +08:00
## 样式定制
2025-08-07 17:30:36 +08:00
组件会自动添加以下 CSS 类:
2025-08-07 17:30:36 +08:00
- `.navbar-scrolled`: 滚动状态下的样式
- 过渡动画:背景色变化有 0.3s 的过渡效果
- 阴影效果:滚动时自动添加阴影
2025-08-07 17:30:36 +08:00
## 注意事项
2025-08-07 17:30:36 +08:00
1. **重要**:需要在父页面的 `onPageScroll` 生命周期中调用组件的 `handlePageScroll` 方法
2. 滚动效果默认启用,可通过 `enableScrollEffect` 关闭
3. 背景色变化有平滑过渡动画
4. 组件使用 `ref` 引用,确保在页面中正确引用
2025-08-07 17:30:36 +08:00
## 完整使用示例
2025-08-07 17:30:36 +08:00
### 方法一:使用 Mixin推荐
2025-08-07 17:30:36 +08:00
```vue
<template>
<view>
2025-08-14 11:22:53 +08:00
<custom-navbar
2025-08-07 17:30:36 +08:00
ref="customNavbar"
2025-08-14 11:22:53 +08:00
title="页面标题"
2025-08-07 17:30:36 +08:00
backgroundColor="#FAF8F3"
:scrollThreshold="50"
@scroll-change="onScrollChange"
/>
<!-- 页面内容 -->
2025-08-14 11:22:53 +08:00
</view>
2025-08-07 17:30:36 +08:00
</template>
<script>
2025-08-14 11:22:53 +08:00
import CustomNavbar from '@/components/custom-navbar/custom-navbar.vue'
import PageScrollMixin from '@/mixins/page-scroll-mixin.js'
export default {
mixins: [PageScrollMixin], // 自动处理滚动事件
components: {
CustomNavbar,
},
methods: {
onScrollChange(data) {
console.log('滚动状态变化:', data.isScrolled)
},
},
2025-08-07 17:30:36 +08:00
}
</script>
```
### 方法二:手动处理
```vue
<template>
<view>
2025-08-14 11:22:53 +08:00
<custom-navbar
2025-08-07 17:30:36 +08:00
ref="customNavbar"
2025-08-14 11:22:53 +08:00
title="页面标题"
2025-08-07 17:30:36 +08:00
backgroundColor="#FAF8F3"
:scrollThreshold="50"
@scroll-change="onScrollChange"
/>
<!-- 页面内容 -->
</view>
</template>
<script>
2025-08-14 11:22:53 +08:00
import CustomNavbar from '@/components/custom-navbar/custom-navbar.vue'
export default {
components: {
CustomNavbar,
},
// 页面生命周期 - 必须添加
onPageScroll(e) {
// 将页面滚动事件传递给导航栏组件
if (this.$refs.customNavbar) {
this.$refs.customNavbar.handlePageScroll(e)
}
},
methods: {
onScrollChange(data) {
console.log('滚动状态变化:', data.isScrolled)
},
},
2025-08-07 17:30:36 +08:00
}
</script>
2025-08-14 11:22:53 +08:00
```