Sup*_*nzi 16 html javascript css animation
我目前正在尝试学习JavaScript并尝试很多东西,但就目前而言,我的JS skilly仍然非常有限.我创建了一个小游戏,其中有一个随机出现的兔子头盒子,用户必须尽快点击它们.
所以我用间隔动画创建了兔子,兔子关闭并在每2秒内睁开眼睛.现在我觉得有点愚蠢,但我无法让动画像我想要的那样工作.现在,兔子每2秒钟闭上眼睛,然后每隔2秒再打开一次.但是,我希望它只是眨眼,这意味着眼睛应该暂时关闭然后再打开,这样兔子每2秒闪烁一次.然后我还希望它有时会偶尔闪烁一次,有时会眨眼两次.不确定这是不是很容易,我只是盲目地编码和学习新东西,但似乎我可能需要你的帮助.
您可以看到小提琴内部使用的完整代码.我不想在代码部分发布整个网站,但我认为我的动画部分感兴趣.
这是眨眼睛的js代码:
var eyeleft = document.getElementById("eyeleft");
var eyeright = document.getElementById("eyeright");
window.onload = setInterval(function() {
eyeleft.className = (eyeleft.className != 'closedeyeleft' ? 'closedeyeleft' : 'eyeleft');
eyeright.className = (eyeright.className != 'closedeyeright' ? 'closedeyeright' : 'eyeright');
}, 2000);
Run Code Online (Sandbox Code Playgroud)
接下来是HTML
<div id="shape" class="game-shapes">
<div class="ears left"></div>
<div class="ears right"></div>
<div id="eyeleft" class="eyeleft"></div>
<div id="eyeright" class="eyeright"></div>
<div id="mouth">
<div id="tooth"></div>
<div id="tongue"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
现在CSS
.game-shapes {
height: 80px;
width: 80px;
cursor: pointer;
opacity: 0;
position: relative;
transition: opacity ease-in-out .2s;
}
.game-shapes div {
position: absolute;
}
.eyeleft,
.eyeright {
background: #000;
border-radius: 50%;
width: 20px;
height: 20px;
top: 30px;
}
.eyeleft {
left: 4px;
}
.eyeright {
right: 4px;
}
.eyeleft:before,
.eyeleft:after,
.eyeright:before,
.eyeright:after {
content: '';
background: #fff;
width: 7px;
height: 7px;
top: 4px;
left: 4px;
border-radius: 50%;
position: absolute;
z-index: 5;
}
.eyeleft:after,
.eyeright:after {
width: 4px;
height: 4px;
top: 10px;
left: 10px;
}
.closedeyeleft,
.closedeyeright {
background: transparent none repeat scroll 0 0;
border-color: #000;
border-radius: 5px;
border-style: solid;
border-width: 4px 4px 0;
height: 4px;
top: 35px;
width: 12px;
}
.closedeyeleft {
left: 3px;
}
.closedeyeright {
right: 3px;
}
Run Code Online (Sandbox Code Playgroud)
Col*_*inD 14
感谢一个结构良好的问题,有很多细节!
这是一个提供快速闪烁以及随机第二次闪烁的潜在解决方案.
//made blink a named function to improve readability a bit
var blink = function(isSecondBlink) {
//got rid of the ternary expressions since we're always doing
//open eyes -> close eyes -> delay -> open eyes
//close both eyes
eyeleft.className = "closedeyeleft";
eyeright.className = "closedeyeright";
//let's reopen those eyes after a brief delay to make a nice blink animation
//as it happens, humans take ~250ms to blink, so let's use a number close to there
setTimeout(function() {
eyeleft.className = "eyeleft";
eyeright.className = "eyeright";
}, 200);
if (isSecondBlink) { return; } //prevents us from blinking 3+ times
//This provides a 40% chance of blinking twice, adjust as needed
var blinkAgain = Math.random() <= 0.4;
//if we need to blink again, go ahead and do it in 300ms
if (blinkAgain) {
setTimeout(function() { blink(true); }, 300);
}
}
//go ahead and blink every 2 seconds
window.onload = setInterval(blink, 2000);
Run Code Online (Sandbox Code Playgroud)