123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <template>
- <view v-show="show" :animation="animationData" class="tm-translate" >
- <slot></slot>
- </view>
- </template>
- <script>
- /**
- * 动画
- * @property {Function} start 动画开始触发.
- * @property {Function} end 动画结束触发.
- * @property {Number} duration 动画时长
- * @property {Number} wait 延时
- * @property {String} animation-name = [fadeUp|fadeDown|fadeLeft|fadeRight|zoomIn|zoomOut|fadeIn|fadeOut] 动画效果名称.
- * @property {String|Boolean} auto = [true|false] 默认:true,是否自动播放动画.
- * @example <tm-translate wait="500" animation-name="fadeDown"><tm-upload></tm-upload></tm-translate>
- */
- export default {
- name:"tm-translate",
- props:{
- duration:{
- type:Number|String,
- default:600
- },//动画时长.
- wait:{
- type:Number|String,
- default:0
- },//延时动画.
- animationName:{
- type:String,
- default:"fadeUp"
- },
- auto:{//是否自动播放动画.
- type:Boolean,
- default:true
- }
- },
- data() {
- return {
- animationData:"",
- show:false,
- aniOff:true,//是否结束了动画.
- };
- },
- watch:{
- animationName:function(){
- this.play();
- }
- },
- mounted() {
- this.autoPlay();
- },
- destroyed() {
- this.$nextTick(function(){
- this[this.animationName]()
- })
- },
- methods: {
- play(){
- this.$nextTick(function(){
- this[this.animationName]()
- })
- },
- autoPlay(){
- if(!this.auto) return;
-
- this[this.animationName]()
- },
- fadeIn() {
- let t = this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
-
- this.$nextTick(function(){
- this.animationData = "";
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'linear',
- })
- this.animation = animation
- animation.opacity(0).step()
- this.animationData = animation.export()
-
- uni.$tm.sleep(wait).then(()=>{
- animation.opacity(1).step({duration:duration})
- t.animationData = animation.export()
- }).then(()=>{
- return uni.$tm.sleep(duration);
- }).then(()=>{
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
-
-
-
- })
- },
- fadeOut() {
- let t = this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
-
- this.$nextTick(function(){
- this.animationData = "";
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'ease',
- })
- this.animation = animation
- animation.opacity(1).step()
- this.animationData = animation.export()
-
-
- uni.$tm.sleep(wait).then(()=>{
- animation.opacity(0).step({duration:duration})
- t.animationData = animation.export()
- }).then(()=>{
- return uni.$tm.sleep(duration);;
- }).then(()=>{
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
-
-
-
- })
- },
- fadeUp() {
- let t = this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
-
- this.animationData = "";
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'ease',
- })
- this.animation = animation
- animation.translateY(45).opacity(0).step()
- this.animationData = animation.export()
-
- uni.$tm.sleep(wait).then(function(){
- animation.translateY(0).opacity(1).step({duration:duration})
- t.animationData = animation.export()
- uni.$tm.sleep(duration).then(()=>{
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
- })
- },
- fadeDown() {
- let t = this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
-
- this.$nextTick(function(){
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'ease',
- })
- this.animation = animation
- animation.translateY(-45).opacity(0).step()
- this.animationData = animation.export()
- uni.$tm.sleep(wait).then(()=>{
- animation.translateY(0).opacity(1).step({duration:duration})
- t.animationData = animation.export()
- }).then(()=>{
- return uni.$tm.sleep(duration);
- }).then(()=>{
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
-
- })
- },
- fadeLeft() {
- let t= this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
- this.$nextTick(function(){
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'ease',
- })
- this.animation = animation
- animation.translateX(45).opacity(0).step()
- this.animationData = animation.export()
- uni.$tm.sleep(wait).then(()=>{
- animation.translateX(0).opacity(1).step({duration:duration})
- t.animationData = animation.export()
- }).then(()=>{
- return uni.$tm.sleep(duration);
- }).then(()=>{
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
-
- })
- },
- fadeRight() {
- let t = this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
- this.$nextTick(function(){
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'ease',
- })
- this.animation = animation
- animation.translateX(-45).opacity(0).step()
- this.animationData = animation.export()
- uni.$tm.sleep(wait).then(()=>{
- animation.translateX(0).opacity(1).step({duration:duration})
- t.animationData = animation.export()
- }).then(()=>{
- return uni.$tm.sleep(duration);
- }).then(()=>{
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
- })
- },
- zoomIn() {
- let t= this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
- this.$nextTick(function(){
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'ease',
- })
- this.animation = animation
- animation.scale(0.64).opacity(0).step()
- this.animationData = animation.export()
- uni.$tm.sleep(wait).then(()=>{
- animation.scale(1).opacity(1).step({duration:duration})
- this.animationData = animation.export()
- }).then(()=>{
- return uni.$tm.sleep(duration);
- }).then(()=>{
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
-
- })
- },
- zoomOut() {
- let t = this;
- if(!this.aniOff) {
- console.warn("未结束动画,不能连续点击.")
- return;
- }
- // 开始动画.
- this.$emit('start')
- this.show = true;
- this.$nextTick(function(){
- this.aniOff = false;
- let duration = parseInt(this.duration)?parseInt(this.duration):500;
- let wait = parseInt(this.wait)? parseInt(this.wait) : 50;
- var animation = uni.createAnimation({
- duration: 0,
- timingFunction: 'ease',
- })
- this.animation = animation
- animation.scale(1).opacity(1).step()
- this.animationData = animation.export()
- uni.$tm.sleep(wait).then(()=>{
- animation.scale(0.64).opacity(0).step({duration:duration})
- t.animationData = animation.export()
- }).then(()=>{
- return uni.$tm.sleep(duration);
- }).then(()=>{
- t.show = false;
- // 结束动画.
- t.$emit('end')
- t.aniOff = true;
- })
-
- })
- },
- },
- }
- </script>
- <style lang="scss" scoped>
- .tm-translate{
- // #ifdef H5
- opacity: 0;
- // #endif
- }
- </style>
|