u-dropdown.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <view class="u-dropdown">
  3. <view class="u-dropdown__menu" ref="u-dropdown__menu" :style="{
  4. height: $u.addUnit(height),
  5. backgroundColor: bgColor,
  6. borderRadius: $u.addUnit(round)
  7. }" :class="{
  8. 'u-border-bottom': borderBottom
  9. }">
  10. <view class="u-dropdown__menu__item" v-for="(item, index) in menuList" :key="index" @tap.stop="menuClick(index)">
  11. <text class="u-dropdown__menu__item__text" :style="{
  12. color: item.disabled ? '#c0c4cc' : (index === current || highlightIndex == index) ? activeColor : inactiveColor,
  13. fontSize: $u.addUnit(titleSize)
  14. }">{{item.title}}</text>
  15. <view class="u-dropdown__menu__item__arrow" :class="{
  16. 'u-dropdown__menu__item__arrow--rotate': index === current
  17. }">
  18. <u-icon :custom-style="{display: 'flex'}" :name="menuIcon" :size="menuIconSize" :color="index === current || highlightIndex == index ? activeColor : '#c0c4cc'"></u-icon>
  19. </view>
  20. </view>
  21. </view>
  22. <view class="u-dropdown__content" :style="[contentStyle]" @tap="maskClick" @touchmove.stop.prevent>
  23. <view class="u-dropdown__content__mask"></view>
  24. <view @tap.stop.prevent class="u-dropdown__content__popup" :style="[popupStyle]">
  25. <slot></slot>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. // #ifdef APP-NVUE
  32. const dom = uni.requireNativePlugin('dom')
  33. // #endif
  34. import props from './props.js';
  35. import mixin from '../../libs/mixin/mixin'
  36. import mpMixin from '../../libs/mixin/mpMixin';
  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} round 菜单的圆角值,单位任意(默认0)
  48. * @property {String | Number} bg-color 菜单的背景颜色,单位任意(默认#fff)
  49. * @property {String | Number} border-radius 菜单展开内容下方的圆角值,单位任意(默认0)
  50. * @property {Boolean} border-bottom 标题菜单是否显示下边框(默认false)
  51. * @property {String | Number} title-size 标题的字体大小,单位任意,数值默认为rpx单位(默认28)
  52. * @event {Function} open 下拉菜单被打开时触发
  53. * @event {Function} close 下拉菜单被关闭时触发
  54. * @example <u-dropdown></u-dropdown>
  55. */
  56. export default {
  57. name: 'u-dropdown',
  58. mixins: [mixin, props],
  59. data() {
  60. return {
  61. showDropdown: true, // 是否打开下来菜单,
  62. menuList: [], // 显示的菜单
  63. active: false, // 下拉菜单的状态
  64. // 当前是第几个菜单处于激活状态,小程序中此处不能写成false或者"",否则后续将current赋值为0,
  65. // 无能的TX没有使用===而是使用==判断,导致程序认为前后二者没有变化,从而不会触发视图更新
  66. current: 99999,
  67. // 外层内容的样式,初始时处于底层,且透明
  68. contentStyle: {
  69. zIndex: -1,
  70. opacity: 0
  71. },
  72. // 让某个菜单保持高亮的状态
  73. highlightIndex: 99999,
  74. contentHeight: 0
  75. }
  76. },
  77. computed: {
  78. // 下拉出来部分的样式
  79. popupStyle() {
  80. let style = {};
  81. // 进行Y轴位移,展开状态时,恢复原位。收齐状态时,往上位移100%,进行隐藏
  82. style.transform = `translateY(${this.active ? 0 : '-100%'})`
  83. style['transition-duration'] = this.duration / 1000 + 's';
  84. style.borderRadius = `0 0 ${this.$u.addUnit(this.borderRadius)} ${this.$u.addUnit(this.borderRadius)}`;
  85. return style;
  86. }
  87. },
  88. created() {
  89. // 引用所有子组件(u-dropdown-item)的this,不能在data中声明变量,否则在微信小程序会造成循环引用而报错
  90. this.children = [];
  91. },
  92. mounted() {
  93. this.getContentHeight();
  94. },
  95. // #ifdef VUE3
  96. emits: ['open', 'close'],
  97. // #endif
  98. methods: {
  99. init() {
  100. // 当某个子组件内容变化时,触发父组件的init,父组件再让每一个子组件重新初始化一遍
  101. // 以保证数据的正确性
  102. this.menuList = [];
  103. this.children.map(child => {
  104. child.init();
  105. })
  106. },
  107. // 点击菜单
  108. menuClick(index) {
  109. // 判断是否被禁用
  110. if (this.menuList[index].disabled) return;
  111. // 如果点击时的索引和当前激活项索引相同,意味着点击了激活项,需要收起下拉菜单
  112. if (index === this.current && this.closeOnClickSelf) {
  113. this.close();
  114. // 等动画结束后,再移除下拉菜单中的内容,否则直接移除,也就没有下拉菜单收起的效果了
  115. setTimeout(() => {
  116. this.children[index].active = false;
  117. }, this.duration)
  118. return;
  119. }
  120. this.open(index);
  121. },
  122. // 打开下拉菜单
  123. open(index) {
  124. // 嵌套popup使用时可能获取不到正确的高度,重新计算
  125. if (this.contentHeight < 1) this.getContentHeight()
  126. // 重置高亮索引,否则会造成多个菜单同时高亮
  127. // this.highlightIndex = 9999;
  128. // 展开时,设置下拉内容的样式
  129. this.contentStyle.opacity = 1;
  130. this.contentStyle.zIndex = 11;
  131. this.contentStyle.height = this.$u.addUnit(this.contentHeight);
  132. // #ifndef APP-NVUE
  133. this.contentStyle.transition = `opacity ${this.duration / 1000}s linear`;
  134. this.contentStyle.top = uni.$u.addUnit(this.height),
  135. // #endif
  136. // #ifdef H5
  137. document.body.style.overflow = 'hidden';
  138. // #endif
  139. // 标记展开状态以及当前展开项的索引
  140. this.active = true;
  141. this.current = index;
  142. // 历遍所有的子元素,将索引匹配的项标记为激活状态,因为子元素是通过v-if控制切换的
  143. // 之所以不是因display: none,是因为nvue没有display这个属性
  144. this.children.map((val, idx) => {
  145. val.active = index == idx ? true : false;
  146. })
  147. this.$emit('open', this.current);
  148. },
  149. // 设置下拉菜单处于收起状态
  150. close() {
  151. this.$emit('close', this.current);
  152. // 设置为收起状态,同时current归位,设置为空字符串
  153. this.active = false;
  154. this.current = 99999;
  155. // 下拉内容的样式进行调整,不透明度设置为0
  156. this.contentStyle.zIndex = -1;
  157. this.contentStyle.opacity = 0;
  158. // #ifdef H5
  159. document.body.style.overflow = '';
  160. // #endif
  161. setTimeout(() => {
  162. this.contentStyle.height = 0;
  163. }, this.duration)
  164. },
  165. // 点击遮罩
  166. maskClick() {
  167. // 如果不允许点击遮罩,直接返回
  168. if (!this.closeOnClickMask) return;
  169. this.close();
  170. },
  171. // 外部手动设置某个菜单高亮
  172. highlight(index = undefined) {
  173. this.highlightIndex = index !== undefined ? index : 99999;
  174. },
  175. // 获取下拉菜单内容的高度
  176. async getContentHeight() {
  177. // 这里的原理为,因为dropdown组件是相对定位的,它的下拉出来的内容,必须给定一个高度
  178. // 才能让遮罩占满菜单一下,直到屏幕底部的高度
  179. // this.$u.window()为uView封装的获取设备信息的方法
  180. await uni.$u.sleep(30);
  181. let windowHeight = this.$u.window().windowHeight;
  182. // #ifndef APP-NVUE
  183. this.$uGetRect('.u-dropdown__menu').then(res => {
  184. // 这里获取的是dropdown的尺寸,在H5上,uniapp获取尺寸是有bug的(以前提出修复过,后来又出现了此bug,目前hx2.8.11版本)
  185. // H5端bug表现为元素尺寸的top值为导航栏底部到到元素的上边沿的距离,但是元素的bottom值确是导航栏顶部到元素底部的距离
  186. // 二者是互相矛盾的,本质原因是H5端导航栏非原生,uni的开发者大意造成
  187. // 这里取菜单栏的botton值合理的,不能用res.top,否则页面会造成滚动
  188. this.contentHeight = windowHeight - res.bottom;
  189. });
  190. // #endif
  191. // #ifdef APP-NVUE
  192. const ref = this.$refs['u-dropdown__menu']
  193. ref && dom.getComponentRect(ref, (res) => {
  194. this.contentHeight = windowHeight - res.size.bottom
  195. });
  196. // #endif
  197. }
  198. }
  199. }
  200. </script>
  201. <style scoped lang="scss">
  202. @import '../../libs/css/components.scss';
  203. .u-dropdown {
  204. flex: 1;
  205. // #ifndef APP-NVUE
  206. width: 100%;
  207. // #endif
  208. position: relative;
  209. &__menu {
  210. @include flex(row);
  211. position: relative;
  212. z-index: 11;
  213. height: 40px;
  214. &__item {
  215. flex: 1;
  216. @include flex(row);
  217. justify-content: center;
  218. align-items: center;
  219. &__text {
  220. font-size: 14px;
  221. color: $u-content-color;
  222. }
  223. &__arrow {
  224. margin-left: 3px;
  225. transition: transform .3s;
  226. align-items: center;
  227. @include flex(row);
  228. &--rotate {
  229. transform: rotate(180deg);
  230. }
  231. }
  232. }
  233. }
  234. &__content {
  235. z-index: 8;
  236. right: 0;
  237. left: 0;
  238. bottom: 0;
  239. overflow: hidden;
  240. // #ifndef APP-NVUE
  241. position: absolute;
  242. width: 100%;
  243. // #endif
  244. // #ifdef APP-NVUE
  245. position: fixed;
  246. // #endif
  247. &__mask {
  248. position: absolute;
  249. z-index: 9;
  250. background: rgba(0, 0, 0, .3);
  251. right: 0;
  252. left: 0;
  253. top: 0;
  254. bottom: 0;
  255. // #ifndef APP-NVUE
  256. width: 100%;
  257. // #endif
  258. }
  259. &__popup {
  260. background-color: #fff;
  261. position: relative;
  262. z-index: 100;
  263. // #ifndef APP-NVUE
  264. transition: all 0.3s;
  265. transform: translate3D(0, -100%, 0);
  266. // #endif
  267. overflow: hidden;
  268. }
  269. }
  270. }
  271. </style>