1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
| <template>
| <view class="c-nav-bar">
| <AtNavBar
| :title="title"
| :leftIconType="iconType"
| :onClickLeftIcon="evt => onGoBack()"
| :rightFirstIconType="dropNav ? 'bullet-list' : ''"
| :onClickRgIconSt="evt => onDropMenu()"
| />
| <view
| class="c-nav-bar-right"
| v-if="!dropNav"
| >
| <slot />
| </view>
| <view
| class="c-nav-bar-drop"
| v-show="dropShow"
| >
| <view class="inner">
| <view class="arrow"></view>
| <view class="box">
| <view
| class="c-nav-bar-drop-item item"
| v-for="(nav,index) in dropNav"
| :key="index"
| @tap="evt=>onGoNav(nav)"
| >{{nav.title}}</view>
| </view>
| </view>
| </view>
| </view>
| </template>
|
| <script>
| import Taro from '@tarojs/taro';
| import { AtNavBar } from 'taro-ui-vue';
| import './cNavBar.scss';
|
| export default {
| name: 'CNavBar',
| components: {
| AtNavBar,
| },
| props: {
| // H5页面导航标题
| title: String,
| // H5页面导航左侧图标
| iconType: {
| type: String,
| default: 'chevron-left',
| },
| // H5页面导航左侧图点击事件
| onClickIcon: Function,
| // H5页面导航右侧下拉菜单配置
| dropNav: Array,
| },
| data() {
| return {
| dropShow: false,
| dropTimer: -1,
| dropActive: false,
| };
| },
| methods: {
| onGoBack() {
| if (this.onClickIcon) {
| const needBack = this.onClickIcon();
| // 返回 true 时继续跳回上一页
| if (needBack) {
| Taro.navigateBack();
| }
| } else {
| Taro.navigateBack();
| }
| },
| onGoNav(nav) {
| if (!nav.url) {
| return;
| }
| this.dropShow = false;
| setTimeout(() => {
| Taro.navigateTo({ url: nav.url });
| }, 0);
| },
| onDropMenu() {
| this.dropShow = !this.dropShow;
| this.dropActive = true;
| clearTimeout(this.dropTimer);
| this.dropTimer = setTimeout(() => {
| this.dropActive = false;
| }, 500);
| },
| outCloseMenu(evt) {
| setTimeout(() => {
| // 未打开时,忽略
| if (!this.dropShow) {
| return;
| }
| // 刚刚打开的一段时间内,忽略
| if (this.dropActive) {
| return;
| }
| // 点击了菜单项,忽略
| const className = evt.target.className;
| if (
| className.indexOf('c-nav-bar-drop-item') >= 0 ||
| className.indexOf('at-nav-bar__container') >= 0
| ) {
| return;
| }
| // 执行关闭
| this.dropShow = false;
| }, 0);
| },
| },
| mounted() {
| Taro.eventCenter.on('pageTouchstart', this.outCloseMenu);
| Taro.eventCenter.on('pageClick', this.outCloseMenu);
| },
| beforeDestroy() {
| Taro.eventCenter.off('pageTouchstart', this.outCloseMenu);
| Taro.eventCenter.off('pageClick', this.outCloseMenu);
| },
| };
| </script>
|
|