uni-transition.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <!-- 底部弹窗需要组件(分享) -->
  2. <template>
  3. <view v-if="isShow" ref="ani" class="uni-transition" :class="[ani.in]" :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: { in: '',
  57. active: ''
  58. }
  59. };
  60. },
  61. watch: {
  62. show: {
  63. handler(newVal) {
  64. if (newVal) {
  65. this.open()
  66. } else {
  67. this.close()
  68. }
  69. },
  70. immediate: true
  71. }
  72. },
  73. computed: {
  74. stylesObject() {
  75. let styles = {
  76. ...this.styles,
  77. 'transition-duration': this.duration / 1000 + 's'
  78. }
  79. let transfrom = ''
  80. for (let i in styles) {
  81. let line = this.toLine(i)
  82. transfrom += line + ':' + styles[i] + ';'
  83. }
  84. return transfrom
  85. }
  86. },
  87. created() {
  88. // this.timer = null
  89. // this.nextTick = (time = 50) => new Promise(resolve => {
  90. // clearTimeout(this.timer)
  91. // this.timer = setTimeout(resolve, time)
  92. // return this.timer
  93. // });
  94. },
  95. methods: {
  96. change() {
  97. this.$emit('click', {
  98. detail: this.isShow
  99. })
  100. },
  101. open() {
  102. clearTimeout(this.timer)
  103. this.isShow = true
  104. this.transform = ''
  105. this.ani.in = ''
  106. for (let i in this.getTranfrom(false)) {
  107. if (i === 'opacity') {
  108. this.ani.in = 'fade-in'
  109. } else {
  110. this.transform += `${this.getTranfrom(false)[i]} `
  111. }
  112. }
  113. this.$nextTick(() => {
  114. setTimeout(() => {
  115. this._animation(true)
  116. }, 50)
  117. })
  118. },
  119. close(type) {
  120. clearTimeout(this.timer)
  121. this._animation(false)
  122. },
  123. _animation(type) {
  124. let styles = this.getTranfrom(type)
  125. // #ifdef APP-NVUE
  126. if(!this.$refs['ani']) return
  127. animation.transition(this.$refs['ani'].ref, {
  128. styles,
  129. duration: this.duration, //ms
  130. timingFunction: 'ease',
  131. needLayout: false,
  132. delay: 0 //ms
  133. }, () => {
  134. if (!type) {
  135. this.isShow = false
  136. }
  137. this.$emit('change', {
  138. detail: this.isShow
  139. })
  140. })
  141. // #endif
  142. // #ifndef APP-NVUE
  143. this.transform = ''
  144. for (let i in styles) {
  145. if (i === 'opacity') {
  146. this.ani.in = `fade-${type?'out':'in'}`
  147. } else {
  148. this.transform += `${styles[i]} `
  149. }
  150. }
  151. this.timer = setTimeout(() => {
  152. if (!type) {
  153. this.isShow = false
  154. }
  155. this.$emit('change', {
  156. detail: this.isShow
  157. })
  158. }, this.duration)
  159. // #endif
  160. },
  161. getTranfrom(type) {
  162. let styles = {
  163. transform: ''
  164. }
  165. this.modeClass.forEach((mode) => {
  166. switch (mode) {
  167. case 'fade':
  168. styles.opacity = type ? 1 : 0
  169. break;
  170. case 'slide-top':
  171. styles.transform += `translateY(${type?'0':'-100%'}) `
  172. break;
  173. case 'slide-right':
  174. styles.transform += `translateX(${type?'0':'100%'}) `
  175. break;
  176. case 'slide-bottom':
  177. styles.transform += `translateY(${type?'0':'100%'}) `
  178. break;
  179. case 'slide-left':
  180. styles.transform += `translateX(${type?'0':'-100%'}) `
  181. break;
  182. case 'zoom-in':
  183. styles.transform += `scale(${type?1:0.8}) `
  184. break;
  185. case 'zoom-out':
  186. styles.transform += `scale(${type?1:1.2}) `
  187. break;
  188. }
  189. })
  190. return styles
  191. },
  192. _modeClassArr(type) {
  193. let mode = this.modeClass
  194. if (typeof(mode) !== "string") {
  195. let modestr = ''
  196. mode.forEach((item) => {
  197. modestr += (item + '-' + type + ',')
  198. })
  199. return modestr.substr(0, modestr.length - 1)
  200. } else {
  201. return mode + '-' + type
  202. }
  203. },
  204. // getEl(el) {
  205. // console.log(el || el.ref || null);
  206. // return el || el.ref || null
  207. // },
  208. toLine(name) {
  209. return name.replace(/([A-Z])/g, "-$1").toLowerCase();
  210. }
  211. }
  212. }
  213. </script>
  214. <style>
  215. .uni-transition {
  216. transition-timing-function: ease;
  217. transition-duration: 0.3s;
  218. transition-property: transform, opacity;
  219. }
  220. .fade-in {
  221. opacity: 0;
  222. }
  223. .fade-active {
  224. opacity: 1;
  225. }
  226. .slide-top-in {
  227. /* transition-property: transform, opacity; */
  228. transform: translateY(-100%);
  229. }
  230. .slide-top-active {
  231. transform: translateY(0);
  232. /* opacity: 1; */
  233. }
  234. .slide-right-in {
  235. transform: translateX(100%);
  236. }
  237. .slide-right-active {
  238. transform: translateX(0);
  239. }
  240. .slide-bottom-in {
  241. transform: translateY(100%);
  242. }
  243. .slide-bottom-active {
  244. transform: translateY(0);
  245. }
  246. .slide-left-in {
  247. transform: translateX(-100%);
  248. }
  249. .slide-left-active {
  250. transform: translateX(0);
  251. opacity: 1;
  252. }
  253. .zoom-in-in {
  254. transform: scale(0.8);
  255. }
  256. .zoom-out-active {
  257. transform: scale(1);
  258. }
  259. .zoom-out-in {
  260. transform: scale(1.2);
  261. }
  262. </style>