ActionSheet.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <view class="action-sheet-container" v-if="visible" @click="handleMaskClick">
  3. <view class="action-sheet-mask" @touchmove.stop.prevent></view>
  4. <view class="action-sheet" :class="{ 'action-sheet-show': animationShow }" @click.stop>
  5. <view class="action-sheet-title" v-if="title">{{ title }}</view>
  6. <view class="action-sheet-items">
  7. <view class="action-sheet-item" v-for="(item, index) in items" :key="index"
  8. :class="{ 'action-sheet-item-danger': item.danger }" @click.stop="handleItemClick(index, item)">
  9. <text class="action-sheet-item-text">{{ item.text }}</text>
  10. <image class="action-sheet-radio-img" :src="selectedIndex === index ? checkedImg : uncheckedImg"
  11. mode="widthFix" />
  12. </view>
  13. </view>
  14. <view class="action-sheet-confirm-btn" @click="handleConfirm">确认</view>
  15. <image class="action-sheet-confirm-img" src="@/static/icon/wd_icon_guanbi.png" @click="handleMaskClick" />
  16. <view class="action-sheet-line"></view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'ActionSheet',
  23. props: {
  24. // 标题
  25. title: {
  26. type: String,
  27. default: ''
  28. },
  29. // 选项列表,格式:[{text: '选项1', icon: 'fa-share-alt', danger: false}, ...]
  30. items: {
  31. type: Array,
  32. default: () => []
  33. },
  34. // 取消按钮文字
  35. cancelText: {
  36. type: String,
  37. default: '取消'
  38. },
  39. // 点击遮罩是否关闭
  40. maskClosable: {
  41. type: Boolean,
  42. default: true
  43. }
  44. },
  45. data() {
  46. return {
  47. visible: false,
  48. animationShow: false,
  49. selectedIndex: null,
  50. checkedImg: require('@/static/icon/wd_icon_gouxuan05.png'), // 选中图片
  51. uncheckedImg: require('@/static/icon/wd_icon_gouxuan04.png') // 未选中图片
  52. }
  53. },
  54. methods: {
  55. // 显示ActionSheet
  56. show() {
  57. this.visible = true;
  58. setTimeout(() => {
  59. this.animationShow = true;
  60. }, 10);
  61. },
  62. // 隐藏ActionSheet
  63. hide() {
  64. this.animationShow = false;
  65. setTimeout(() => {
  66. this.visible = false;
  67. }, 300); // 动画持续时间
  68. },
  69. // 处理选项点击
  70. handleItemClick(index, item) {
  71. this.selectedIndex = index;
  72. },
  73. // 处理确认按钮点击
  74. handleConfirm() {
  75. if (this.selectedIndex !== null) {
  76. this.$emit('select', this.selectedIndex, this.items[this.selectedIndex]);
  77. this.hide();
  78. }
  79. },
  80. // 处理遮罩点击
  81. handleMaskClick() {
  82. if (this.maskClosable) {
  83. this.$emit('cancel');
  84. this.hide();
  85. }
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss">
  91. .action-sheet-container {
  92. position: fixed;
  93. top: 0;
  94. right: 0;
  95. bottom: 0;
  96. left: 0;
  97. z-index: 999;
  98. .action-sheet-mask {
  99. position: absolute;
  100. top: 0;
  101. right: 0;
  102. bottom: 0;
  103. left: 0;
  104. background-color: rgba(0, 0, 0, 0.5);
  105. }
  106. .action-sheet {
  107. position: absolute;
  108. left: 0;
  109. right: 0;
  110. bottom: 0;
  111. transform: translateY(100%);
  112. transition: transform 0.3s ease;
  113. background: #F2F6F2;
  114. border-radius: 24rpx 24rpx 0 0;
  115. overflow: hidden;
  116. padding: 0rpx 32rpx 32rpx 32rpx;
  117. padding-bottom: 70rpx;
  118. &.action-sheet-show {
  119. transform: translateY(0);
  120. }
  121. .action-sheet-title {
  122. padding: 24rpx 0 32rpx 0;
  123. text-align: center;
  124. font-size: 32rpx;
  125. color: #222;
  126. background-color: transparent;
  127. font-weight: bold;
  128. border-bottom: none;
  129. }
  130. .action-sheet-items {
  131. .action-sheet-item {
  132. background-color: #fff;
  133. display: flex;
  134. align-items: center;
  135. height: 90rpx;
  136. font-size: 32rpx;
  137. color: #333;
  138. border-radius: 16rpx;
  139. margin-bottom: 20rpx;
  140. box-sizing: border-box;
  141. justify-content: space-between;
  142. padding: 24rpx 28rpx;
  143. &:active {
  144. background-color: #f2f2f2;
  145. }
  146. &.action-sheet-item-danger {
  147. color: #ff5151;
  148. margin-top: 16rpx;
  149. border-radius: 20rpx;
  150. }
  151. .action-sheet-item-text {
  152. font-size: 32rpx;
  153. }
  154. &:first-child {
  155. border-top-right-radius: 20rpx;
  156. border-top-left-radius: 20rpx;
  157. }
  158. &:nth-child(2) {
  159. border-bottom-right-radius: 20rpx;
  160. border-bottom-left-radius: 20rpx;
  161. }
  162. &:last-child {
  163. border-bottom-right-radius: 20rpx;
  164. border-bottom-left-radius: 20rpx;
  165. }
  166. }
  167. }
  168. .action-sheet-confirm-btn {
  169. height: 90rpx;
  170. line-height: 90rpx;
  171. background: #111;
  172. color: #A6E22E;
  173. border-radius: 45rpx;
  174. text-align: center;
  175. font-size: 36rpx;
  176. font-weight: bold;
  177. letter-spacing: 4rpx;
  178. width: 90%;
  179. margin: 0 auto;
  180. margin-top: 40rpx;
  181. }
  182. .action-sheet-line {
  183. position: absolute;
  184. bottom: 4rpx;
  185. left: 50%;
  186. transform: translateX(-50%);
  187. width: 268rpx;
  188. height: 10rpx;
  189. background: #000000;
  190. border-radius: 200rpx;
  191. }
  192. }
  193. }
  194. .action-sheet-radio-img {
  195. width: 36rpx;
  196. height: 36rpx;
  197. margin-left: 24rpx;
  198. }
  199. .action-sheet-confirm-img {
  200. width: 38rpx;
  201. height: 38rpx;
  202. position: absolute;
  203. right: 28rpx;
  204. top: 24rpx;
  205. border-radius: 50%;
  206. }
  207. </style>