wyb-transition.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <view :class="contentClass" id="content" v-if="isShow" :animation="animData" :style="root + contentStyle">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * wybTransition 过渡
  9. * @description 基于uni.createAnimation()进行封装的,简单过渡动画组件
  10. * @property {Boolean} isContentShow = [false|true] 控制组件显示或隐藏
  11. * @property {Array} typeList = ['fade', 'slide-up', 'rotate-aw'] 过渡动画类型
  12. * @value fade 渐隐渐现
  13. * @value slide-up 向上滑出
  14. * @value slide-right 向右滑出
  15. * @value slide-down 向下滑出
  16. * @value slide-left 向左滑出
  17. * @value slide-up-left 向左上方滑出
  18. * @value slide-up-right 向右上方滑出
  19. * @value slide-down-left 向左下方滑出
  20. * @value slide-down-right 向右下方滑出
  21. * @value zoom-largen 缩放(由小到大)
  22. * @value zoom-lesson 缩放(由大到小)
  23. * @value rotate-cw 顺时针旋转而出
  24. * @value rotate-aw 逆时针旋转而出
  25. * @value bounce 弹簧弹出
  26. * @property {Number} duration 过渡动画持续时间
  27. * @property {String} mode 动画演出模式
  28. * @value linear 动画从头到尾的速度是相同的
  29. * @value ease 动画以低速开始,然后加快,在结束前变慢
  30. * @value ease-in 动画以低速开始
  31. * @value ease-in-out 动画以低速开始和结束
  32. * @value ease-out 动画以低速结束
  33. * @value step-start 动画第一帧就跳至结束状态直到结束
  34. * @value step-end 动画一直保持开始状态,最后一帧跳到结束状态
  35. * @property {String} origin 动画演出中心 例如:'50% 50%',第一个是控制水平方向的,第二个是控制垂直方向的
  36. * @property {Number} multi 所有slide-*动画的平移倍数(对于自身宽高的倍数,默认一倍,即平移本身宽高的距离)
  37. * @property {Number} zoomLessenMulti 当type包含zoom-lessen(从大到小缩放)时的基础放大倍数,默认1.5倍
  38. * @property {Number} delay 动画演出延迟,默认为0,单位ms
  39. * @property {String} contentClass 可放入css样式类,因为组件本身就是一个view容器
  40. * @property {String} contentStyle 可放入css样式,因为组件本身就是一个view容器
  41. * @event {Function} onComeIn 入场动画开始事件
  42. * @event {Function} onGoOut 退场动画开始事件
  43. * @event {Function} finishComeIn 入场动画完成事件
  44. * @event {Function} finishGoOut 退场动画完成事件
  45. */
  46. var width = 0
  47. var height = 0
  48. export default {
  49. data() {
  50. return {
  51. animData: {}, // 动画对象
  52. isShow: false, // 组件内部的显隐开关,在动画结束时,改变值的时候,比isContentShow晚1ms(防止动画无法演出)
  53. root: '', // 组件的基础样式,给动画演出提供基础环境。例如在演出fade时,先将opacity设为0
  54. }
  55. },
  56. props: {
  57. isContentShow: {
  58. type: Boolean,
  59. default: false
  60. },
  61. duration: {
  62. type: Number,
  63. default: 400
  64. },
  65. typeList: {
  66. type: Array,
  67. default () {
  68. return ['fade']
  69. }
  70. },
  71. mode: {
  72. type: String,
  73. default: 'ease'
  74. },
  75. origin: {
  76. type: String,
  77. default: '50% 50%'
  78. },
  79. multi: {
  80. type: Number,
  81. default: 1
  82. },
  83. zoomLessenMulti: {
  84. type: Number,
  85. default: 1.5
  86. },
  87. delay: {
  88. type: Number,
  89. default: 0
  90. },
  91. contentClass: {
  92. type: String,
  93. default: ''
  94. },
  95. contentStyle: {
  96. type: String,
  97. default: ''
  98. }
  99. },
  100. watch: {
  101. // 监视isContentShow值的改变,演出相应动画
  102. isContentShow(val) {
  103. // 处理动画的函数
  104. this.processAll(val)
  105. }
  106. },
  107. created() {
  108. // 创建动画
  109. this.animation = uni.createAnimation({
  110. duration: this.duration,
  111. timingFunction: this.mode,
  112. transformOrigin: this.origin,
  113. delay: this.delay
  114. })
  115. },
  116. methods: {
  117. // 处理动画的函数
  118. processAll(val) {
  119. /**
  120. * 判断isContentShow的值
  121. * true:内部开关isShow设为true,然后注册入场动画开始事件,并延时1ms启动入场动画(防止动画无法演出),演出完成后,注册入场动画完成事件
  122. * false: 注册退场动画开始事件,并启动退场动画,然后延时(动画演出时间 + 10)ms的时间,再将内部开关isShow设为false,演出完成后,注册退场动画完成事件
  123. */
  124. if (val) {
  125. this.isShow = true
  126. this.root = this.processStyle(this.typeList)
  127. setTimeout(function() {
  128. this.$emit('onComeIn')
  129. this.processIn(this.typeList)
  130. this.animation.step()
  131. this.animData = this.animation.export()
  132. setTimeout(function() {
  133. this.$emit('finishComeIn')
  134. }.bind(this), this.duration + 1)
  135. }.bind(this), 10)
  136. } else {
  137. this.$emit('onGoOut')
  138. this.processOut(this.typeList)
  139. this.animation.step()
  140. this.animData = this.animation.export()
  141. setTimeout(function() {
  142. this.isShow = false
  143. this.$emit('finishGoOut')
  144. this.$forceUpdate()
  145. }.bind(this), this.duration + 1)
  146. }
  147. },
  148. /**
  149. * @param {Array} typeList 动画类型列表
  150. * 函数作用:将root处理为 "opacity: 0; transform: scale(1.5) translateY(-100%);" 的形式
  151. */
  152. processStyle(typeList) {
  153. let root = ''
  154. let transform = 'transform:'
  155. const hundred = 100
  156. if (typeList.includes('fade'))
  157. root += 'opacity: 0;'
  158. if (typeList.includes('zoom-largen') || typeList.includes('bounce')) {
  159. if (root.indexOf(transform) === -1) {
  160. root += transform + ' scale(0)'
  161. } else {
  162. root += ' scale(0)'
  163. }
  164. } else if (typeList.includes('zoom-lessen')) {
  165. if (root.indexOf(transform) === -1) {
  166. root += transform + ' scale(' + this.zoomLessenMulti + ')'
  167. } else {
  168. root += ' scale(' + this.zoomLessenMulti + ')'
  169. }
  170. }
  171. if (typeList.includes('slide-down')) {
  172. if (root.indexOf(transform) === -1) {
  173. root += transform + ' translateY(-' + hundred * this.multi + '%)'
  174. } else {
  175. root += ' translateY(-' + hundred * this.multi + '%)'
  176. }
  177. } else if (typeList.includes('slide-up')) {
  178. if (root.indexOf(transform) === -1) {
  179. root += transform + ' translateY(' + hundred * this.multi + '%)'
  180. } else {
  181. root += ' translateY(' + hundred * this.multi + '%)'
  182. }
  183. } else if (typeList.includes('slide-left')) {
  184. if (root.indexOf(transform) === -1) {
  185. root += transform + ' translateX(-' + hundred * this.multi + '%)'
  186. } else {
  187. root += ' translateX(-' + hundred * this.multi + '%)'
  188. }
  189. } else if (typeList.includes('slide-right')) {
  190. if (root.indexOf(transform) === -1) {
  191. root += transform + ' translateX(' + hundred * this.multi + '%)'
  192. } else {
  193. root += ' translateX(' + hundred * this.multi + '%)'
  194. }
  195. } else if (typeList.includes('slide-up-left')) {
  196. if (root.indexOf(transform) === -1) {
  197. root += transform + ' translate(' + hundred * this.multi + '%, ' + hundred * this.multi + '%)'
  198. } else {
  199. root += ' translate(' + hundred * this.multi + '%, ' + hundred * this.multi + '%)'
  200. }
  201. } else if (typeList.includes('slide-up-right')) {
  202. if (root.indexOf(transform) === -1) {
  203. root += transform + ' translate(-' + hundred * this.multi + '%, ' + hundred * this.multi + '%)'
  204. } else {
  205. root += ' translate(-' + hundred * this.multi + '%, ' + hundred * this.multi + '%)'
  206. }
  207. } else if (typeList.includes('slide-down-left')) {
  208. if (root.indexOf(transform) === -1) {
  209. root += transform + ' translate(' + hundred * this.multi + '%, -' + hundred * this.multi + '%)'
  210. } else {
  211. root += ' translate(' + hundred * this.multi + '%, -' + hundred * this.multi + '%)'
  212. }
  213. } else if (typeList.includes('slide-down-right')) {
  214. if (root.indexOf(transform) === -1) {
  215. root += transform + ' translate(-' + hundred * this.multi + '%, -' + hundred * this.multi + '%)'
  216. } else {
  217. root += ' translate(-' + hundred * this.multi + '%, -' + hundred * this.multi + '%)'
  218. }
  219. }
  220. if (typeList.includes('rotate-cw')) {
  221. if (root.indexOf(transform) === -1) {
  222. root += transform + ' rotate(-180deg)'
  223. } else {
  224. root += ' rotate(-180deg)'
  225. }
  226. } else if (typeList.includes('rotate-aw')) {
  227. if (root.indexOf(transform) === -1) {
  228. root += transform + ' rotate(180deg)'
  229. } else {
  230. root += ' rotate(180deg)'
  231. }
  232. }
  233. let rootList = root.split('')
  234. if (rootList[root.length - 1] !== ';') {
  235. root += ';'
  236. }
  237. return root
  238. },
  239. /**
  240. * @param {Array} typeList 动画类型列表
  241. * 函数作用:设置退场动画
  242. */
  243. processOut(typeList) {
  244. if (typeList.includes('fade'))
  245. this.animation.opacity(0)
  246. if (typeList.includes('zoom-largen'))
  247. this.animation.scale(0)
  248. else if (typeList.includes('zoom-lessen'))
  249. this.animation.scale(this.zoomLessenMulti)
  250. else if (typeList.includes('bounce')) {
  251. this.animation.scale(1.2).step({
  252. duration: this.duration * 1 / 3
  253. })
  254. this.animation.scale(0).step({
  255. duration: this.duration * 2 / 3
  256. })
  257. }
  258. if (typeList.includes('slide-down'))
  259. this.animation.translateY(0 - height)
  260. else if (typeList.includes('slide-up'))
  261. this.animation.translateY(height)
  262. else if (typeList.includes('slide-left'))
  263. this.animation.translateX(0 - width)
  264. else if (typeList.includes('slide-right'))
  265. this.animation.translateX(width)
  266. else if (typeList.includes('slide-up-left'))
  267. this.animation.translate(width, height)
  268. else if (typeList.includes('slide-up-right'))
  269. this.animation.translate(0 - width, height)
  270. else if (typeList.includes('slide-down-left'))
  271. this.animation.translate(width, 0 - height)
  272. else if (typeList.includes('slide-down-right'))
  273. this.animation.translate(0 - width, 0 - height)
  274. if (typeList.includes('rotate-cw'))
  275. this.animation.rotate(-180)
  276. else if (typeList.includes('rotate-aw'))
  277. this.animation.rotate(180)
  278. },
  279. /**
  280. * @param {Array} typeList 动画类型列表
  281. * 函数作用:设置入场动画
  282. */
  283. processIn(typeList) {
  284. // 查询放进组件slot节点元素的宽高,用于后面的平移距离
  285. this.getRect('#content').then(res => {
  286. width = parseFloat(res.width) * this.multi,
  287. height = parseFloat(res.height) * this.multi
  288. })
  289. if (typeList.includes('fade'))
  290. this.animation.opacity(1)
  291. if (typeList.includes('zoom-largen') || typeList.includes('zoom-lessen'))
  292. this.animation.scale(1)
  293. else if (typeList.includes('bounce')) {
  294. this.animation.scale(1.2).step({
  295. duration: this.duration * 1 / 3
  296. })
  297. this.animation.scale(1).step({
  298. duration: this.duration * 2 / 3
  299. })
  300. }
  301. if (typeList.includes('slide-up') || typeList.includes('slide-down')) {
  302. this.animation.translateY(0)
  303. } else if (typeList.includes('slide-left') || typeList.includes('slide-right')) {
  304. this.animation.translateX(0)
  305. } else if (typeList.includes('slide-up-left') || typeList.includes('slide-up-right') ||
  306. typeList.includes('slide-down-left') || typeList.includes('slide-down-right'))
  307. this.animation.translate(0, 0)
  308. if (typeList.includes('rotate-cw') || typeList.includes('rotate-aw'))
  309. this.animation.rotate(0)
  310. },
  311. /**
  312. * @param {Object} selector 选择器
  313. * 函数作用:查询元素的宽高、位置等信息
  314. */
  315. getRect(selector) {
  316. return new Promise(resolve => {
  317. uni.createSelectorQuery().in(this)['select'](selector).boundingClientRect(rect => {
  318. if (rect) {
  319. resolve(rect)
  320. }
  321. }).exec()
  322. })
  323. }
  324. }
  325. }
  326. </script>