CustomDialog.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view class="custom-dialog" v-if="visible">
  3. <view class="dialog-mask" @click="closeDialog"></view>
  4. <view class="dialog-content" :class="{'dialog-show': visible}" :animation="animationData" :style="{ width: contentWidth }">
  5. <view class="dialog-header" v-if="title && title.length > 0">
  6. <text class="dialog-title">{{title}}</text>
  7. </view>
  8. <view class="dialog-close" @click="closeDialog" v-if="!closeImg">×</view>
  9. <image v-if="closeImg" class="dialog-close-img" :src="closeImg" @click="closeDialog" mode="aspectFit" :style="{ top: closeImgTop && closeImgTop.length > 0 ? closeImgTop : '20rpx' }"></image>
  10. <view class="dialog-body">
  11. <slot></slot>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'CustomDialog',
  19. props: {
  20. visible: {
  21. type: Boolean,
  22. default: false
  23. },
  24. title: {
  25. type: String,
  26. default: '提示'
  27. },
  28. contentWidth: {
  29. type: String,
  30. default: '600rpx'
  31. },
  32. closeImg: {
  33. type: String,
  34. default: ''
  35. },
  36. closeImgTop: {
  37. type: String,
  38. default: ''
  39. }
  40. },
  41. data() {
  42. return {
  43. animationData: {}
  44. }
  45. },
  46. watch: {
  47. visible(val) {
  48. if (val) {
  49. this.playShowAnimation()
  50. } else {
  51. this.playHideAnimation()
  52. }
  53. }
  54. },
  55. methods: {
  56. playShowAnimation() {
  57. const animation = uni.createAnimation({
  58. duration: 300,
  59. timingFunction: 'ease'
  60. })
  61. animation.scale(0.8).opacity(0).step({ duration: 0 })
  62. animation.scale(1.1).opacity(1).step({ duration: 150 })
  63. animation.scale(0.95).step({ duration: 75 })
  64. animation.scale(1).step({ duration: 75 })
  65. this.animationData = animation.export()
  66. },
  67. playHideAnimation() {
  68. const animation = uni.createAnimation({
  69. duration: 200,
  70. timingFunction: 'ease'
  71. })
  72. animation.scale(0.95).opacity(0.8).step({ duration: 100 })
  73. animation.scale(0.8).opacity(0).step({ duration: 100 })
  74. this.animationData = animation.export()
  75. },
  76. closeDialog() {
  77. this.$emit('update:visible', false)
  78. this.$emit('close')
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. .custom-dialog {
  85. position: fixed;
  86. top: 0;
  87. left: 0;
  88. right: 0;
  89. bottom: 0;
  90. z-index: 900;
  91. display: flex;
  92. align-items: center;
  93. justify-content: center;
  94. .dialog-mask {
  95. position: absolute;
  96. top: 0;
  97. left: 0;
  98. right: 0;
  99. bottom: 0;
  100. background: rgba(0, 0, 0, 0.6);
  101. }
  102. .dialog-content {
  103. position: relative;
  104. background: #fff;
  105. border-radius: 16rpx;
  106. overflow: hidden;
  107. opacity: 0;
  108. transform: scale(0.8);
  109. &.dialog-show {
  110. opacity: 1;
  111. transform: scale(1);
  112. }
  113. }
  114. .dialog-header {
  115. position: relative;
  116. padding: 30rpx;
  117. text-align: center;
  118. border-bottom: 1rpx solid #eee;
  119. .dialog-title {
  120. font-size: 32rpx;
  121. font-weight: 500;
  122. color: #333;
  123. }
  124. }
  125. .dialog-close {
  126. position: absolute;
  127. right: 20rpx;
  128. top: 20rpx;
  129. width: 60rpx;
  130. height: 60rpx;
  131. line-height: 60rpx;
  132. text-align: center;
  133. font-size: 40rpx;
  134. color: #999;
  135. cursor: pointer;
  136. z-index: 1;
  137. &:active {
  138. opacity: 0.7;
  139. }
  140. }
  141. .dialog-close-img {
  142. position: absolute;
  143. right: 20rpx;
  144. width: 60rpx;
  145. height: 60rpx;
  146. z-index: 1;
  147. cursor: pointer;
  148. &:active {
  149. opacity: 0.7;
  150. }
  151. }
  152. .dialog-body {
  153. padding: 0rpx;
  154. }
  155. }
  156. </style>