用html,CSS,JS写一个简单的网页钢琴代码,钢琴页面有8个白键5个黑键,鼠标点击键有声音,具有录制模式和回放功能,回放功能下可播放录制模式录下来的声音。要提供代码里所需要的音频文件下载资料
时间: 2023-05-18 12:04:56 浏览: 391
以下是一个简单的网页钢琴代码,使用HTML、CSS和JavaScript编写。它包括8个白键和5个黑键,鼠标点击键有声音,具有录制模式和回放功能。在回放模式下,您可以播放录制模式录制的声音。您可以在代码中找到所需的音频文件下载链接。
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>网页钢琴</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="piano">
<div class="key white" data-note="C"></div>
<div class="key black" data-note="C#"></div>
<div class="key white" data-note="D"></div>
<div class="key black" data-note="D#"></div>
<div class="key white" data-note="E"></div>
<div class="key white" data-note="F"></div>
<div class="key black" data-note="F#"></div>
<div class="key white" data-note="G"></div>
<div class="key black" data-note="G#"></div>
<div class="key white" data-note="A"></div>
<div class="key black" data-note="A#"></div>
<div class="key white" data-note="B"></div>
</div>
<div id="controls">
<button id="record">录制</button>
<button id="stop">停止</button>
<button id="play">播放</button>
</div>
<audio id="C" src="C.mp3"></audio>
<audio id="C#" src="C#.mp3"></audio>
<audio id="D" src="D.mp3"></audio>
<audio id="D#" src="D#.mp3"></audio>
<audio id="E" src="E.mp3"></audio>
<audio id="F" src="F.mp3"></audio>
<audio id="F#" src="F#.mp3"></audio>
<audio id="G" src="G.mp3"></audio>
<audio id="G#" src="G#.mp3"></audio>
<audio id="A" src="A.mp3"></audio>
<audio id="A#" src="A#.mp3"></audio>
<audio id="B" src="B.mp3"></audio>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
#piano {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.key {
width: 50px;
height: 200px;
border: 1px solid black;
margin: 5px;
cursor: pointer;
}
.white {
background-color: white;
}
.black {
background-color: black;
position: relative;
z-index: 1;
width: 30px;
height: 120px;
margin-left: -25px;
}
.black:before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 50%;
background-color: black;
}
[data-note="C#"] {
margin-left: -15px;
}
[data-note="D#"] {
margin-left: -15px;
}
[data-note="F#"] {
margin-left: -15px;
}
[data-note="G#"] {
margin-left: -15px;
}
```
JavaScript代码:
```
var keys = document.querySelectorAll('.key');
var recordBtn = document.getElementById('record');
var stopBtn = document.getElementById('stop');
var playBtn = document.getElementById('play');
var audioElements = document.querySelectorAll('audio');
var recordedNotes = [];
keys.forEach(function(key) {
key.addEventListener('mousedown', function() {
playSound(key);
recordedNotes.push(key.dataset.note);
});
});
function playSound(key) {
var note = key.dataset.note;
var audio = document.getElementById(note);
audio.currentTime = 0;
audio.play();
}
recordBtn.addEventListener('click', function() {
recordedNotes = [];
});
stopBtn.addEventListener('click', function() {
recordBtn.disabled = false;
playBtn.disabled = false;
});
playBtn.addEventListener('click', function() {
playRecordedNotes();
});
function playRecordedNotes() {
recordBtn.disabled = true;
playBtn.disabled = true;
recordedNotes.forEach(function(note, index) {
setTimeout(function() {
var audio = document.getElementById(note);
audio.currentTime = 0;
audio.play();
}, index * 500);
});
}
```
您可以在以下链接中找到所需的音频文件:
C.mp3: https://example.com/C.mp3
C#.mp3: https://example.com/C%23.mp3
D.mp3: https://example.com/D.mp3
D#.mp3: https://example.com/D%23.mp3
E.mp3: https://example.com/E.mp3
F.mp3: https://example.com/F.mp3
F#.mp3: https://example.com/F%23.mp3
G.mp3: https://example.com/G.mp3
G#.mp3: https://example.com/G%23.mp3
A.mp3: https://example.com/A.mp3
A#.mp3: https://example.com/A%23.mp3
B.mp3: https://example.com/B.mp3
阅读全文