modal.vue 6.2 KB

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