feat: add shake effect

This commit is contained in:
sangge 2023-12-13 00:15:41 +08:00
parent 520e9febd0
commit a886b4d3f2
2 changed files with 30 additions and 1 deletions

View File

@ -5,8 +5,10 @@
} }
#desktop{ #desktop{
position: absolute; position: absolute;
/*background-image: url('../asset/pic/qinshihuang/desktop.png');*/
background-repeat: no-repeat; background-repeat: no-repeat;
}
#desktop img{
position: absolute;
z-index: 1; z-index: 1;
} }
#restaurant .customer img { #restaurant .customer img {
@ -157,3 +159,15 @@
#kung-pao-chicken img{ #kung-pao-chicken img{
display: none; display: none;
} }
/* 定义抖动动画 */
@keyframes shake {
0%, 100% { transform: translateX(0); }
10%, 30%, 50%, 70%, 90% { transform: translateX(-10px); }
20%, 40%, 60%, 80% { transform: translateX(10px); }
}
.shake-animation {
animation: shake 0.8s; /* 动画持续时间 */
animation-timing-function: ease-in-out;
}

View File

@ -74,6 +74,7 @@ function handleClick(dishName) {
} else { } else {
// 错误答案的处理逻辑 // 错误答案的处理逻辑
console.log("不幸,这不是正确答案。"); console.log("不幸,这不是正确答案。");
addShakeEffect()
failedTimes = failedTimes + 1; failedTimes = failedTimes + 1;
switch (failedTimes) { switch (failedTimes) {
case 1: case 1:
@ -171,3 +172,17 @@ document.addEventListener("DOMContentLoaded", function() {
document.getElementById("dialogueText").innerHTML = randomQuestion; document.getElementById("dialogueText").innerHTML = randomQuestion;
console.log(answers[QAlist[currentIndex]]); console.log(answers[QAlist[currentIndex]]);
}); });
// 为所有元素添加抖动效果
function addShakeEffect() {
const allElements = document.querySelectorAll('body *'); // 选择页面上的所有元素
allElements.forEach(el => {
el.classList.add('shake-animation');
});
// 设置定时器以在动画完成后移除抖动效果
setTimeout(() => {
allElements.forEach(el => {
el.classList.remove('shake-animation');
});
}, 800); // 与 CSS 动画时间相同
}