ActionSheet.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. <slot>
  8. <view class="action-sheet-item" v-for="(item, index) in items" :key="index"
  9. :class="{ 'action-sheet-item-danger': item.danger }" @click.stop="handleItemClick(index, item)">
  10. <image class="action-sheet-item-icon fa" v-if="item.icon" :src="item.icon"></image>
  11. <text class="action-sheet-item-text">{{ item.text }}</text>
  12. </view>
  13. </slot>
  14. </view>
  15. <view class="action-sheet-cancel" v-if="false" @click.stop="handleCancel">{{ cancelText }}</view>
  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. }
  50. },
  51. methods: {
  52. // 显示ActionSheet
  53. show() {
  54. this.visible = true;
  55. setTimeout(() => {
  56. this.animationShow = true;
  57. }, 10);
  58. },
  59. // 隐藏ActionSheet
  60. hide() {
  61. this.animationShow = false;
  62. setTimeout(() => {
  63. this.visible = false;
  64. }, 300); // 动画持续时间
  65. },
  66. // 处理选项点击
  67. handleItemClick(index, item) {
  68. this.$emit('select', index, item);
  69. this.hide();
  70. },
  71. // 处理取消按钮点击
  72. handleCancel() {
  73. this.$emit('cancel');
  74. this.hide();
  75. },
  76. // 处理遮罩点击
  77. handleMaskClick() {
  78. if (this.maskClosable) {
  79. this.$emit('cancel');
  80. this.hide();
  81. }
  82. }
  83. }
  84. }
  85. </script>
  86. <style lang="scss">
  87. .action-sheet-container {
  88. position: fixed;
  89. top: 0;
  90. right: 0;
  91. bottom: 0;
  92. left: 0;
  93. z-index: 999;
  94. .action-sheet-mask {
  95. position: absolute;
  96. top: 0;
  97. right: 0;
  98. bottom: 0;
  99. left: 0;
  100. background-color: rgba(0, 0, 0, 0.5);
  101. }
  102. .action-sheet {
  103. position: absolute;
  104. left: 0;
  105. right: 0;
  106. bottom: 0;
  107. transform: translateY(100%);
  108. transition: transform 0.3s ease;
  109. background: #F2F6F2;
  110. border-radius: 24rpx 24rpx 0 0;
  111. overflow: hidden;
  112. padding: 60rpx 32rpx;
  113. &.action-sheet-show {
  114. transform: translateY(0);
  115. }
  116. .action-sheet-title {
  117. padding: 24rpx;
  118. text-align: center;
  119. font-size: 28rpx;
  120. color: #999;
  121. background-color: #fff;
  122. border-bottom: 1rpx solid #eee;
  123. }
  124. .action-sheet-items {
  125. .action-sheet-item {
  126. background-color: #fff;
  127. display: flex;
  128. align-items: center;
  129. justify-content: start;
  130. height: 110rpx;
  131. font-size: 32rpx;
  132. color: #333;
  133. border-bottom: 1rpx solid #eee;
  134. box-sizing: border-box;
  135. padding-left: 30rpx;
  136. &:active {
  137. background-color: #f2f2f2;
  138. }
  139. &.action-sheet-item-danger {
  140. color: #ff5151;
  141. margin-top: 16rpx;
  142. border-radius: 20rpx;
  143. }
  144. .action-sheet-item-icon {
  145. margin-right: 16rpx;
  146. width: 36rpx;
  147. height: 36rpx;
  148. }
  149. .action-sheet-item-text {
  150. font-size: 32rpx;
  151. }
  152. &:first-child {
  153. border-top-right-radius: 20rpx;
  154. border-top-left-radius: 20rpx;
  155. }
  156. &:nth-child(2) {
  157. border-bottom-right-radius: 20rpx;
  158. border-bottom-left-radius: 20rpx;
  159. }
  160. &:last-child {
  161. border-bottom-right-radius: 20rpx;
  162. border-bottom-left-radius: 20rpx;
  163. }
  164. }
  165. }
  166. .action-sheet-cancel {
  167. height: 110rpx;
  168. line-height: 110rpx;
  169. text-align: center;
  170. font-size: 32rpx;
  171. color: #333;
  172. background-color: #fff;
  173. margin-top: 16rpx;
  174. &:active {
  175. background-color: #f2f2f2;
  176. }
  177. }
  178. .action-sheet-line {
  179. position: absolute;
  180. top: 16rpx;
  181. left: 50%;
  182. transform: translateX(-50%);
  183. width: 62rpx;
  184. height: 8rpx;
  185. background: #D1DED1;
  186. border-radius: 224rpx;
  187. }
  188. }
  189. }
  190. </style>