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 小时前
Vue和React怎么选?全面比对
前端·vue.js·react.js
楼田莉子3 小时前
Qt开发学习——QtCreator深度介绍/程序运行/开发规范/对象树
开发语言·前端·c++·qt·学习
暮之沧蓝3 小时前
Vue总结
前端·javascript·vue.js
木易 士心4 小时前
Promise深度解析:前端异步编程的核心
前端·javascript
im_AMBER4 小时前
Web 开发 21
前端·学习
又是忙碌的一天4 小时前
前端学习day01
前端·学习·html
Joker Zxc4 小时前
【前端基础】20、CSS属性——transform、translate、transition
前端·css
excel4 小时前
深入解析 Vue 3 源码:computed 的底层实现原理
前端·javascript·vue.js
大前端helloworld4 小时前
前端梳理体系从常问问题去完善-框架篇(react生态)
前端
不会算法的小灰4 小时前
HTML简单入门—— 基础标签与路径解析
前端·算法·html