tui-popup.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <view class="tui-popup__transition" :class="[ani.in]" :style="'transform:' + transform + ';' + stylesObject" @click="change" v-if="isShow" ref="ani"><slot></slot></view>
  3. </template>
  4. <script>
  5. // #ifdef APP-NVUE
  6. const animation = uni.requireNativePlugin('animation');
  7. // #endif
  8. export default {
  9. name: 'tuiPopup',
  10. emits: ['click','change'],
  11. props: {
  12. show: {
  13. type: Boolean,
  14. default: false
  15. },
  16. /*
  17. [fade|slide-top|slide-right|slide-bottom|slide-left|zoom-in|zoom-out]
  18. 过渡动画类型
  19. */
  20. modeClass: {
  21. type: Array,
  22. default() {
  23. return [];
  24. }
  25. },
  26. duration: {
  27. type: Number,
  28. default: 300
  29. },
  30. //styles 组件样式,同 css 样式
  31. styles: {
  32. type: Object,
  33. default() {
  34. return {
  35. position: 'fixed',
  36. bottom: 0,
  37. top: 0,
  38. left: 0,
  39. right: 0,
  40. /* #ifndef APP-NVUE */
  41. display: 'flex',
  42. /* #endif */
  43. 'justify-content': 'center',
  44. 'align-items': 'center'
  45. };
  46. }
  47. }
  48. },
  49. data() {
  50. return {
  51. isShow: false,
  52. transform: '',
  53. ani: {
  54. in: '',
  55. active: ''
  56. }
  57. };
  58. },
  59. watch: {
  60. show: {
  61. handler(newVal) {
  62. if (newVal) {
  63. this.open();
  64. } else {
  65. this.close();
  66. }
  67. },
  68. immediate: true
  69. }
  70. },
  71. computed: {
  72. stylesObject() {
  73. let styles = {
  74. ...this.styles,
  75. 'transition-duration': this.duration / 1000 + 's'
  76. };
  77. let transfrom = '';
  78. for (let i in styles) {
  79. let line = this.toLine(i);
  80. transfrom += line + ':' + styles[i] + ';';
  81. }
  82. return transfrom;
  83. }
  84. },
  85. methods: {
  86. change() {
  87. this.$emit('click', {
  88. detail: this.isShow
  89. });
  90. },
  91. open() {
  92. clearTimeout(this.timer);
  93. this.isShow = true;
  94. this.transform = '';
  95. this.ani.in = '';
  96. for (let i in this.getTranfrom(false)) {
  97. if (i === 'opacity') {
  98. this.ani.in = 'fade-in';
  99. } else {
  100. this.transform += `${this.getTranfrom(false)[i]} `;
  101. }
  102. }
  103. this.$nextTick(() => {
  104. setTimeout(() => {
  105. this._animation(true);
  106. }, 50);
  107. });
  108. },
  109. close(type) {
  110. clearTimeout(this.timer);
  111. this._animation(false);
  112. },
  113. _animation(type) {
  114. let styles = this.getTranfrom(type);
  115. // #ifdef APP-NVUE
  116. if (!this.$refs['ani']) return;
  117. animation.transition(
  118. this.$refs['ani'].ref,
  119. {
  120. styles,
  121. duration: this.duration, //ms
  122. timingFunction: 'ease',
  123. needLayout: false,
  124. delay: 0 //ms
  125. },
  126. () => {
  127. if (!type) {
  128. this.isShow = false;
  129. }
  130. this.$emit('change', {
  131. detail: this.isShow
  132. });
  133. }
  134. );
  135. // #endif
  136. // #ifndef APP-NVUE
  137. this.transform = '';
  138. for (let i in styles) {
  139. if (i === 'opacity') {
  140. this.ani.in = `fade-${type ? 'out' : 'in'}`;
  141. } else {
  142. this.transform += `${styles[i]} `;
  143. }
  144. }
  145. this.timer = setTimeout(() => {
  146. if (!type) {
  147. this.isShow = false;
  148. }
  149. this.$emit('change', {
  150. detail: this.isShow
  151. });
  152. }, this.duration);
  153. // #endif
  154. },
  155. getTranfrom(type) {
  156. let styles = {
  157. transform: ''
  158. };
  159. this.modeClass.forEach(mode => {
  160. switch (mode) {
  161. case 'fade':
  162. styles.opacity = type ? 1 : 0;
  163. break;
  164. case 'slide-top':
  165. styles.transform += `translateY(${type ? '0' : '-100%'}) `;
  166. break;
  167. case 'slide-right':
  168. styles.transform += `translateX(${type ? '0' : '100%'}) `;
  169. break;
  170. case 'slide-bottom':
  171. styles.transform += `translateY(${type ? '0' : '100%'}) `;
  172. break;
  173. case 'slide-left':
  174. styles.transform += `translateX(${type ? '0' : '-100%'}) `;
  175. break;
  176. case 'zoom-in':
  177. styles.transform += `scale(${type ? 1 : 0.8}) `;
  178. break;
  179. case 'zoom-out':
  180. styles.transform += `scale(${type ? 1 : 1.2}) `;
  181. break;
  182. }
  183. });
  184. return styles;
  185. },
  186. _modeClassArr(type) {
  187. let mode = this.modeClass;
  188. if (typeof mode !== 'string') {
  189. let modestr = '';
  190. mode.forEach(item => {
  191. modestr += item + '-' + type + ',';
  192. });
  193. return modestr.substr(0, modestr.length - 1);
  194. } else {
  195. return mode + '-' + type;
  196. }
  197. },
  198. toLine(name) {
  199. return name.replace(/([A-Z])/g, '-$1').toLowerCase();
  200. }
  201. }
  202. };
  203. </script>
  204. <style scoped>
  205. .tui-popup__transition {
  206. transition-timing-function: ease;
  207. transition-duration: 0.3s;
  208. transition-property: transform, opacity;
  209. position: relative;
  210. z-index: 99;
  211. }
  212. .fade-in {
  213. opacity: 0;
  214. }
  215. .fade-out {
  216. opacity: 1;
  217. }
  218. .slide-top-in {
  219. transform: translateY(-100%);
  220. }
  221. .slide-top-active {
  222. transform: translateY(0);
  223. }
  224. .slide-right-in {
  225. transform: translateX(100%);
  226. }
  227. .slide-right-active {
  228. transform: translateX(0);
  229. }
  230. .slide-bottom-in {
  231. transform: translateY(100%);
  232. }
  233. .slide-bottom-active {
  234. transform: translateY(0);
  235. }
  236. .slide-left-in {
  237. transform: translateX(-100%);
  238. }
  239. .slide-left-active {
  240. transform: translateX(0);
  241. opacity: 1;
  242. }
  243. .zoom-in-in {
  244. transform: scale(0.8);
  245. }
  246. .zoom-out-active {
  247. transform: scale(1);
  248. }
  249. .zoom-out-in {
  250. transform: scale(1.2);
  251. }
  252. </style>