uni-transition.vue 8.3 KB

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