tm-translate.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <view v-show="show" :animation="animationData" class="tm-translate" >
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * 动画
  9. * @property {Function} start 动画开始触发.
  10. * @property {Function} end 动画结束触发.
  11. * @property {Number} duration 动画时长
  12. * @property {Number} wait 延时
  13. * @property {String} animation-name = [fadeUp|fadeDown|fadeLeft|fadeRight|zoomIn|zoomOut|fadeIn|fadeOut] 动画效果名称.
  14. * @property {String|Boolean} auto = [true|false] 默认:true,是否自动播放动画.
  15. * @example <tm-translate wait="500" animation-name="fadeDown"><tm-upload></tm-upload></tm-translate>
  16. */
  17. export default {
  18. name:"tm-translate",
  19. props:{
  20. duration:{
  21. type:Number|String,
  22. default:600
  23. },//动画时长.
  24. wait:{
  25. type:Number|String,
  26. default:0
  27. },//延时动画.
  28. animationName:{
  29. type:String,
  30. default:"fadeUp"
  31. },
  32. auto:{//是否自动播放动画.
  33. type:Boolean,
  34. default:true
  35. }
  36. },
  37. data() {
  38. return {
  39. animationData:"",
  40. show:false,
  41. aniOff:true,//是否结束了动画.
  42. };
  43. },
  44. watch:{
  45. animationName:function(){
  46. this.play();
  47. }
  48. },
  49. mounted() {
  50. this.autoPlay();
  51. },
  52. destroyed() {
  53. this.$nextTick(function(){
  54. this[this.animationName]()
  55. })
  56. },
  57. methods: {
  58. play(){
  59. this.$nextTick(function(){
  60. this[this.animationName]()
  61. })
  62. },
  63. autoPlay(){
  64. if(!this.auto) return;
  65. this[this.animationName]()
  66. },
  67. fadeIn() {
  68. let t = this;
  69. if(!this.aniOff) {
  70. console.warn("未结束动画,不能连续点击.")
  71. return;
  72. }
  73. // 开始动画.
  74. this.$emit('start')
  75. this.show = true;
  76. this.$nextTick(function(){
  77. this.animationData = "";
  78. this.aniOff = false;
  79. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  80. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  81. var animation = uni.createAnimation({
  82. duration: 0,
  83. timingFunction: 'linear',
  84. })
  85. this.animation = animation
  86. animation.opacity(0).step()
  87. this.animationData = animation.export()
  88. uni.$tm.sleep(wait).then(()=>{
  89. animation.opacity(1).step({duration:duration})
  90. t.animationData = animation.export()
  91. }).then(()=>{
  92. return uni.$tm.sleep(duration);
  93. }).then(()=>{
  94. // 结束动画.
  95. t.$emit('end')
  96. t.aniOff = true;
  97. })
  98. })
  99. },
  100. fadeOut() {
  101. let t = this;
  102. if(!this.aniOff) {
  103. console.warn("未结束动画,不能连续点击.")
  104. return;
  105. }
  106. // 开始动画.
  107. this.$emit('start')
  108. this.show = true;
  109. this.$nextTick(function(){
  110. this.animationData = "";
  111. this.aniOff = false;
  112. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  113. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  114. var animation = uni.createAnimation({
  115. duration: 0,
  116. timingFunction: 'ease',
  117. })
  118. this.animation = animation
  119. animation.opacity(1).step()
  120. this.animationData = animation.export()
  121. uni.$tm.sleep(wait).then(()=>{
  122. animation.opacity(0).step({duration:duration})
  123. t.animationData = animation.export()
  124. }).then(()=>{
  125. return uni.$tm.sleep(duration);;
  126. }).then(()=>{
  127. // 结束动画.
  128. t.$emit('end')
  129. t.aniOff = true;
  130. })
  131. })
  132. },
  133. fadeUp() {
  134. let t = this;
  135. if(!this.aniOff) {
  136. console.warn("未结束动画,不能连续点击.")
  137. return;
  138. }
  139. // 开始动画.
  140. this.$emit('start')
  141. this.show = true;
  142. this.animationData = "";
  143. this.aniOff = false;
  144. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  145. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  146. var animation = uni.createAnimation({
  147. duration: 0,
  148. timingFunction: 'ease',
  149. })
  150. this.animation = animation
  151. animation.translateY(45).opacity(0).step()
  152. this.animationData = animation.export()
  153. uni.$tm.sleep(wait).then(function(){
  154. animation.translateY(0).opacity(1).step({duration:duration})
  155. t.animationData = animation.export()
  156. uni.$tm.sleep(duration).then(()=>{
  157. // 结束动画.
  158. t.$emit('end')
  159. t.aniOff = true;
  160. })
  161. })
  162. },
  163. fadeDown() {
  164. let t = this;
  165. if(!this.aniOff) {
  166. console.warn("未结束动画,不能连续点击.")
  167. return;
  168. }
  169. // 开始动画.
  170. this.$emit('start')
  171. this.show = true;
  172. this.$nextTick(function(){
  173. this.aniOff = false;
  174. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  175. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  176. var animation = uni.createAnimation({
  177. duration: 0,
  178. timingFunction: 'ease',
  179. })
  180. this.animation = animation
  181. animation.translateY(-45).opacity(0).step()
  182. this.animationData = animation.export()
  183. uni.$tm.sleep(wait).then(()=>{
  184. animation.translateY(0).opacity(1).step({duration:duration})
  185. t.animationData = animation.export()
  186. }).then(()=>{
  187. return uni.$tm.sleep(duration);
  188. }).then(()=>{
  189. // 结束动画.
  190. t.$emit('end')
  191. t.aniOff = true;
  192. })
  193. })
  194. },
  195. fadeLeft() {
  196. let t= this;
  197. if(!this.aniOff) {
  198. console.warn("未结束动画,不能连续点击.")
  199. return;
  200. }
  201. // 开始动画.
  202. this.$emit('start')
  203. this.show = true;
  204. this.$nextTick(function(){
  205. this.aniOff = false;
  206. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  207. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  208. var animation = uni.createAnimation({
  209. duration: 0,
  210. timingFunction: 'ease',
  211. })
  212. this.animation = animation
  213. animation.translateX(45).opacity(0).step()
  214. this.animationData = animation.export()
  215. uni.$tm.sleep(wait).then(()=>{
  216. animation.translateX(0).opacity(1).step({duration:duration})
  217. t.animationData = animation.export()
  218. }).then(()=>{
  219. return uni.$tm.sleep(duration);
  220. }).then(()=>{
  221. // 结束动画.
  222. t.$emit('end')
  223. t.aniOff = true;
  224. })
  225. })
  226. },
  227. fadeRight() {
  228. let t = this;
  229. if(!this.aniOff) {
  230. console.warn("未结束动画,不能连续点击.")
  231. return;
  232. }
  233. // 开始动画.
  234. this.$emit('start')
  235. this.show = true;
  236. this.$nextTick(function(){
  237. this.aniOff = false;
  238. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  239. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  240. var animation = uni.createAnimation({
  241. duration: 0,
  242. timingFunction: 'ease',
  243. })
  244. this.animation = animation
  245. animation.translateX(-45).opacity(0).step()
  246. this.animationData = animation.export()
  247. uni.$tm.sleep(wait).then(()=>{
  248. animation.translateX(0).opacity(1).step({duration:duration})
  249. t.animationData = animation.export()
  250. }).then(()=>{
  251. return uni.$tm.sleep(duration);
  252. }).then(()=>{
  253. // 结束动画.
  254. t.$emit('end')
  255. t.aniOff = true;
  256. })
  257. })
  258. },
  259. zoomIn() {
  260. let t= this;
  261. if(!this.aniOff) {
  262. console.warn("未结束动画,不能连续点击.")
  263. return;
  264. }
  265. // 开始动画.
  266. this.$emit('start')
  267. this.show = true;
  268. this.$nextTick(function(){
  269. this.aniOff = false;
  270. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  271. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  272. var animation = uni.createAnimation({
  273. duration: 0,
  274. timingFunction: 'ease',
  275. })
  276. this.animation = animation
  277. animation.scale(0.64).opacity(0).step()
  278. this.animationData = animation.export()
  279. uni.$tm.sleep(wait).then(()=>{
  280. animation.scale(1).opacity(1).step({duration:duration})
  281. this.animationData = animation.export()
  282. }).then(()=>{
  283. return uni.$tm.sleep(duration);
  284. }).then(()=>{
  285. // 结束动画.
  286. t.$emit('end')
  287. t.aniOff = true;
  288. })
  289. })
  290. },
  291. zoomOut() {
  292. let t = this;
  293. if(!this.aniOff) {
  294. console.warn("未结束动画,不能连续点击.")
  295. return;
  296. }
  297. // 开始动画.
  298. this.$emit('start')
  299. this.show = true;
  300. this.$nextTick(function(){
  301. this.aniOff = false;
  302. let duration = parseInt(this.duration)?parseInt(this.duration):500;
  303. let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
  304. var animation = uni.createAnimation({
  305. duration: 0,
  306. timingFunction: 'ease',
  307. })
  308. this.animation = animation
  309. animation.scale(1).opacity(1).step()
  310. this.animationData = animation.export()
  311. uni.$tm.sleep(wait).then(()=>{
  312. animation.scale(0.64).opacity(0).step({duration:duration})
  313. t.animationData = animation.export()
  314. }).then(()=>{
  315. return uni.$tm.sleep(duration);
  316. }).then(()=>{
  317. t.show = false;
  318. // 结束动画.
  319. t.$emit('end')
  320. t.aniOff = true;
  321. })
  322. })
  323. },
  324. },
  325. }
  326. </script>
  327. <style lang="scss" scoped>
  328. .tm-translate{
  329. // #ifdef H5
  330. opacity: 0;
  331. // #endif
  332. }
  333. </style>