Vue 实现的精彩动画效果

在 Vue 开发中,我们可以利用<transition>组件来打造各种令人惊艳的动画效果。下面来详细看看这些有趣的动画效果及其实现代码。

一、缩放类效果

  1. zoom-in(整体放大进入)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="zoom-in">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.zoom-in-enter-active {
  animation: zoomIn 0.5s ease;
}

@keyframes zoomIn {
  from {
    transform: scale(0);
  }

  to {
    transform: scale(1);
  }
}
</style>
  1. zoom-in-left(从左侧放大进入)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="zoom-in-left">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.zoom-in-left-enter-active {
  animation: zoomInLeft 0.5s ease;
}

@keyframes zoomInLeft {
  from {
    transform: scale(0) translateX(-100%);
  }

  to {
    transform: scale(1) translateX(0);
  }
}
</style>
  1. zoom-in-right(从右侧放大进入)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="zoom-in-right">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.zoom-in-right-enter-active {
  animation: zoomInRight 0.5s ease;
}

@keyframes zoomInRight {
  from {
    transform: scale(0) translateX(100%);
  }

  to {
    transform: scale(1) translateX(0);
  }
}
</style>
  1. zoom-in-top(从顶部放大进入)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="zoom-in-top">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.zoom-in-top-enter-active {
  animation: zoomInTop 0.5s ease;
}

@keyframes zoomInTop {
  from {
    transform: scale(0) translateY(-100%);
  }

  to {
    transform: scale(1) translateY(0);
  }
}
</style>
  1. zoom-in-bottom(从底部放大进入)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="zoom-in-bottom">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.zoom-in-bottom-enter-active {
  animation: zoomInBottom 0.5s ease;
}

@keyframes zoomInBottom {
  from {
    transform: scale(0) translateY(100%);
  }

  to {
    transform: scale(1) translateY(0);
  }
}
</style>
  1. zoom-in-center-x(沿水平中心轴放大进入)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="zoom-in-center-x">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.zoom-in-center-x-enter-active {
  animation: zoomInCenterX 0.5s ease;
}

@keyframes zoomInCenterX {
  from {
    transform: scaleX(0);
  }

  to {
    transform: scaleX(1);
  }
}
</style>
  1. zoom-in-center-y(沿垂直中心轴放大进入)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="zoom-in-center-y">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.zoom-in-center-y-enter-active {
  animation: zoomInCenterY 0.5s ease;
}

@keyframes zoomInCenterY {
  from {
    transform: scaleY(0);
  }

  to {
    transform: scaleY(1);
  }
}
</style>

二、滑动类效果

  1. slide(普通滑动)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="slide">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.slide-enter-active {
  animation: slideIn 0.5s ease;
}

@keyframes slideIn {
  from {
    transform: translateX(-100%);
  }

  to {
    transform: translateX(0);
  }
}
</style>
  1. slide-left(向左滑动)
html 复制代码
<template>
  <div>
    <button @click="isShow =! isShow">显示/隐藏</button>
    <transition name="slide-left">
      <h1 v-show="isShow">你好啊</h1>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      isShow: true,
    };
  },
};
</script>

<style scoped>
.slide-left-enter-active {
  animation: slideLeftIn 0.5s ease;
}

@keyframes slideLeftIn {
  from {
    transform: translateX(100%);
  }

  to {
    transform: translateX(0);
  }
}
</style>
  1. 向右滑动(slide-right)
html 复制代码
<template>
	 <div>
	   <button @click="isShow =! isShow">显示/隐藏</button>
	   <transition name="slide-right">
		 <h1 v-show="isShow">你好啊</h1>
	   </transition>
	 </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false
    };
  }
};
</script>

<style scoped>
.slide-right-enter-active,
.slide-right-leave-active {
  transition: all 0.5s ease;
}

.slide-right-enter,
.slide-right-leave-to {
  transform: translateX(-100%);
}
</style>
  1. 向上滑动(slide-top)
html 复制代码
<template>
	<div>
	  <button @click="isShow =! isShow">显示/隐藏</button>
	  <transition name="slide-top">
			 <h1 v-show="isShow">你好啊</h1>
	  </transition>
	</div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false
    };
  }
};
</script>

<style scoped>
		.slide-top-enter-active,
		.slide-top-leave-active {
		  transition: all 0.5s ease;
		}

		.slide-top-enter,
		.slide-top-leave-to {
		  transform: translateY(-100%);
		}
</style>
  1. 向下滑动(slide-bottom)
html 复制代码
<template>
  <div class="slide-bottom-animation" v-if="showBottom">向下滑动示例</div>
</template>

<script>
export default {
  data() {
    return {
      showBottom: false
    };
  }
};
</script>

<style scoped>
  		.slide-bottom-enter-active,
	  .slide-bottom-leave-active {
	    transition: all 0.5s ease;
	  }
	  
	  .slide-bottom-enter,
	  .slide-bottom-leave-to {
	    transform: translateY(100%);
	  }
	  
</style>

三、淡入淡出效果

html 复制代码
<template>
		 <div>
		   <button @click="isShow =! isShow">显示/隐藏</button>
		   <transition name="fade-animation">
		 		 <h1 v-show="isShow">你好啊</h1>
		   </transition>
		 </div> 
</template>

<script>
export default {
  data() {
    return {
      isShow: true
    };
  }
};
</script>

<style scoped>
.fade-animation-enter-active,
.fade-animation-leave-active {
  transition: opacity 0.5s ease;
}

.fade-animation-enter,
.fade-animation-leave-to {
  opacity: 0;
}
</style>
相关推荐
xixixin_几秒前
【vue】中字符串与数组转换:为何首选 Computed 而非 Methods?
前端·javascript·vue.js
Sylvia33.6 分钟前
网球/羽毛球数据API:专业赛事数据服务的技术实现
java·前端·websocket·json
i_am_a_div_日积月累_6 分钟前
el-drawer注册全局点击事件无效;el-dialog注册全局点击事件无效
javascript·vue.js·elementui
开开心心_Every19 分钟前
文件数量统计工具:支持多层文件夹数量统计
游戏·微信·pdf·excel·语音识别·swift·lisp
啥都不懂的小小白24 分钟前
Vue第四篇:组件通信 + DOM 更新 + 过渡动画
vue.js·全局事件通信
向下的大树27 分钟前
VUE父子组件传参中的触发时机问题:异步场景下的解决方案
前端·javascript·vue.js
英俊潇洒美少年28 分钟前
vue2中使用节流防抖函数时,使用的vue状态始终是初始化的数据
前端·javascript·vue.js
棒棒的唐42 分钟前
适合小程序使用的将对象数组转换为参数字符串方法
前端·javascript·小程序
刘一说1 小时前
Vue3响应式原理重构:从Object.defineProperty到Proxy的革命性升级
javascript·vue.js·重构
博客zhu虎康1 小时前
音频视频处理:前端直播流播放 flv
前端