dt-dropdown.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view>
  3. <view @click="showShadow" class="dropWrap">{{ list[current].name }}<view class="sanjiao"><image src="../../static/more.png" mode=""></image></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.name }}</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: 145px;
  49. // margin: 0 3px;
  50. height: 30px;
  51. // border: 1px solid #e8ecef;
  52. font-size: 12px;
  53. // color: #8c9fad;
  54. background: #F7F7F7;
  55. color: #138E57;
  56. border-radius: 3px;
  57. padding: 4px 8px;
  58. display: flex;
  59. align-items: center;
  60. justify-content: space-between;
  61. .sanjiao{
  62. // width: 0;
  63. // height: 0;
  64. // border-left: 6px solid transparent;
  65. // border-right:6px solid transparent;
  66. // border-top:6px solid #C5CFD5;
  67. // border-bottom:6px solid transparent;
  68. // transform: translateY(3px);
  69. transform: rotate(90deg);
  70. image{
  71. width: 13px;
  72. height: 18px;
  73. }
  74. }
  75. }
  76. .dropdown {
  77. position: absolute;
  78. z-index: 100;
  79. .ul {
  80. width: 145px;
  81. position: relative;
  82. z-index: 101;
  83. list-style: none;
  84. background-color: #fff;
  85. border-radius: 4rpx;
  86. padding-left: 0;
  87. box-shadow: 6rpx 6rpx 10rpx rgba(122, 122, 122, 0.2);
  88. transition: all 0.2s;
  89. height: 0;
  90. overflow: hidden;
  91. .li {
  92. box-sizing: border-box;
  93. color: #000;
  94. height: 30px;
  95. border-bottom: 1px solid #e6eaeb;
  96. font-size: 24rpx;
  97. line-height: 30px; //与下面的高度保持一致
  98. padding-left: 8px;
  99. }
  100. .li:last-child {
  101. border-bottom: none;
  102. }
  103. }
  104. .show {
  105. height: calc( var(--i) * 30px ); //与上面的高度保持一致
  106. }
  107. .dropdown-mask {
  108. top: 0;
  109. left: 0;
  110. position: fixed;
  111. width: 100vw;
  112. height: 100vh;
  113. z-index: 100;
  114. pointer-events: auto;
  115. }
  116. .undropdown-mask {
  117. pointer-events: none;
  118. }
  119. }
  120. </style>