dt-dropdown.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view>
  3. <view @click="showShadow" class="dropWrap">{{ list[current] }}<view class="sanjiao"></view></view>
  4. <view class="dropdown">
  5. <view :class="showIf ? 'dropdown-mask' : 'undropdown-mask'" @click="hideShadow"></view>
  6. <!-- <view class="ul" :style="showIf?'height:'+list.length*30+'px':''"> -->
  7. <view class="ul" :style="'--i:'+list.length" :class="showIf?'show':''"> <!-- 不支持就用上面那种 -->
  8. <view class="li" v-for="(item, index) in list" :key="index" @click="handlerItem(index)">{{ item }}</view>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'dropdown',
  16. props: {
  17. list: {
  18. type: Array,
  19. default: () => []
  20. },
  21. current: {
  22. type: [String, Number],
  23. default: 0
  24. }
  25. },
  26. data() {
  27. return {
  28. showIf: false
  29. };
  30. },
  31. methods: {
  32. handlerItem(value) {
  33. this.showIf = false
  34. this.$emit('onClick', value);
  35. },
  36. hideShadow() {
  37. this.showIf = false;
  38. },
  39. showShadow() {
  40. this.showIf = true;
  41. }
  42. }
  43. };
  44. </script>
  45. <style scoped lang="scss">
  46. .dropWrap {
  47. box-sizing: border-box;
  48. width:400rpx;
  49. height: 26px;
  50. border: 1px solid #e8ecef;
  51. font-size: 12px;
  52. color: #8c9fad;
  53. padding: 4px 8px;
  54. display: flex;
  55. align-items: center;
  56. justify-content: space-between;
  57. .sanjiao{
  58. width: 0;
  59. height: 0;
  60. border-left: 6px solid transparent;
  61. border-right:6px solid transparent;
  62. border-top:6px solid #C5CFD5;
  63. border-bottom:6px solid transparent;
  64. transform: translateY(3px);
  65. }
  66. }
  67. .dropdown {
  68. position: absolute;
  69. z-index: 100;
  70. .ul {
  71. width: 400rpx;
  72. position: relative;
  73. z-index: 101;
  74. list-style: none;
  75. background-color: #fff;
  76. border-radius: 4rpx;
  77. padding-left: 0;
  78. box-shadow: 6rpx 6rpx 10rpx rgba(122, 122, 122, 0.2);
  79. transition: all 0.2s;
  80. height: 0;
  81. overflow: hidden;
  82. .li {
  83. box-sizing: border-box;
  84. color: #000;
  85. height: 30px;
  86. border-bottom: 1px solid #e6eaeb;
  87. font-size: 24rpx;
  88. line-height: 30px; //与下面的高度保持一致
  89. padding-left: 8px;
  90. }
  91. .li:last-child {
  92. border-bottom: none;
  93. }
  94. }
  95. .show {
  96. height: calc( var(--i) * 30px ); //与上面的高度保持一致
  97. }
  98. .dropdown-mask {
  99. top: 0;
  100. left: 0;
  101. position: fixed;
  102. width: 100vw;
  103. height: 100vh;
  104. z-index: 100;
  105. pointer-events: auto;
  106. }
  107. .undropdown-mask {
  108. pointer-events: none;
  109. }
  110. }
  111. </style>