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;
	}

	/* 根据isAnimating的状态应用或移除旋转动画类 */
	.box.animate {
		-webkit-animation: scale-up-top 0.8s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
		animation: scale-up-top 0.8s cubic-bezier(0.390, 0.575, 0.565, 1.000) both;
	}

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

		100% {
			-webkit-transform: scale(1);
			transform: scale(1);
			-webkit-transform-origin: 50% 0%;
			transform-origin: 50% 0%;
		}
	}

	@keyframes scale-up-top {
		0% {
			-webkit-transform: scale(0.5);
			transform: scale(0.5);
			-webkit-transform-origin: 50% 0%;
			transform-origin: 50% 0%;
		}

		100% {
			-webkit-transform: scale(1);
			transform: scale(1);
			-webkit-transform-origin: 50% 0%;
			transform-origin: 50% 0%;
		}
	}
</style>

教学视频地址

点击跳转教学视频

相关推荐
风骏时光牛马1 小时前
Bash变量未加双引号导致文件名含空格解析异常实战案例
前端
Vennn1 小时前
Android自动化:使用 Web 方式实现某音未读消息检查与采集
前端·javascript·vue.js
Smilezyl1 小时前
为了搞懂 AI Agent,我用 6000 行 JS 代码手搓了一个零依赖的 Coding Agent
前端·javascript·github
海鸥-w1 小时前
前端学习python第三天笔记整理(list 列表,str字符串,tuple元组,set集合,dect,函数,类型注解)
前端·python·学习
flavor1 小时前
Vue3 大屏适配组件(Scale / Rem 双方案一键切换)
前端
用户059540174461 小时前
把 AI Agent 记忆验证从手工比对换成 Pytest + 向量数据库,测试效率提升 10 倍
前端·css
要写代码1 小时前
2026-Css忘掉一切、归零再启-alpha和opacity
前端
光影少年1 小时前
前端如何和蓝牙物联网进行通信和兼容性问题
前端·物联网·掘金·金石计划
星栈1 小时前
我把售后模块砍到只剩 64 行:Rust 全栈 CRM 的 MVP 取舍实录
前端·后端·开源
玉宇夕落1 小时前
懒加载与Suspense的学习
前端