如果我按下A或S这样的按键按钮几秒钟而不抬起我的手指,那么转换效果就会“sticks”。并且玩的类别没有被删除。如果我只是点击A,则添加播放类,然后删除。我注意到,如果我评论出某个声明,它的作用就像它应该的那样。为什么? (添加注释掉的语句以查看我的意思)。
https://codepen.io/anon/pen/dWpxqg
// If i keep a key button like A or S pressed down all the time, eventually the transform effect (yellow border appearing) doesn't go away. If i simply tap A a class of Playing is added and then removed. I noticed that if i comment out a certain statement it works like it's supposed to. Why?
function removeTransition(e) {
// COMMENTED STATEMENT BELOW IS PROBLEMATIC.
// if(e.propertyName !== 'transform') return;
this.classList.remove('playing');
}
function playSound(e) {
const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
if(!audio) return;
audio.currentTime = 0;
audio.play();
key.classList.add('playing');
}
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);

html {
box-sizing: border-box; }
*,
*:before,
*:after {
box-sizing: inherit; }
html {
height: 100%; }
html body {
margin: 0;
padding: 0;
min-height: 100%;
color: white; }
.root {
display: flex;
flex-direction: column;
height: 100vh;
text-align: center;
background: url("../img/crowd.jpg") bottom center;
background-size: cover; }
.section {
display: flex;
justify-content: center;
align-items: center;
flex: 1; }
.key {
display: inline-block;
margin: 10px;
width: 80px;
border: 5px solid black;
border-radius: 5px;
transition: all .7s ease;
background-color: rgba(0, 0, 0, 0.4); }
.key kbd {
padding: 5px 10px 0 10px;
display: block;
font-size: 3rem; }
.key span {
color: gold;
padding-bottom: 10px;
text-transform: uppercase;
letter-spacing: 1px;
font-size: 0.7rem;
font-weight: 700;
font-family: sans-serif; }
.playing {
transform: scale(1.1);
border-color: #ffc600;
box-shadow: 0 0 1rem #ffc600; }
.k-grp-1 {
display: flex;
justify-content: space-between; }
footer {
background-color: #303F9F; }

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="root">
<section class="section">
<div class="keys">
<div class="k-grp-1">
<div data-key="65" class="key">
<kbd>A</kbd>
<span class="sound">clap</span>
</div>
<div data-key="83" class="key">
<kbd>S</kbd>
<span class="sound">hihat</span>
</div>
<div data-key="68" class="key">
<kbd>D</kbd>
<span class="sound">kick</span>
</div>
<div data-key="70" class="key">
<kbd>F</kbd>
<span class="sound">openhat</span>
</div>
<div data-key="71" class="key">
<kbd>G</kbd>
<span class="sound">boom</span>
</div>
</div>
<div class="k-grp-2">
<div data-key="72" class="key">
<kbd>H</kbd>
<span class="sound">ride</span>
</div>
<div data-key="74" class="key">
<kbd>J</kbd>
<span class="sound">snare</span>
</div>
<div data-key="75" class="key">
<kbd>K</kbd>
<span class="sound">tom</span>
</div>
<div data-key="76" class="key">
<kbd>L</kbd>
<span class="sound">tink</span>
</div>
</div>
</div>
</section>
<footer> Footer</footer>
</div>
<audio data-key="65" src="sounds/clap.wav"></audio>
<audio data-key="83" src="sounds/hihat.wav"></audio>
<audio data-key="68" src="sounds/kick.wav"></audio>
<audio data-key="70" src="sounds/openhat.wav"></audio>
<audio data-key="71" src="sounds/boom.wav"></audio>
<audio data-key="72" src="sounds/ride.wav"></audio>
<audio data-key="74" src="sounds/snare.wav"></audio>
<audio data-key="75" src="sounds/tom.wav"></audio>
<audio data-key="76" src="sounds/tink.wav"></audio>
<script src="js/main.js"></script>
</body>
</html>
&#13;
答案 0 :(得分:1)
我将尝试通过在事件回调中添加几行来演示特定条件语句的问题,如下所示:
var counts = {};
function removeTransition (e) {
// if ( (e.propertyName === 'transform') ) {
// return;
// }
if (typeof counts[e.propertyName] === 'undefined') {
counts[e.propertyName] = 1;
} else {
counts[e.propertyName] = counts[e.propertyName] + 1;
}
// console.log(e.propertyName);
this.classList.remove('playing');
console.log(counts);
}
我只是添加了一张地图,以便在按住按钮的同时跟踪 e.propertyName 属性可以拥有的不同值的不同数量。
例如,如果我按住'A'键5“然后停止我得到以下测量:
{
"transform": 29,
"border-right-color": 58,
"border-bottom-color": 58,
"border-left-color": 58,
"border-top-color": 58,
"box-shadow": 30
}
正如您将注意到的那样, removeTransition()函数被调用的总次数 - 由 transitionend 事件触发 - 转换与其他CSS属性相比,值几乎发生了一半的时间。
这意味着如果有人按下按钮(无论多长时间), keydown 事件将被持续触发并且正在播放类将在转向添加到元素。但问题是,如果您只检查要接收的'transform' CSS属性,那么与其他CSS属性相比,它将捕获一半的时间,主要是由于它的异步性质,因为它需要更长的时间才能完成。