uni-transition.vue 5.7 KB

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