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>
相关推荐
web1478621072321 分钟前
C# .Net Web 路由相关配置
前端·c#·.net
m0_7482478022 分钟前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
飞的肖26 分钟前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
青灯文案133 分钟前
前端 HTTP 请求由 Nginx 反向代理和 API 网关到后端服务的流程
前端·nginx·http
m0_7482548838 分钟前
DataX3.0+DataX-Web部署分布式可视化ETL系统
前端·分布式·etl
ZJ_.1 小时前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
GIS开发特训营1 小时前
Vue零基础教程|从前端框架到GIS开发系列课程(七)响应式系统介绍
前端·vue.js·前端框架·gis开发·webgis·三维gis
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
学代码的小前端1 小时前
0基础学前端-----CSS DAY9
前端·css
joan_851 小时前
layui表格templet图片渲染--模板字符串和字符串拼接
前端·javascript·layui