Javascript动画间歇性激发
我有一个数学游戏,里面有一个计算正确答案的函数。如果用户回答正确,我想显示动画图像。我能够做到这一点,但动画它自己的作品基本上一次是和一次不是 我错过什么了吗Javascript动画间歇性激发,javascript,Javascript,我有一个数学游戏,里面有一个计算正确答案的函数。如果用户回答正确,我想显示动画图像。我能够做到这一点,但动画它自己的作品基本上一次是和一次不是 我错过什么了吗 function checkAnswer() { // Checks the answer against the first element in // the returned calculateCorrectAnswer array let userAnswer = parseInt(document.getEle
function checkAnswer() {
// Checks the answer against the first element in
// the returned calculateCorrectAnswer array
let userAnswer = parseInt(document.getElementById('answer-box').value);
let calculatedAnswer = calculateCorrectAnswer();
let isCorrect = userAnswer === calculatedAnswer[0];
if(isCorrect) {
incrementScore();
moveRight();
} else {
alert(`Awwww .... you answered ${userAnswer}. the correct answer was ${calculatedAnswer[0]} In your case I'd use transition
, instead of animation
The code would look like that:
#myImg {
opacity: 0;
transition: opacity 2s linear;
}
#myImg.fade-in {
opacity: 1;
}
函数checkAnswer(){
//对照中的第一个元素检查答案
//返回的calculateCorrectAnswer数组
让userAnswer=parseInt(document.getElementById('answer-box').value);
让calculatedAnswer=calculateCorrectAnswer();
让isCorrect=userAnswer==calculatedAnswer[0];
如果(不正确){
递增分数();
moveRight();
}否则{
警报(`Awwww….您回答了${userAnswer}。正确答案是${calculatedAnswer[0]},在您的情况下,我将使用转换
,而不是动画
代码如下所示:
#myImg{
不透明度:0;
过渡:线性;
}
#淡入{
不透明度:1;
}
在您的情况下,我会使用转换
,而不是动画
代码如下所示:
#myImg{
不透明度:0;
过渡:线性;
}
#淡入{
不透明度:1;
}
找到解决方案,基本上只需删除类并再次添加:)
找到解决方案,基本上只需删除类并再次添加:)
搜索“重置css关键帧”搜索“重置css关键帧”更好,但现在图像淡入并保留,然后再次提交时继续。使用以下函数找到解决方案:函数moveRight(){let theImg=document.getElementById('myImg');let msg=document.getElementById('message');theImg.classList.remove('fade-in'));void theImg.offsetWidth;theImg.classList.toggle('fade-in');msg.classList.remove('fade-in');void msg.offsetWidth;msg.classList.toggle('fade-in');}更好,但现在图像淡入并保留,然后在再次提交时继续。使用以下函数找到解决方案:moveRight(){let theImg=document.getElementById('myImg');let msg=document.getElementById('message');theImg.classList.remove('fade-in');void theImg.offsetWidth;theImg.classList.toggle('fade-in');msg.classList.remove('fade-in');void msg.offsetWidth;msg.classList.toggle('fade-in');}
function moveRight() {
let theImg = document.getElementById('myImg');
let msg = document.getElementById('message');
theImg.classList.remove('fade-in');
void theImg.offsetWidth;
theImg.classList.toggle('fade-in');
msg.classList.remove('fade-in');
void msg.offsetWidth;
msg.classList.toggle('fade-in');
}