<template>
<div class="balloon" :style="balloonStyle" @mouseover="bounceBalloon">
<div class="string"></div>
<div class="balloon-body"></div>
</div>
</template>
<script>
export default {
data() {
return {
balloonStyle: {
top: '70%',
left: '50%'
}
};
},
methods: {
bounceBalloon() {
const randomNumber = (min, max) => Math.random() * (max - min) + min;
const angle = randomNumber(-20, 20);
const distance = randomNumber(50, 100);
const x = Math.sin(angle) * distance;
const y = Math.cos(angle) * distance * -1;
this.balloonStyle = {
top: `calc(70% + ${y}px)`,
left: `calc(50% + ${x}px)`
};
}
}
};
</script>
<style>
.balloon {
position: absolute;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-end;
width: 60px;
height: 80px;
border-radius: 50%;
background-color: #ff6384;
animation: float 6s infinite ease-in-out alternate;
cursor: pointer;
}
.string {
width: 4px;
height: 70%;
background-color: #8f94fb;
margin-bottom: 10px;
}
.balloon-body {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #ff6384;
}
@keyframes float {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-10px);
}
100% {
transform: translateY(0);
}
}
</style>
鼠标悬停就会跑开哦 vue文件 复制直接就能跑
标签:const,color,50%,height,70%,angle,气球 From: https://blog.51cto.com/u_16128364/6343067