const sounds = [
{
key: 'Q',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
title: 'Title Q'
},
{
key: 'W',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3',
title: 'Title W'
},
{
key: 'E',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
title: 'Title E'
},
{
key: 'A',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3',
title: 'Title A'
},
{
key: 'S',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3',
title: 'Title S'
},
{
key: 'D',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
title: 'Title D'
},
{
key: 'Z',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3',
title: 'Title Z'
},
{
key: 'X',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3',
title: 'Title X'
},
{
key: 'C',
mp3: 'https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3',
title: 'Title C'
}
]
const keys = ['Q','W','E','A','S','D','Z','X','C'];
const App = () => (
<div id='drum-machine' className='container'>
<div id='display' className='display'>
<h1> Title </h1>
{sounds.map((sound, idx, title) => (
<Box text={sound.key} key={idx} audio={sound.mp3} title={sound.title}/>
))}
</div>
</div>
);
class Box extends React.Component {
constructor(props) {
super(props);
this.audio = React.createRef();
}
playSound = () => {
this.audio.current.play();
window.document.querySelector('h1').innerText = this.props.title
}
render() {
const {text, audio} = this.props
return (
<div className='drum-pad' onClick={this.playSound} id={`drum-${text}`} >{text}<audio ref={this.audio} src={audio} className='clip' id={text}/>
</div>
)
}
}
document.addEventListener('keydown', (e) => {
{/*sur l'appui d'une touche je crèe un id égale à la key, si cet id correspond à ce lui de audio, je joue l'audio */}
const id = e.key.toUpperCase();
const audio = document.getElementById(id);
if (audio) {
const parent = audio.parentNode;
parent.classList.add('active');
audio.play();
{/*Lorsque la touche est relachéé, la touche repasse en affichage normal.
audio.addEventListener('ended', () => {
parent.classList.remove('active')
})*/}
}
});
ReactDOM.render(<App />, document.getElementById('app')); @import url('https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900&display=swap');
* {
box-sizing: border-box;
}
body {
background-color: #9b59b6;
font-family: 'Lato', sans-serif;
height: 100vh;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
.container { /* container de l'App id 'drum-machine' */
background-color: grey;
padding: 10px;
width: 380px;
}
.display { /* container des touches */
background-color: #fff;
width: 360px;
display: flex;
flex-wrap: wrap;
}
.display h1 {
display: block;
width: 100%;
text-align: center;
}
.drum-pad { /* touche */
height: 100px;
width: 100px;
border: 2px solid #333;
border-radius: 4px;
background-color: #8e44ad;
cursor: pointer;
color: #fff;
margin: 10px;
line-height: 100px;
text-align: center;
font-size: 60px;
/* Alternative pour centrer les lettre:
display: flex;
justify-content: center;
align-items: center;
*/
}<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='app'><div>
这是我的代码链接:删除!我把所有代码都加进去了。这是预览窗口中的作品。
我尝试在div元素上添加一个'active‘类,并使用className=’鼓-pad‘。但这不管用,我不知道为什么。在JavaScript代码的末尾,看看addEventListener。
Ps :我不知道为什么,但我编辑我的信息,以纠正它,我不能提交它。一条短信问我“更多的细节”。所以我就为这个加了一些短信。有人能说我的pb到底是为了我未来的职位吗?
发布于 2022-04-04 10:45:44
如果将鼓的状态保存在App中,则可以将该支柱传递给Box组件。Box组件可以决定是立即播放声音,还是等待单击。
但是最重要的是事件发生在窗口上,并允许使用React来驱动代码。
我还通过使用CSS网格减少了您的CSS代码。
const { useEffect, useRef, useState } = React;
const sounds=[{key:"Q",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3"},{key:"W",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"},{key:"E",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3"},{key:"A",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3"},{key:"S",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3"},{key:"D",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3"},{key:"Z",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3"},{key:"X",mp3:"https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3"},{key:"C",mp3:"https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3"}];
// Passing in the sounds into the App component
function App({ sounds }) {
// Creating an initial state with an array of
// keys from the sounds object set to false
const state = sounds.map(obj => ({ [obj.key]: false }));
// Set that state
const [ isPlaying, setIsPlaying ] = useState(state);
// The useEffect sets up the keydown event on the window
// When a key is pressed the state is updated, and a render takes place
useEffect(() => {
window.addEventListener('keydown', (e) => {
setIsPlaying({ ...isPlaying, [e.key.toUpperCase()]: true });
});
// Clean up
return function () {
window.removeEventListener('keydown');
}
}, []);
// So now we're passing the property (key)
// of state down to the Box component
return (
<div className="container">
{sounds.map((sound, idx) => (
<Box
text={sound.key}
key={idx}
audio={sound.mp3}
isPlaying={isPlaying[sound.key]}
/>
))}
</div>
);
}
function Box({ text, audio, isPlaying }) {
// Still using `ref` but I've changed this to
// a function component so now we use `useRef`
const ref = useRef();
// Play the sound
function playSound() {
ref.current.play();
}
// If `isPlaying` is true play the sound
if (isPlaying) playSound();
// Otherwise wait for the click event on the
// div and then play the sound
return (
<div
className="box"
onClick={playSound}
>{text}
<audio ref={ref} src={audio} />
</div>
)
}
ReactDOM.render(
<App sounds={sounds} />,
document.getElementById('react')
);.container {
width: 300px;
display: grid;
grid-template-columns: repeat(3, 100px);
}
.box {
display: flex;
align-items: center;
justify-content: center;
font-size: 2em;
background-color: #8e44ad;
cursor: pointer;
color: #fff;
margin: 0.2em;
padding: 0.2em;
}<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>
https://stackoverflow.com/questions/71734162
复制相似问题