u-dropdown.vue 9.2 KB

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