javascript - 我的边框切换颜色之间有延迟。想知道如何解决这个问题,因为它看起来很奇怪
问题描述
完整代码: https ://github.com/XxTyaftioNxX/RockPaperScissors jsfiddle: https ://jsfiddle.net/45y3sdth/
这里记分板的边界比选项按钮的边界早一秒 userChoice_div = document.getElementById(userChoice); scoreBoard_Id = document.getElementById('score');
js
if(resultType === "tie"){
//changing the color of the option border
userChoice_div.classList.add('gray-glow');
setTimeout(() => userChoice_div.classList.remove('gray-glow'), 300);
//chnaging the color of the scoreboard border
scoreBoard_Id.classList.add('gray-glow');
setTimeout(() => scoreBoard_Id.classList.remove('gray-glow'), 300);
//increasing the score
userScore = userScore + 1;
userScore_span.innerHTML = userScore;
computerScore = computerScore + 1;
computerScore_span.innerHTML = computerScore;
css
.score-board{
margin: 20px auto;
border: 4px solid white;
border-radius: 5px;
text-align: center;
font-family: Times New Roman;
width: 200px;
color: white;
font-size: 30px;
padding: 15px 20px;
position: relative;
}
.gray-glow{
border: 4px solid gray;
box-shadow: 0 0 10px rgb(61, 68, 70);
}
html
<div class="score-board" id="score">
<div id="user-label" class="badge">user</div>
<div id ="comp-label"class="badge">comp</div>
<span id="user-score">0</span>:<span id="computer-score">0</span>
</div>
<div class="result">
<p>LETS GET THAT BREAD</p>
</div>
<div class="choices">
<div class="choice" id="rock">
<img src="assets/rock.png" alt="">
</div>
<div class="choice" id="paper">
<img src="assets/paper.png" alt="">
</div>
<div class="choice" id="scissors">
<img src="assets/scissors.png" alt="">
</div>
</div>
</div> ```
解决方案
检查您的代码 setTimeout 函数中是否设置了 300,这是导致边框更改延迟设置为 0 或删除设置超时功能的原因。
setTimeout(() => scoreBoard_Id.classList.remove('gray-glow'), 0);
推荐阅读
- java - 应用更新后无法从共享首选项加载数据
- java - 返回几个项目列表
- python - 由于页面自动重新加载,使用 selenium 无限向下滚动总是失败
- node.js - NodeJS cors 不过滤任何东西
- r - r: Assign n dataframe subsets from a list created in a custom function to objects in global environment
- python - 数据框打印为空数据框?
- python - 如何使用python仅使用一个循环迭代二维矩阵的所有元素
- bash - 使用 cron 扩展参数
- azure - 部署在 Azure 中的 API 不起作用,我不知道为什么
- css - 使用背景滤镜时无法移除滚动上的模糊边缘:模糊