wl-modal.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view class="wl-modal" v-if="visible">
  3. <view class="wl-modal-body" :class="{'modal-center-enter': visible}">
  4. <view class="wl-modal-body-title" v-if="title">
  5. {{title}}
  6. </view>
  7. <view class="wl-modal-body-content" v-if="content" :style="{textAlign: textAlign}">
  8. {{content}}
  9. </view>
  10. <view class="wl-modal-body-button">
  11. <button :open-type="cancelOpenType" class="wl-modal-body-button-item" @tap="cancel" :style="{color: cancelColor}" v-if="showCancel && cancelText">
  12. {{cancelText}}
  13. </button>
  14. <button :open-type="confirmOpenType" class="wl-modal-body-button-item" @tap="confirm" :style="{color: confirmColor}" v-if="confirmText">
  15. {{confirmText}}
  16. </button>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'wl-modal',
  24. data () {
  25. return {
  26. visible: false, // 是否显示
  27. title: '提示', // 提示标题
  28. content: '', // 提示内容
  29. cancelText: '取消', // 取消按钮的文字
  30. confirmText: '确定', // 确认按钮文字
  31. showCancel: true, // 是否显示取消按钮,默认为 true
  32. confirmColor: '#576B95', // 确定按钮颜色
  33. cancelColor: '#666', // 取消按钮颜色
  34. textAlign: 'center' ,// 对齐方式
  35. confirmOpenType: '', // 确定按钮open-type
  36. cancelOpenType: '', // 取消按钮open-type
  37. success: () => {} // 回调方法
  38. }
  39. },
  40. methods: {
  41. // 初始化弹框并打开
  42. showModal(data) {
  43. if (data.title) {
  44. this.title = data.title
  45. } else {
  46. this.title = '提示'
  47. }
  48. if (data.content) {
  49. this.content = data.content
  50. } else {
  51. this.content = ''
  52. }
  53. if (data.cancelText) {
  54. this.cancelText = data.cancelText
  55. } else {
  56. this.cancelText = '取消'
  57. }
  58. if (data.confirmText) {
  59. this.confirmText = data.confirmText
  60. } else {
  61. this.confirmText = '确定'
  62. }
  63. if (data.showCancel === false || data.showCancel === true) {
  64. this.showCancel = data.showCancel
  65. } else {
  66. this.showCancel = true
  67. }
  68. if (data.confirmColor) {
  69. this.confirmColor = data.confirmColor
  70. } else {
  71. this.confirmColor = '#576B95'
  72. }
  73. if (data.cancelColor) {
  74. this.cancelColor = data.cancelColor
  75. } else {
  76. this.cancelColor = '#666'
  77. }
  78. if (data.textAlign) {
  79. this.textAlign = data.textAlign
  80. } else {
  81. this.textAlign = 'center'
  82. }
  83. if (data.confirmOpenType) {
  84. this.confirmOpenType = data.confirmOpenType
  85. } else {
  86. this.confirmOpenType = ''
  87. }
  88. if (data.cancelOpenType) {
  89. this.cancelOpenType = data.cancelOpenType
  90. } else {
  91. this.cancelOpenType = ''
  92. }
  93. if (data.success) {
  94. this.success = data.success
  95. } else {
  96. this.success = () => {}
  97. }
  98. this.visible = true
  99. },
  100. // 取消
  101. cancel() {
  102. this.success({
  103. cancel: true,
  104. confirm: false,
  105. errMsg: 'showModal:ok'
  106. });
  107. this.visible = false;
  108. },
  109. // 确定
  110. confirm() {
  111. this.success({
  112. cancel: false,
  113. confirm: true,
  114. content: null,
  115. errMsg: 'showModal:ok'
  116. });
  117. this.visible = false;
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. // 显示和隐藏效果
  124. @keyframes modal-center-enter {
  125. 0% {
  126. opacity: 0;
  127. transform: scale(0.7);
  128. }
  129. to {
  130. opacity: 1;
  131. }
  132. }
  133. .wl-modal{
  134. width: 100%;
  135. height: 100%;
  136. background-color: rgba($color: #000000, $alpha: 0.6);
  137. display: flex;
  138. justify-content: center;
  139. align-items: center;
  140. position: fixed;
  141. left: 0;
  142. top: 0;
  143. z-index: 1000;
  144. .wl-modal-body{
  145. width: 640rpx;
  146. padding-top: 64rpx;
  147. background-color: #fff;
  148. border-radius: 16rpx;
  149. box-sizing: border-box;
  150. display: table-cell;
  151. vertical-align: middle;
  152. &.modal-center-enter{
  153. animation: modal-center-enter 0.2s ease-out forwards;
  154. }
  155. .wl-modal-body-title{
  156. font-size: 34rpx;
  157. font-weight: 900;
  158. color: #000;
  159. text-align: center;
  160. margin-bottom: 32rpx;
  161. padding: 0 48rpx;
  162. box-sizing: border-box;
  163. }
  164. .wl-modal-body-content{
  165. font-size: 34rpx;
  166. line-height: 48rpx;
  167. color: #666;
  168. word-break:break-all;
  169. word-wrap:break-word;
  170. white-space:pre-wrap;
  171. margin-bottom: 32rpx;
  172. padding: 0 48rpx;
  173. box-sizing: border-box;
  174. }
  175. .wl-modal-body-button{
  176. display: flex;
  177. justify-content: space-between;
  178. align-items: center;
  179. border-top: #E5E5E5 1px solid;
  180. .wl-modal-body-button-item{
  181. &::after {
  182. border: none;
  183. }
  184. border-right: #E5E5E5 1px solid;
  185. background-color: initial;
  186. font-size: 34rpx;
  187. border-radius: 0;
  188. flex: auto;
  189. box-sizing: border-box;
  190. text-align: center;
  191. padding: 30rpx 16rpx;
  192. font-weight: 900;
  193. line-height: normal;
  194. overflow: initial!important;
  195. &:last-child{
  196. border-right: 0;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. </style>