1 回答
TA貢獻1875條經驗 獲得超3個贊
您可以使用 Javascript 向ball元素添加一個類(例如click),然后設置一個新動畫,以便在設置該類后運行。它基本上將您的原始動畫一分為二。
// Get the ball element
let ball = document.getElementsByClassName("ball");
// First instance of the ball object, add a click listener.
ball[0].addEventListener('click', (event) => {
// add the click class
ball[0].classList.add('click');
});
*,
*::after,
*::before {
box-sizing: inherit;
}
html {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
overflow: hidden;
}
.ball {
background-color: #eb8c28;
width: 100px;
height: 100px;
border-radius: 0;
position: absolute;
bottom: 0%;
/* Added calc here to center the ball */
left: calc(50% - 50px);
animation: rise;
animation-duration: 2s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
}
.ball.click {
animation: fill;
animation-duration: 2s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
}
@keyframes rise {
0% {
border-radius: 50%;
}
100% {
border-radius: 50%;
transform: translateY(-100%) scale(1);
}
}
@keyframes fill {
0% {
border-radius: 50%;
transform: translateY(-100%) scale(1);
}
100% {
border-radius: 0;
transform: translateY(-100%) scale(20);
}
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="ballcopy.css">
<meta name="veiwport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<main>
<div class="ball"></div>
</main>
</body>
</html>
添加回答
舉報
