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>

教学视频地址

点击跳转教学视频

相关推荐
Lhuu(重开版1 天前
CSS:动效布局动画
前端·css
Q***K551 天前
前端构建工具
前端
laocooon5238578861 天前
创建了一个带悬停效果的“我的个人主页“按钮
前端
2013编程爱好者1 天前
Vue工程结构分析
前端·javascript·vue.js·typescript·前端框架
小满zs1 天前
Next.js第十一章(渲染基础概念)
前端
不羁的fang少年1 天前
前端常见问题(vue,css,html,js等)
前端·javascript·css
change_fate1 天前
el-menu折叠后文字下移
前端·javascript·vue.js
yivifu1 天前
CSS Grid 布局详解(2025最新标准)
前端·css
o***Z4481 天前
前端性能优化案例
前端
张拭心1 天前
前端没有实际的必要了?结合今年工作内容,谈谈我的看法
前端·ai编程