u-dropdown.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <view class="u-dropdown">
  3. <view class="u-dropdown__menu" :style="{
  4. height: $u.addUnit(height)
  5. }" :class="{
  6. 'u-border-bottom': borderBottom
  7. }">
  8. <view class="u-dropdown__menu__item" v-for="(item, index) in menuList" :key="index" @tap.stop="menuClick(index)">
  9. <view class="u-flex">
  10. <text class="u-dropdown__menu__item__text" :style="{
  11. color: item.disabled ? '#c0c4cc' : (index === current || highlightIndex == index) ? activeColor : inactiveColor,
  12. fontSize: $u.addUnit(titleSize)
  13. }">{{item.title}}</text>
  14. <view class="u-dropdown__menu__item__arrow" :class="{
  15. 'u-dropdown__menu__item__arrow--rotate': index === current
  16. }">
  17. <u-icon :custom-style="{display: 'flex'}" name="arrow-down" size="26" :color="index === current || highlightIndex == index ? activeColor : '#c0c4cc'"></u-icon>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. <view class="u-dropdown__content" :style="[contentStyle, {
  23. transition: `opacity ${duration / 1000}s linear`
  24. }]" @tap="maskClick" @touchmove.stop.prevent>
  25. <view @tap.stop.prevent class="u-dropdown__content__popup" :style="[popupStyle]">
  26. <slot></slot>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'u-dropdown',
  34. props: {
  35. // 菜单标题和选项的激活态颜色
  36. activeColor: {
  37. type: String,
  38. default: '#2979ff'
  39. },
  40. // 菜单标题和选项的未激活态颜色
  41. inactiveColor: {
  42. type: String,
  43. default: '#606266'
  44. },
  45. // 点击遮罩是否关闭菜单
  46. closeOnClickMask: {
  47. type: Boolean,
  48. default: true
  49. },
  50. // 点击当前激活项标题是否关闭菜单
  51. closeOnClickSelf: {
  52. type: Boolean,
  53. default: true
  54. },
  55. // 过渡时间
  56. duration: {
  57. type: [Number, String],
  58. default: 300
  59. },
  60. // 标题菜单的高度,单位任意,数值默认为rpx单位
  61. height: {
  62. type: [Number, String],
  63. default: 80
  64. },
  65. // 是否显示下边框
  66. borderBottom: {
  67. type: Boolean,
  68. default: false
  69. },
  70. // 标题的字体大小
  71. titleSize: {
  72. type: [Number, String],
  73. default: 28
  74. }
  75. },
  76. data() {
  77. return {
  78. showDropdown: true, // 是否打开下来菜单,
  79. menuList: [], // 显示的菜单
  80. active: false, // 下拉菜单的状态
  81. // 当前是第几个菜单处于激活状态,小程序中此处不能写成false或者"",否则后续将current赋值为0,
  82. // 无能的TX没有使用===而是使用==判断,导致程序认为前后二者没有变化,从而不会触发视图更新
  83. current: 99999,
  84. // 外层内容的样式,初始时处于底层,且透明
  85. contentStyle: {
  86. zIndex: -1,
  87. opacity: 0
  88. },
  89. // 让某个菜单保持高亮的状态
  90. highlightIndex: 99999
  91. }
  92. },
  93. computed: {
  94. // 下拉出来部分的样式
  95. popupStyle() {
  96. let style = {};
  97. // 进行Y轴位移,展开状态时,恢复原位。收齐状态时,往上位移100%,进行隐藏
  98. style.transform = `translateY(${this.active ? 0 : '-100%'})`
  99. return style;
  100. }
  101. },
  102. created() {
  103. // 引用所有子组件(u-dropdown-item)的this,不能在data中声明变量,否则在微信小程序会造成循环引用而报错
  104. this.children = [];
  105. },
  106. methods: {
  107. init() {
  108. // 当某个子组件内容变化时,触发父组件的init,父组件再让每一个子组件重新初始化一遍
  109. // 以保证数据的正确性
  110. this.menuList = [];
  111. this.children.map(child => {
  112. child.init();
  113. })
  114. },
  115. // 点击菜单
  116. menuClick(index) {
  117. // 判断是否被禁用
  118. if(this.menuList[index].disabled) return ;
  119. // 如果点击时的索引和当前激活项索引相同,意味着点击了激活项,需要收起下拉菜单
  120. if (index === this.current && this.closeOnClickSelf) {
  121. this.close();
  122. // 等动画结束后,再移除下拉菜单中的内容,否则直接移除,也就没有下拉菜单收起的效果了
  123. setTimeout(() => {
  124. this.children[index].active = false;
  125. }, this.duration)
  126. return;
  127. }
  128. this.open(index);
  129. },
  130. // 打开下拉菜单
  131. open(index) {
  132. // 重置高亮索引,否则会造成多个菜单同时高亮
  133. // this.highlightIndex = 9999;
  134. // 展开时,设置下拉内容的样式
  135. this.contentStyle = {
  136. zIndex: 11,
  137. opacity: 1
  138. }
  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 = {
  157. zIndex: -1,
  158. opacity: 0
  159. }
  160. },
  161. // 点击遮罩
  162. maskClick() {
  163. // 如果不允许点击遮罩,直接返回
  164. if(!this.closeOnClickMask) return;
  165. this.close();
  166. },
  167. // 外部手动设置某个菜单高亮
  168. highlight(index = undefined) {
  169. this.highlightIndex = index !== undefined ? index : 99999;
  170. }
  171. }
  172. }
  173. </script>
  174. <style scoped lang="scss">
  175. @import "../../libs/css/style.components.scss";
  176. .u-dropdown {
  177. flex: 1;
  178. width: 100%;
  179. &__menu {
  180. display: flex;
  181. position: relative;
  182. z-index: 11;
  183. height: 80rpx;
  184. &__item {
  185. flex: 1;
  186. display: flex;
  187. justify-content: center;
  188. align-items: center;
  189. &__text {
  190. font-size: 28rpx;
  191. color: $u-content-color;
  192. }
  193. &__arrow {
  194. margin-left: 6rpx;
  195. transition: transform .3s;
  196. align-items: center;
  197. display: flex;
  198. &--rotate {
  199. transform: rotate(180deg);
  200. }
  201. }
  202. }
  203. }
  204. &__content {
  205. position: absolute;
  206. z-index: 11;
  207. height: 100%;
  208. width: 100%;
  209. left: 0px;
  210. overflow: hidden;
  211. background: rgba(0, 0, 0, .3);
  212. opacity: 0;
  213. &__popup {
  214. z-index: 11;
  215. transition: all 0.3s;
  216. transform: translate3D(0, -100%, 0);
  217. }
  218. }
  219. }
  220. </style>