CSS 缩减顶部动画

复制代码
<template>
	<!-- @mouseenter="startAnimation" 表示在鼠标进入元素时触发 startAnimation 方法。
	@mouseleave="stopAnimation" 表示在鼠标离开元素时触发 stopAnimation 方法。 -->
	<!-- 容器元素 -->
	<div class="container" @mouseenter="startAnimation" @mouseleave="stopAnimation">
		<!-- 方块 -->
		<div class="box" :class="{ 'animate': isAnimating }">
			<!-- 元素内容 -->
		</div>
	</div>
</template>
<script setup>
	import {
		ref
	} from 'vue';


	const isAnimating = ref(false); // 控制是否应用动画的响应式状态
	function startAnimation() {
		// 鼠标进入容器时,启动动画
		isAnimating.value = true;
	}

	function stopAnimation() {
		// 鼠标离开容器时,停止动画
		isAnimating.value = false;
	}
</script>
<style>
	.container {
		/* 定义容器宽度和高度 */
		width: 100px;
		height: 100px;
		margin-top: 50px;
		margin-left: 40%;
	}

	.box {
		/* 定义方块宽度和高度 */
		width: 100px;
		height: 100px;
		background-color: blue;
		/* 定义过渡效果 */
		transition: transform 0.5s;
	}

	/* 应用动画类 */
	.box.animate {
	-webkit-animation: scale-down-top 0.4s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
		        animation: scale-down-top 0.4s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
	}

	/* 定义动画 */
	@-webkit-keyframes scale-down-top {
	  0% {
	    -webkit-transform: scale(1);
	            transform: scale(1);
	    -webkit-transform-origin: 50% 0%;
	            transform-origin: 50% 0%;
	  }
	  100% {
	    -webkit-transform: scale(0.5);
	            transform: scale(0.5);
	    -webkit-transform-origin: 50% 0%;
	            transform-origin: 50% 0%;
	  }
	}
	@keyframes scale-down-top {
	  0% {
	    -webkit-transform: scale(1);
	            transform: scale(1);
	    -webkit-transform-origin: 50% 0%;
	            transform-origin: 50% 0%;
	  }
	  100% {
	    -webkit-transform: scale(0.5);
	            transform: scale(0.5);
	    -webkit-transform-origin: 50% 0%;
	            transform-origin: 50% 0%;
	  }
	}

</style>

### 教学视频地址

[点击跳转教学视频](https://edu.csdn.net/course/detail/39132)

相关推荐
小鸡吃米…19 分钟前
Python - 类属性
java·前端·python
前端不太难29 分钟前
Navigation State 驱动的页面调试方法论
开发语言·前端·react.js
用户47949283569151 小时前
你每天都在用的 JSON.stringify ,V8 给它开了“加速通道”
前端·chrome·后端
JIngJaneIL1 小时前
基于java+ vue办公管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
静待雨落1 小时前
Electron无边框窗口如何拖拽以及最大化和还原窗口
前端·electron
沐泽__2 小时前
iframe内嵌页面双向通信
前端·javascript·chrome
小北方城市网2 小时前
第4 课:Vue 3 路由与状态管理实战 —— 从单页面到多页面应用
前端·javascript·vue.js
ohyeah2 小时前
用 Vue3 + Coze API 打造冰球运动员 AI 生成器:从图片上传到风格化输出
前端·vue.js·coze
Dragon Wu2 小时前
TailWindCss 核心功能总结
前端·css·前端框架·postcss
lcc1872 小时前
CSS 隐藏元素的方式
css