|
21 | 21 | </view>
|
22 | 22 | <view class="u-dropdown__content" :style="[contentStyle, {
|
23 | 23 | transition: `opacity ${duration / 1000}s linear`,
|
24 |
| - top: $u.addUnit(height) |
| 24 | + top: $u.addUnit(height), |
| 25 | + height: contentHeight + 'px' |
25 | 26 | }]"
|
26 | 27 | @tap="maskClick" @touchmove.stop.prevent>
|
27 | 28 | <view @tap.stop.prevent class="u-dropdown__content__popup" :style="[popupStyle]">
|
|
33 | 34 | </template>
|
34 | 35 |
|
35 | 36 | <script>
|
| 37 | + /** |
| 38 | + * dropdown 下拉菜单 |
| 39 | + * @description 该组件一般用于向下展开菜单,同时可切换多个选项卡的场景 |
| 40 | + * @tutorial http://uviewui.com/components/dropdown.html |
| 41 | + * @property {String} active-color 标题和选项卡选中的颜色(默认#2979ff) |
| 42 | + * @property {String} inactive-color 标题和选项卡未选中的颜色(默认#606266) |
| 43 | + * @property {Boolean} close-on-click-mask 点击遮罩是否关闭菜单(默认true) |
| 44 | + * @property {Boolean} close-on-click-self 点击当前激活项标题是否关闭菜单(默认true) |
| 45 | + * @property {String | Number} duration 选项卡展开和收起的过渡时间,单位ms(默认300) |
| 46 | + * @property {String | Number} height 标题菜单的高度,单位任意(默认80) |
| 47 | + * @property {String | Number} border-radius 菜单展开内容下方的圆角值,单位任意(默认0) |
| 48 | + * @property {Boolean} border-bottom 标题菜单是否显示下边框(默认false) |
| 49 | + * @property {String | Number} title-size 标题的字体大小,单位任意,数值默认为rpx单位(默认28) |
| 50 | + * @event {Function} open 下拉菜单被打开时触发 |
| 51 | + * @event {Function} close 下拉菜单被关闭时触发 |
| 52 | + * @example <u-dropdown></u-dropdown> |
| 53 | + */ |
36 | 54 | export default {
|
37 | 55 | name: 'u-dropdown',
|
38 | 56 | props: {
|
|
75 | 93 | titleSize: {
|
76 | 94 | type: [Number, String],
|
77 | 95 | default: 28
|
| 96 | + }, |
| 97 | + // 下拉出来的内容部分的圆角值 |
| 98 | + borderRadius: { |
| 99 | + type: [Number, String], |
| 100 | + default: 0 |
78 | 101 | }
|
79 | 102 | },
|
80 | 103 | data() {
|
|
91 | 114 | opacity: 0
|
92 | 115 | },
|
93 | 116 | // 让某个菜单保持高亮的状态
|
94 |
| - highlightIndex: 99999 |
| 117 | + highlightIndex: 99999, |
| 118 | + contentHeight: 0 |
95 | 119 | }
|
96 | 120 | },
|
97 | 121 | computed: {
|
|
101 | 125 | // 进行Y轴位移,展开状态时,恢复原位。收齐状态时,往上位移100%,进行隐藏
|
102 | 126 | style.transform = `translateY(${this.active ? 0 : '-100%'})`
|
103 | 127 | style['transition-duration'] = this.duration / 1000 + 's';
|
| 128 | + style.borderRadius = `0 0 ${this.$u.addUnit(this.borderRadius)} ${this.$u.addUnit(this.borderRadius)}`; |
104 | 129 | return style;
|
105 | 130 | }
|
106 | 131 | },
|
107 | 132 | created() {
|
108 | 133 | // 引用所有子组件(u-dropdown-item)的this,不能在data中声明变量,否则在微信小程序会造成循环引用而报错
|
109 | 134 | this.children = [];
|
110 | 135 | },
|
| 136 | + mounted() { |
| 137 | + this.getContentHeight(); |
| 138 | + }, |
111 | 139 | methods: {
|
112 | 140 | init() {
|
113 | 141 | // 当某个子组件内容变化时,触发父组件的init,父组件再让每一个子组件重新初始化一遍
|
|
171 | 199 | // 外部手动设置某个菜单高亮
|
172 | 200 | highlight(index = undefined) {
|
173 | 201 | this.highlightIndex = index !== undefined ? index : 99999;
|
| 202 | + }, |
| 203 | + // 获取下拉菜单内容的高度 |
| 204 | + getContentHeight() { |
| 205 | + // 这里的原理为,因为dropdown组件是相对定位的,它的下拉出来的内容,必须给定一个高度 |
| 206 | + // 才能让遮罩占满菜单一下,直到屏幕底部的高度 |
| 207 | + // this.$u.sys()为uView封装的获取设备信息的方法 |
| 208 | + let windowHeight = this.$u.sys().windowHeight; |
| 209 | + this.$uGetRect('.u-dropdown__menu').then(res => { |
| 210 | + // 这里获取的是dropdown的尺寸,在H5上,uniapp获取尺寸是有bug的(以前提出修复过,后来又出现了此bug,目前hx2.8.11版本) |
| 211 | + // H5端bug表现为元素尺寸的top值为导航栏底部到到元素的上边沿的距离,但是元素的bottom值确是导航栏顶部到元素底部的距离 |
| 212 | + // 二者是互相矛盾的,本质原因是H5端导航栏非原生,uni的开发者大意造成 |
| 213 | + // 这里取菜单栏的botton值合理的,不能用res.top,否则页面会造成滚动 |
| 214 | + this.contentHeight = windowHeight - res.bottom; |
| 215 | + }) |
174 | 216 | }
|
175 | 217 | }
|
176 | 218 | }
|
|
182 | 224 | .u-dropdown {
|
183 | 225 | flex: 1;
|
184 | 226 | width: 100%;
|
| 227 | + position: relative; |
185 | 228 |
|
186 | 229 | &__menu {
|
187 | 230 | @include vue-flex;
|
|
220 | 263 | left: 0px;
|
221 | 264 | bottom: 0;
|
222 | 265 | overflow: hidden;
|
| 266 | + |
223 | 267 |
|
224 | 268 | &__mask {
|
225 | 269 | position: absolute;
|
|
236 | 280 | z-index: 10;
|
237 | 281 | transition: all 0.3s;
|
238 | 282 | transform: translate3D(0, -100%, 0);
|
| 283 | + overflow: hidden; |
239 | 284 | }
|
240 | 285 | }
|
241 | 286 |
|
|
0 commit comments