tn-tips.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view
  3. v-if="visibleSync"
  4. class="tn-tips-class tn-tips"
  5. :class="[tipsClass]"
  6. :style="[tipsStyle]"
  7. >
  8. <view
  9. class="tn-tips__content"
  10. :class="[
  11. backgroundColorClass,
  12. fontColorClass
  13. ]"
  14. :style="{
  15. backgroundColor: backgroundColorStyle,
  16. color: fontColorStyle
  17. }"
  18. >{{ msg }}</view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'tn-tips',
  24. props: {
  25. // 层级
  26. zIndex: {
  27. type: Number,
  28. default: 0
  29. },
  30. // 提示框显示位置 top center bottom
  31. position: {
  32. type: String,
  33. default: 'top'
  34. },
  35. // 当位置设置为top的时候,设置距离顶部的距离
  36. top: {
  37. type: Number,
  38. default: 0
  39. }
  40. },
  41. computed: {
  42. tipsClass() {
  43. let clazz = ''
  44. switch (this.position) {
  45. case 'top':
  46. clazz += ' tn-tips--top'
  47. break
  48. case 'center':
  49. clazz += ' tn-tips--center'
  50. break
  51. case 'bottom':
  52. clazz += ' tn-tips--bottom'
  53. break
  54. default:
  55. clazz += ' tn-tips--top'
  56. }
  57. if (this.showTips) {
  58. clazz += ' tn-tips--show'
  59. }
  60. return clazz
  61. },
  62. tipsStyle() {
  63. let style = {}
  64. if ((this.position === 'top' || this.position === '') && this.top) {
  65. style.top = this.top + 'px'
  66. }
  67. style.zIndex = (this.zIndex ? this.zIndex : this.$t.zIndex.tips) + 1
  68. return style
  69. },
  70. backgroundColorStyle() {
  71. return this.$t.color.getBackgroundColorStyle(this.backgroundColor)
  72. },
  73. backgroundColorClass() {
  74. return this.$t.color.getBackgroundColorInternalClass(this.backgroundColor)
  75. },
  76. fontColorStyle() {
  77. return this.$t.color.getFontColorStyle(this.fontColor)
  78. },
  79. fontColorClass() {
  80. return this.$t.color.getFontColorInternalClass(this.fontColor)
  81. },
  82. },
  83. data() {
  84. return {
  85. //关闭提示框定时器
  86. timer: null,
  87. // 是否渲染组件
  88. visibleSync: false,
  89. // 是否显示内容
  90. showTips: false,
  91. // 提示信息
  92. msg: '',
  93. // 背景颜色
  94. backgroundColor: '',
  95. // 字体颜色
  96. fontColor: ''
  97. }
  98. },
  99. methods: {
  100. show(options = {}) {
  101. const {
  102. duration = 2000,
  103. msg = '',
  104. backgroundColor = '',
  105. fontColor = ''
  106. } = options
  107. if (this.timer !== null) clearTimeout(this.timer)
  108. // 如果没有设置内容则不弹出
  109. if (!msg) {
  110. this._clearOptions()
  111. this.$emit('close')
  112. return
  113. }
  114. this.msg = msg
  115. this.backgroundColor = backgroundColor || '#01BEFF'
  116. this.fontColor = fontColor || '#FFFFFF'
  117. this.change('visibleSync', 'showTips', true)
  118. this.timer = setTimeout(() => {
  119. clearTimeout(this.timer)
  120. this.timer = null
  121. this.change('showTips', 'visibleSync', false)
  122. }, duration)
  123. },
  124. // 关闭时先通过动画隐藏弹窗和遮罩,再移除整个组件
  125. // 打开时,先渲染组件,延时一定时间再让遮罩和弹窗的动画起作用
  126. change(param1, param2, status) {
  127. this[param1] = status
  128. if (status) {
  129. // #ifdef H5 || MP
  130. this.timer = setTimeout(() => {
  131. this[param2] = status
  132. this.$emit(status ? 'open' : 'close')
  133. }, 50)
  134. // #endif
  135. // #ifndef H5 || MP
  136. this.$nextTick(() => {
  137. this[param2] = status
  138. this.$emit(status ? 'open' : 'close')
  139. })
  140. // #endif
  141. } else {
  142. this.timer = setTimeout(() => {
  143. this[param2] = status
  144. this.$emit(status ? 'open' : 'close')
  145. this._clearOptions()
  146. }, 300)
  147. }
  148. },
  149. // 清除传递的参数
  150. _clearOptions() {
  151. this.msg = ''
  152. this.backgroundColor = ''
  153. this.fontColor = ''
  154. },
  155. }
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. /*注意问题:
  160. 1、fixed 元素宽度无法自适应,所以增加了子元素
  161. 2、fixed 和 display冲突导致动画效果消失,暂时使用visibility替代
  162. */
  163. .tn-tips {
  164. height: auto;
  165. position: fixed;
  166. box-sizing: border-box;
  167. display: flex;
  168. align-items: center;
  169. justify-content: center;
  170. transition: all 0.3s ease-in-out;
  171. opacity: 0;
  172. &__content {
  173. word-wrap: break-word;
  174. word-break: break-all;
  175. width: 100%;
  176. height: auto;
  177. text-align: center;
  178. background-color: rgba(0, 0, 0, 0.7);
  179. color: #FFFFFF;
  180. }
  181. &--top {
  182. width: 100% !important;
  183. /* padding: 18rpx 30rpx; */
  184. top: 0;
  185. left: 0;
  186. transform: translateY(-100%) translateZ(0);
  187. word-break: break-all;
  188. }
  189. &--center {
  190. left: 50%;
  191. top: 50%;
  192. transform: translate(-50%, -50%);
  193. }
  194. &--bottom {
  195. bottom: 120rpx;
  196. left: 50%;
  197. transform: translateX(-50%);
  198. }
  199. &--center, &--bottom {
  200. .content {
  201. border-radius: 8rpx;
  202. padding: 0;
  203. }
  204. }
  205. &--center, &--bottom {
  206. .tn-tips__content {
  207. padding: 18rpx 30rpx !important;
  208. }
  209. }
  210. &--show {
  211. opacity: 1;
  212. &.tn-tips--top {
  213. transform: translateY(0) translateZ(0) !important;
  214. }
  215. }
  216. }
  217. </style>