wyb-drop-down.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <template>
  2. <view
  3. class="wyb-drop-down-box"
  4. :style="{
  5. '--duration': duration + 'ms',
  6. '--autoContentTop': autoContentTop}">
  7. <view class="wyb-drop-down-container" @tap.stop.prevent @touchmove.stop.prevent>
  8. <view
  9. class="wyb-drop-down-header"
  10. :style="{
  11. zIndex: zIndex,
  12. backgroundColor: bgColor.header}">
  13. <view
  14. class="wyb-drop-down-header-item"
  15. v-for="(item,index) in options"
  16. :key="item.header"
  17. @tap.stop="onHeaderTap(index)"
  18. :style="{fontSize: fontSize.header + 'rpx'}">
  19. <text
  20. class="wyb-drop-down-header-item-label"
  21. :style="{
  22. fontWeight: headerActiveIndex === index && dropOver && activeWeight ? 'bold' : 'normal',
  23. color: headerActiveIndex === index && dropOver ? activeColor: defaultColor}">{{item.header}}</text>
  24. <text
  25. v-if="dropIcon === 'fill'"
  26. class="iconfont icon-down-fill wyb-drop-down-header-item-icon"
  27. :class="[headerActiveIndex === index && dropOver ? 'wyb-drop-down-header-item-icon-active' : '']"
  28. :style="{
  29. fontSize: fontSize.header - 5 + 'rpx',
  30. color: headerActiveIndex === index && dropOver ? activeColor: defaultColor}" />
  31. <text
  32. v-if="dropIcon==='line'"
  33. class="iconfont icon-down wyb-drop-down-header-item-icon"
  34. :class="[headerActiveIndex === index && dropOver ? 'wyb-drop-down-header-item-icon-active' : '']"
  35. :style="{
  36. fontSize: fontSize.header - 5 + 'rpx',
  37. transformOrigin: '50% 45%',
  38. color: headerActiveIndex === index && dropOver ? activeColor: defaultColor}" />
  39. <view class="wyb-drop-down-vline" v-if="index !== options.length - 1" />
  40. </view>
  41. </view>
  42. <scroll-view
  43. v-if="dropDown"
  44. class="wyb-drop-down-content"
  45. :class="[dropOver ? 'wyb-drop-down-content-active' : '']"
  46. :scroll-y="scroll"
  47. :enable-flex="true"
  48. :scroll-anchoring="true"
  49. :style="{
  50. zIndex: zIndex - 1,
  51. fontSize: fontSize.content + 'rpx',
  52. backgroundColor: bgColor.content,
  53. borderBottomLeftRadius: radius + 'px',
  54. borderBottomRightRadius: radius + 'px',
  55. minHeight: minHeight + 'rpx',
  56. height: autoHeight ? 'auto' : minHeight + 'rpx',
  57. maxHeight: autoHeight && maxHeight ? maxHeight + 'rpx' : 'auto'}">
  58. <view class="wyb-drop-down-content-box" v-for="(item,index) in options" :key="contentBoxKey(index)">
  59. <view v-if="item['custom'] && headerActiveIndex === index && dropDown" class="wyb-drop-down-content-slot">
  60. <slot></slot>
  61. </view>
  62. <view
  63. v-if="!item['custom'] && headerActiveIndex === index && dropDown"
  64. class="wyb-drop-down-content-item"
  65. v-for="(content,zIndex) in item['contents']"
  66. :key="content"
  67. @tap.stop="onContentItemsTap(zIndex)">
  68. <text
  69. class="wyb-drop-down-content-item-label"
  70. :style="{color: contentActiveIndexList[headerActiveIndex]['index'] === zIndex && dropOver ? activeColor: defaultColor}">
  71. {{content}}
  72. </text>
  73. <text
  74. v-if="contentActiveIndexList[headerActiveIndex]['index'] === zIndex && dropOver"
  75. :style="{color: activeColor}"
  76. class="iconfont icon-selected wyb-drop-down-content-item-icon" />
  77. <view class="wyb-drop-down-line" v-if="zIndex !== options[headerActiveIndex].contents.length - 1" />
  78. </view>
  79. </view>
  80. </scroll-view>
  81. </view>
  82. <view
  83. v-if="dropDown"
  84. class="wyb-drop-down-mask"
  85. :class="[dropOver ? 'wyb-drop-down-mask-active' : '']"
  86. @tap.stop="close"
  87. @touchmove.stop.prevent
  88. :style="{
  89. zIndex: zIndex - 2,
  90. height: screenHeight + 'px',
  91. backgroundColor: 'rgba(0, 0, 0, ' + maskAlpha + ')'}" />
  92. </view>
  93. </template>
  94. <script>
  95. export default {
  96. data() {
  97. return {
  98. dropDown: false,
  99. dropOver: false,
  100. duration: 500,
  101. contents: this.options[0].contents || [0],
  102. headerActiveIndex: 0,
  103. contentActiveIndexList: []
  104. }
  105. },
  106. computed: {
  107. autoContentTop() {
  108. return `${44 + this.rpxToPx(100)}px`
  109. },
  110. screenHeight() {
  111. return uni.getSystemInfoSync().screenHeight
  112. },
  113. screenWidth() {
  114. return uni.getSystemInfoSync().screenWidth
  115. },
  116. contentBoxKey() {
  117. return function(index) {
  118. return `option${index}`
  119. }
  120. }
  121. },
  122. props: {
  123. options: {
  124. type: Array,
  125. default() {
  126. return [{
  127. header: 'A',
  128. contents: ['1', '2']
  129. }]
  130. }
  131. },
  132. defaultIndexList: {
  133. type: Array,
  134. default() {
  135. return []
  136. }
  137. },
  138. autoHeight: {
  139. type: Boolean,
  140. default: true
  141. },
  142. minHeight: {
  143. type: [String, Number],
  144. default: 10
  145. },
  146. maxHeight: {
  147. type: [String, Number],
  148. default: 600
  149. },
  150. scroll: {
  151. type: Boolean,
  152. default: true
  153. },
  154. radius: {
  155. type: [String, Number],
  156. default: '0'
  157. },
  158. activeColor: {
  159. type: String,
  160. default: '#e1c0a0'
  161. },
  162. activeWeight: {
  163. type: Boolean,
  164. default: true
  165. },
  166. defaultColor: {
  167. type: String,
  168. default: '#fff'
  169. },
  170. bgColor: {
  171. type: Object,
  172. default() {
  173. return {
  174. header: '#fff',
  175. content: '#fff'
  176. }
  177. }
  178. },
  179. dropIcon: {
  180. type: String,
  181. default: 'fill'
  182. },
  183. fontSize: {
  184. type: Object,
  185. default() {
  186. return {
  187. header: 30,
  188. content: 30
  189. }
  190. }
  191. },
  192. maskAlpha: {
  193. type: [String, Number],
  194. default: '0.5'
  195. },
  196. zIndex: {
  197. type: Number,
  198. default: 500
  199. }
  200. },
  201. mounted() {
  202. if (this.defaultIndexList.length === 0) {
  203. this.options.forEach((item, index) => {
  204. if (!item.custom) {
  205. this.contentActiveIndexList.push({headerIndex: index, index: 0})
  206. } else {
  207. this.contentActiveIndexList.push({headerIndex: index, custom: true})
  208. }
  209. })
  210. } else {
  211. let i = 0
  212. this.options.forEach((item, index) => {
  213. if (!item.custom) {
  214. this.contentActiveIndexList.push([...this.defaultIndexList][i])
  215. i++
  216. } else {
  217. this.contentActiveIndexList.push({headerIndex: index, custom: true})
  218. }
  219. })
  220. }
  221. },
  222. methods: {
  223. onHeaderTap(index) {
  224. let item = this.options[index]
  225. if (Object.is(this.headerActiveIndex, index) && this.dropOver) {
  226. this.close()
  227. } else {
  228. this.headerActiveIndex = index
  229. if (item.custom) {
  230. this.$emit('change', {
  231. headerIndex: index,
  232. header: this.options[index].header
  233. })
  234. }
  235. this.dropDown = true
  236. this.$nextTick(() => {
  237. this.dropOver = true
  238. this.$emit('show')
  239. })
  240. }
  241. },
  242. onContentItemsTap(index) {
  243. this.contentActiveIndexList[this.headerActiveIndex]['index'] = index
  244. this.$forceUpdate()
  245. let event = {
  246. headerIndex: this.headerActiveIndex,
  247. header: this.options[this.headerActiveIndex]['header'],
  248. contentIndex: index,
  249. content: this.options[this.headerActiveIndex]['contents'][index],
  250. contentActiveIndexList: this.contentActiveIndexList
  251. }
  252. this.$emit('select', event)
  253. },
  254. close() {
  255. this.dropOver = false
  256. setTimeout(() => {
  257. this.dropDown = false
  258. this.$emit('hide')
  259. }, this.duration)
  260. },
  261. rpxToPx(rpx) {
  262. return rpx / 750 * this.screenWidth
  263. }
  264. }
  265. }
  266. </script>
  267. <style>
  268. @import './iconfont.css';
  269. .wyb-drop-down-mask {
  270. position: fixed;
  271. top: 44px;
  272. /* #ifndef H5 */
  273. top: 0;
  274. /* #endif */
  275. left: 0;
  276. bottom: 0;
  277. right: 0;
  278. opacity: 0;
  279. transition: opacity var(--duration);
  280. z-index: 498;
  281. }
  282. .wyb-drop-down-mask-active {
  283. opacity: 1;
  284. transition: opacity var(--duration);
  285. }
  286. .wyb-drop-down-header {
  287. position: fixed;
  288. top: 44px;
  289. /* #ifndef H5 */
  290. top: 0;
  291. /* #endif */
  292. left: 0;
  293. right: 0;
  294. display: flex;
  295. flex-direction: row;
  296. background-color: #fff;
  297. z-index: 500;
  298. }
  299. .wyb-drop-down-header-item {
  300. flex: 1;
  301. height: 100rpx;
  302. font-size: 30rpx;
  303. border-bottom: 1px solid #eee;
  304. display: flex;
  305. flex-direction: row;
  306. align-items: center;
  307. justify-content: center;
  308. position: relative;
  309. background: #000 !important;
  310. }
  311. .wyb-drop-down-header-item-label {
  312. color: #fff !important;
  313. display: flex;
  314. flex-direction: row;
  315. align-items: center;
  316. justify-content: flex-end;
  317. }
  318. .wyb-drop-down-header-item-icon {
  319. color: #fff !important;
  320. margin-left: 20rpx;
  321. display: flex;
  322. flex-direction: row;
  323. align-items: center;
  324. justify-content: flex-start;
  325. transform-origin: 50% 40%;
  326. transform: rotate(0);
  327. transition: transform var(--duration);
  328. }
  329. .wyb-drop-down-header-item-icon-active {
  330. transform: rotate(180deg);
  331. transition: transform var(--duration);
  332. }
  333. .wyb-drop-down-content {
  334. position: fixed;
  335. top: var(--autoContentTop);
  336. /* #ifndef H5 */
  337. top: 100rpx;
  338. /* #endif */
  339. left: 0;
  340. right: 0;
  341. z-index: 499;
  342. display: flex;
  343. flex-direction: column;
  344. align-items: flex-start;
  345. background-color: #fff;
  346. transform: translateY(-100%);
  347. transition: transform var(--duration);
  348. }
  349. .wyb-drop-down-content-active {
  350. transform: translateY(0);
  351. transition: transform var(--duration);
  352. }
  353. .wyb-drop-down-content-item {
  354. background: #000;
  355. color: #fff;
  356. width: 100%;
  357. height: 100rpx;
  358. font-size: 30rpx;
  359. display: flex;
  360. flex-direction: column;
  361. position: relative;
  362. }
  363. .wyb-drop-down-content-item-label {
  364. width: 90%;
  365. height: 100%;
  366. display: flex;
  367. flex-direction: row;
  368. align-items: center;
  369. justify-content: flex-start;
  370. padding-left: 50rpx;
  371. }
  372. .wyb-drop-down-content-item-icon {
  373. position: absolute;
  374. top: 50%;
  375. right: 40rpx;
  376. font-size: 40rpx;
  377. transform: translateY(-50%);
  378. }
  379. .wyb-drop-down-content-box {
  380. padding-top: var(--status-bar-height);
  381. width: 100%;
  382. background-color: #000;
  383. }
  384. .wyb-drop-down-content-slot {
  385. width: 100%;
  386. height: 100%;
  387. }
  388. .wyb-drop-down-vline {
  389. width: 1px;
  390. height: 40rpx;
  391. background-color: #eee;
  392. position: absolute;
  393. right: 0;
  394. }
  395. .wyb-drop-down-line {
  396. width: 100%;
  397. height: 1px;
  398. background-color: #eee;
  399. margin-left: 50rpx;
  400. }
  401. </style>