uni-dropdown.vue 9.5 KB

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