uni-transition.vue 4.9 KB

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