popup.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <div style="z-index:9999;overflow:hidden;" v-if="status">
  3. <!-- 蒙版 -->
  4. <view v-if="mask" class="position-fixed top-0 left-0 right-0 bottom-0 z-index" :style="getMaskColor"
  5. @click="hide"></view>
  6. <!-- 弹出框内容 -->
  7. <div ref="popup" class="position-fixed free-animated z-index" :class="getBodyClass" :style="getBodyStyle">
  8. <slot></slot>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. // #ifdef APP-PLUS-NVUE
  14. const animation = weex.requireModule('animation')
  15. // #endif
  16. export default {
  17. props: {
  18. // 是否开启蒙版颜色
  19. maskColor: {
  20. type: Boolean,
  21. default: true
  22. },
  23. // 是否开启蒙版
  24. mask: {
  25. type: Boolean,
  26. default: true
  27. },
  28. // 是否居中
  29. center: {
  30. type: Boolean,
  31. default: false
  32. },
  33. // 是否处于底部
  34. bottom: {
  35. type: Boolean,
  36. default: false
  37. },
  38. // 弹出层内容宽度
  39. bodyWidth: {
  40. type: Number,
  41. default: 0
  42. },
  43. // 弹出层内容高度
  44. bodyHeight: {
  45. type: Number,
  46. default: 0
  47. },
  48. // 弹出层内容 背景颜色
  49. bodyBgColor: {
  50. type: String,
  51. default: "bg-white"
  52. },
  53. // 弹出层 动画方向
  54. transformOrigin: {
  55. type: String,
  56. default: "left top"
  57. },
  58. // tabbar高度
  59. tabbarHeight: {
  60. type: Number,
  61. default: 0
  62. }
  63. },
  64. data() {
  65. return {
  66. status: false, //显示模态框 状态
  67. x: -1,
  68. y: -1,
  69. maxX: 0,
  70. maxY: 0
  71. }
  72. },
  73. mounted() {
  74. try {
  75. const res = uni.getSystemInfoSync();
  76. this.maxX = res.windowWidth - uni.upx2px(this.bodyWidth)
  77. this.maxY = res.windowHeight - uni.upx2px(this.bodyHeight) - uni.upx2px(this.tabbarHeight)
  78. } catch (e) {
  79. // error
  80. }
  81. },
  82. computed: {
  83. getMaskColor() {
  84. let i = this.maskColor ? 0.5 : 0
  85. return `background-color: rgba(0,0,0,${i});`
  86. },
  87. getBodyClass() {
  88. if (this.center) {
  89. return 'left-0 right-0 bottom-0 top-0 flex align-center justify-center'
  90. }
  91. let bottom = this.bottom ? 'left-0 right-0 bottom-0' : 'rounded border'
  92. return `${this.bodyBgColor} ${bottom}`
  93. },
  94. getBodyStyle() {
  95. let left = this.x > -1 ? `left:${this.x}px;` : ''
  96. let top = this.y > -1 ? `top:${this.y}px;` : ''
  97. return left + top
  98. }
  99. },
  100. methods: {
  101. show(x = -1, y = -1) {
  102. if (this.status) {
  103. return;
  104. }
  105. this.x = (x > this.maxX) ? this.maxX : x
  106. this.y = (y > this.maxY) ? this.maxY : y
  107. this.status = true
  108. // #ifdef APP-PLUS-NVUE
  109. this.$nextTick(() => {
  110. animation.transition(this.$refs.popup, {
  111. styles: {
  112. transform: 'scale(1,1)',
  113. transformOrigin: this.transformOrigin,
  114. opacity: 1
  115. },
  116. duration: 100, //ms
  117. timingFunction: 'ease',
  118. }, function() {
  119. console.log('动画执行结束');
  120. })
  121. })
  122. // #endif
  123. },
  124. hide() {
  125. this.$emit('hide')
  126. // #ifdef APP-PLUS-NVUE
  127. animation.transition(this.$refs.popup, {
  128. styles: {
  129. transform: 'scale(0,0)',
  130. transformOrigin: this.transformOrigin,
  131. opacity: 0
  132. },
  133. duration: 100, //ms
  134. timingFunction: 'ease',
  135. }, () => {
  136. this.status = false
  137. console.log('动画执行结束');
  138. })
  139. // #endif
  140. this.status = false
  141. },
  142. }
  143. }
  144. </script>
  145. <style scoped>
  146. .flex {
  147. display: flex;
  148. }
  149. .align-center {
  150. align-items: center;
  151. }
  152. .justify-center {
  153. justify-content: center;
  154. }
  155. .rounded {
  156. border-radius: 8rpx;
  157. }
  158. .border {
  159. border-width: 1rpx;
  160. border-style: solid;
  161. border-color: #dee2e6;
  162. }
  163. .top-0 {
  164. top: 0;
  165. }
  166. .left-0 {
  167. left: 0;
  168. }
  169. .right-0 {
  170. right: 0;
  171. }
  172. .bottom-0 {
  173. bottom: 0;
  174. }
  175. .bg-white {
  176. background-color: #FFFFFF;
  177. }
  178. .position-fixed {
  179. position: fixed;
  180. }
  181. .free-animated {
  182. /* #ifdef APP-PLUS-NVUE */
  183. transform: scale(0, 0);
  184. opacity: 0;
  185. /* #endif */
  186. }
  187. .z-index {
  188. /* #ifndef APP-NVUE */
  189. z-index: 9999;
  190. /* #endif */
  191. }
  192. </style>