modal.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <view @touchmove.stop.prevent>
  3. <view class="modal-box" :style="{width:props.width,padding:props.padding,borderRadius:props.radius}" :class="[(fadein || props.show)?'modal-normal':'modal-scale',props.show?'modal-show':'']">
  4. <view v-if="props.custom">
  5. <slot></slot>
  6. </view>
  7. <view v-else>
  8. <view class="modal-title" v-if="props.title">{{props.title}}</view>
  9. <view class="modal-content" :class="[props.title?'':'mtop']" :style="{color:props.color,fontSize:props.size+'rpx'}">
  10. <slot></slot>
  11. </view>
  12. <view class="modalBtn-box" :class="[props.button.length!=2?'flex-column':'']">
  13. <block v-for="(item,index) in props.button" :key="index">
  14. <button class="modal-btn"
  15. :class="[
  16. ''+(item.type || 'primary')+(item.plain?'-outline':''),
  17. props.button.length!=2?'btn-width':'',
  18. props.button.length>2?'mbtm':'',
  19. props.shape=='circle'?'circle-btn':'',
  20. 'btn-' + (item.size || 'default'),
  21. ]"
  22. :hover-class="''+(item.plain?'outline':(item.type || 'primary'))+'-hover'" :data-index="index" @tap="handleClick">{{item.text || "确定"}}</button>
  23. </block>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="modal-mask" :class="[props.show?'mask-show':'']" @tap="handleClickCancel"></view>
  28. </view>
  29. </template>
  30. <script setup>
  31. import { ref } from 'vue'
  32. //const props = defineProps(['show','custom','width','padding','radius','title','content','color','size','shape','button','maskClosable','fadein'])
  33. const props = defineProps ({
  34. show: {type: Boolean,default: false},
  35. custom: {type: Boolean,default: false},
  36. width: {type: String,default: "84%"},
  37. padding: {type: String,default: "40rpx"},
  38. radius: {type: String,default: "24rpx"},
  39. title: {type: String,default: ""},
  40. content: {type: String,default: ""},
  41. color: {type: String,default: "#999"},
  42. size: {type: Number,default: 28},
  43. shape: {type: String,default: 'square'}, //形状 circle, square
  44. button: {type: Array,
  45. default: function() {
  46. return [{
  47. text: "取消",
  48. type: "red",
  49. plain: true //是否空心
  50. }, {
  51. text: "确定",
  52. type: "red",
  53. plain: false
  54. }]
  55. }},
  56. maskClosable: {type: Boolean,default: true},
  57. custom: {type: Boolean,default: false},
  58. fadein: {type: Boolean,default: false} //淡入效果,自定义弹框插入input输入框时传true
  59. })
  60. // const show = ref(props.show)
  61. // const maskClosable = ref(props.maskClosable)
  62. const emit = defineEmits(['click','cancel','test'])
  63. const handleClick = (e) => {
  64. if (!props.show) return;
  65. const dataset = e.currentTarget.dataset;
  66. emit('click', {
  67. index: Number(dataset.index)
  68. });
  69. }
  70. const handleClickCancel = () => {
  71. if (!props.maskClosable) return;
  72. emit('cancel');
  73. }
  74. </script>
  75. <style lang="scss">
  76. .modal-box {
  77. position: fixed;
  78. left: 50%;
  79. top: 50%;
  80. margin: auto;
  81. background: #fff;
  82. z-index: 201;
  83. transition: all 0.3s ease-in-out;
  84. opacity: 0;
  85. box-sizing: border-box;
  86. visibility: hidden;
  87. }
  88. .modal-scale {
  89. transform: translate(-50%, -50%) scale(0);
  90. }
  91. .modal-normal {
  92. transform: translate(-50%, -50%) scale(1);
  93. }
  94. .modal-show {
  95. opacity: 1;
  96. visibility: visible;
  97. }
  98. .modal-mask {
  99. position: fixed;
  100. top: 0;
  101. left: 0;
  102. right: 0;
  103. bottom: 0;
  104. background: rgba(0, 0, 0, 0.6);
  105. z-index: 98;
  106. transition: all 0.3s ease-in-out;
  107. opacity: 0;
  108. visibility: hidden;
  109. }
  110. .mask-show {
  111. visibility: visible;
  112. opacity: 1;
  113. }
  114. .modal-title {
  115. text-align: center;
  116. font-size: 34rpx;
  117. color: #333;
  118. padding-top: 20rpx;
  119. font-weight: bold;
  120. }
  121. .modal-content {
  122. color: #999;
  123. font-size: 28rpx;
  124. padding-top: 20rpx;
  125. padding-bottom: 60rpx;
  126. }
  127. .mtop {
  128. margin-top: 30rpx;
  129. }
  130. .mbtm {
  131. margin-bottom: 30rpx;
  132. }
  133. .modalBtn-box {
  134. width: 100%;
  135. display: flex;
  136. align-items: center;
  137. justify-content: space-between
  138. }
  139. .flex-column {
  140. flex-direction: column;
  141. }
  142. .modal-btn {
  143. width: 46%;
  144. height: 68rpx;
  145. line-height: 68rpx;
  146. position: relative;
  147. border-radius: 60rpx;
  148. font-size: 28rpx;
  149. overflow: visible;
  150. margin-left: 0;
  151. margin-right: 0;
  152. &.btn-default {
  153. font-size: 28rpx;
  154. }
  155. &.btn-lg {
  156. font-size: 32rpx;
  157. }
  158. &.btn-sm {
  159. font-size: 24rpx;
  160. }
  161. }
  162. .modal-btn::after {
  163. content: "";
  164. position: absolute;
  165. width: 200%;
  166. height: 200%;
  167. -webkit-transform-origin: 0 0;
  168. transform-origin: 0 0;
  169. -webkit-transform: scale(0.5, 0.5);
  170. transform: scale(0.5, 0.5);
  171. left: 0;
  172. top: 0;
  173. border-radius: 60rpx;
  174. }
  175. .btn-width {
  176. width: 80% !important;
  177. }
  178. .primary {
  179. background: #97AF13;
  180. color: #fff;
  181. }
  182. .primary-hover {
  183. background: #97AF13;
  184. color: #e5e5e5;
  185. }
  186. .primary-outline {
  187. color: #97AF13;
  188. background: none;
  189. }
  190. .primary-outline::after {
  191. border: 1px solid #97AF13;
  192. }
  193. .danger {
  194. background: #ed3f14;
  195. color: #fff;
  196. }
  197. .danger-hover {
  198. background: #d53912;
  199. color: #e5e5e5;
  200. }
  201. .danger-outline {
  202. color: #ed3f14;
  203. background: none;
  204. }
  205. .danger-outline::after {
  206. border: 1px solid #ed3f14;
  207. }
  208. .red {
  209. background: #e41f19;
  210. color: #fff;
  211. }
  212. .red-hover {
  213. background: #c51a15;
  214. color: #e5e5e5;
  215. }
  216. .red-outline {
  217. color: #e41f19;
  218. background: none;
  219. }
  220. .red-outline::after {
  221. border: 1px solid #e41f19;
  222. }
  223. .warning {
  224. background: #ff7900;
  225. color: #fff;
  226. }
  227. .warning-hover {
  228. background: #e56d00;
  229. color: #e5e5e5;
  230. }
  231. .warning-outline {
  232. color: #ff7900;
  233. background: none;
  234. }
  235. .warning-outline::after {
  236. border: 1px solid #ff7900;
  237. }
  238. .green {
  239. background: #19be6b;
  240. color: #fff;
  241. }
  242. .green-hover {
  243. background: #16ab60;
  244. color: #e5e5e5;
  245. }
  246. .green-outline {
  247. color: #19be6b;
  248. background: none;
  249. }
  250. .green-outline::after {
  251. border: 1px solid #19be6b;
  252. }
  253. .white {
  254. background: #fff;
  255. color: #333;
  256. }
  257. .white-hover {
  258. background: #f7f7f9;
  259. color: #666;
  260. }
  261. .white-outline {
  262. color: #333;
  263. background: none;
  264. }
  265. .white-outline::after {
  266. border: 1px solid #333;
  267. }
  268. .gray {
  269. background: #ededed;
  270. color: #999;
  271. }
  272. .gray-hover {
  273. background: #d5d5d5;
  274. color: #898989;
  275. }
  276. .gray-outline {
  277. color: #999;
  278. background: none;
  279. }
  280. .gray-outline::after {
  281. border: 1px solid #999;
  282. }
  283. .outline-hover {
  284. opacity: 0.6;
  285. }
  286. .circle-btn {
  287. border-radius: 40rpx !important;
  288. }
  289. .circle-btn::after {
  290. border-radius: 80rpx !important;
  291. }
  292. </style>