<template>
<div class="container" @mouseenter="startAnimation" @mouseleave="stopAnimation">
<!-- 旋方块 -->
<div class="box" :class="{ 'rotate-scale-up': isAnimating }">
<!-- 元素内容 -->
</div>
</div>
</template>
<script setup>
import {
onMounted,
ref,
watch,
onUnmounted
} from 'vue';
const isAnimating = ref(false); // 控制是否应用动画的响应式状态
function startAnimation() {
// 鼠标进入容器时,启动动画
isAnimating.value = true;
}
function stopAnimation() {
// 鼠标离开容器时,停止动画
isAnimating.value = false;
}
onMounted(() => {
setInterval(() => {
if(isAnimating.value == false){
isAnimating.value = true
}else{
isAnimating.value = false
}
}, 500)
})
// onUnmounted(() => {
// clearInterval(timer)
// });
</script>
<style>
.container {
/* 定义容器宽度和高度 */
width: 100px;
height: 100px;
margin-top: 250px;
margin-left: 40%;
}
.box {
/* 定义方块宽度和高度 */
width: 100px;
height: 100px;
background-color: blue;
}
.rotate-scale-up {
-webkit-animation: rotate-scale-up 0.65s linear both;
animation: rotate-scale-up 0.65s linear both;
}
@-webkit-keyframes rotate-scale-up {
0% {
-webkit-transform: scale(1) rotateZ(0);
transform: scale(1) rotateZ(0);
}
50% {
-webkit-transform: scale(2) rotateZ(180deg);
transform: scale(2) rotateZ(180deg);
}
100% {
-webkit-transform: scale(1) rotateZ(360deg);
transform: scale(1) rotateZ(360deg);
}
}
@keyframes rotate-scale-up {
0% {
-webkit-transform: scale(1) rotateZ(0);
transform: scale(1) rotateZ(0);
}
50% {
-webkit-transform: scale(2) rotateZ(180deg);
transform: scale(2) rotateZ(180deg);
}
100% {
-webkit-transform: scale(1) rotateZ(360deg);
transform: scale(1) rotateZ(360deg);
}
}
</style>