ios
web
首發:GitHubClub原文:juejin.im/post/6879708939190009869
學生時代的咱們若是有像郎朗同樣的琴技,想必追起女生會很是的容易,但在之前,一架鋼琴對普通家庭來講,消費還蠻高的,因此咱們不如本身寫一架屬於本身的鋼琴,哈哈,雖然音效不如真的鋼琴,可是能寫出來,還挺有成就感的。
數組
<ul>
<ul>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div></li>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div></li>
<li><div></div></li>
</ul>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div></li>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div><p></p></li>
<li><div></div></li>
<li><div></div></li>
</ul>
/*-------鋼琴部分--------*/
ul{
width:480px;
height:360px;
transform: translate(-50%, -50%);
position: absolute;
top: 50%;
left: 50%;
}
li{
float:left;
list-style-type: none;
position: relative;
}
li>div{
height: 360px;
width: 60px;
background: rgba(255,255,255,.3);
border: 1px solid;
border-color:rgba(0, 0, 0, .8);
border-bottom-left-radius : 8px;
border-bottom-right-radius: 8px;
box-sizing: border-box;
box-shadow: /*inset 3px 0 10px #c9c6c6,*/
inset 5px -8px 0 #00000023;
text-align: center;
display:table-cell;
vertical-align: bottom;
}
li:not(:last-child)>div{
border-right: none;
}
li>div:hover{
background: rgb(255, 97, 97);
}
/* 當白鍵按下時的樣式 */
.white_active{
box-shadow: inset 3px -2px 0 #00000036;
background: linear-gradient(45deg, rgba(255,255,255,.7), rgba(255,255,255,.5));
}
li>p{
height: 200px;
width: 40px;
background-color: #000;
border-bottom-left-radius : 5.5px;
border-bottom-right-radius: 5.5px;
box-shadow: inset 5px -7px 0 #2c2c2c;
position: absolute;
top:0;
left: 40px;
z-index: 999;
}
li>p:hover{
background: linear-gradient(45deg, #4c4c4c, #444444);
}
/* 當黑鍵按下時的樣式 */
.black_active{
box-shadow:inset 3px -5px 0 #2c2c2c;
}
// 給鋼琴添加音頻
// 給鋼琴添加音頻
for (var i = 1; i <= 8; i++) {
var addaudio = document.createElement("audio");
addaudio.setAttribute("src", "audios/" + "w" + i + ".ogv");
document.body.appendChild(addaudio);
}
for (var i = 1; i <= 5; i++) {
var addaudio = document.createElement("audio");
addaudio.setAttribute("src", "audios/" + "b" + i + ".ogv");
document.body.appendChild(addaudio);
}
for (var i = 1; i <= 8; i++) {
var addaudio = document.createElement("audio");
addaudio.setAttribute("src", "audios/" + "w" + i + ".ogv");
document.body.appendChild(addaudio);
}
for (var i = 1; i <= 5; i++) {
var addaudio = document.createElement("audio");
addaudio.setAttribute("src", "audios/" + "b" + i + ".ogv");
document.body.appendChild(addaudio);
}
let audios = document.getElementsByTagName("audio"),
buttons = document.querySelectorAll("ul>li>div"),
blacks = document.querySelectorAll("ul>li>p");
微信
let audios = document.getElementsByTagName("audio"),
buttons = document.querySelectorAll("ul>li>div"),
blacks = document.querySelectorAll("ul>li>p");
for (var i = 0; i < 8; i++) {
buttons[i].index = i;
buttons[i].onmousedown = function () {
//alert(this.index);
buttons[this.index].classList.add('white_active')
audios[this.index].load();
audios[this.index].play();
};
buttons[i].onmouseup = function () {
buttons[this.index].classList.remove('white_active')
};
}
for (var i = 0; i < 5; i++) {
blacks[i].index = i + 8;
blacks[i].onmousedown = function () {
//alert(this.index);
blacks[this.index-8].classList.add('black_active')
audios[this.index].load();
audios[this.index].play();
//alert(audios[this.index].src);
};
blacks[i].onmouseup = function () {
blacks[this.index-8].classList.remove('black_active')
};
}
for (var i = 0; i < 8; i++) {
buttons[i].index = i;
buttons[i].onmousedown = function () {
//alert(this.index);
buttons[this.index].classList.add('white_active')
audios[this.index].load();
audios[this.index].play();
};
buttons[i].onmouseup = function () {
buttons[this.index].classList.remove('white_active')
};
}
for (var i = 0; i < 5; i++) {
blacks[i].index = i + 8;
blacks[i].onmousedown = function () {
//alert(this.index);
blacks[this.index-8].classList.add('black_active')
audios[this.index].load();
audios[this.index].play();
//alert(audios[this.index].src);
};
blacks[i].onmouseup = function () {
blacks[this.index-8].classList.remove('black_active')
};
}
// 操做鍵盤鋼琴白鍵發出聲音
var keyCodes = new Array(83, 68, 70, 71, 72, 74, 75, 76, 69, 82, 89, 85, 73);
document.body.onkeydown = function (e) {
for (var i = 0; i < keyCodes.length; i++) {
if (e.keyCode == keyCodes[i]) {
if (i < 8) {
buttons[i].classList.add('white_active');
} else {
blacks[i-8].classList.add('black_active');
}
audios[i].load();
audios[i].play();
}
}
};
// 操做鍵盤鋼琴黑鍵發出聲音
document.body.onkeyup = function (e) {
//document.title=e.keyCode;
for (var i = 0; i < keyCodes.length; i++) {
if (e.keyCode == keyCodes[i]) {
if (i < 8) {
buttons[i].classList.remove('white_active');
} else {
blacks[i-8].classList.remove('black_active');
}
}
}
};
// 操做鍵盤鋼琴白鍵發出聲音
var keyCodes = new Array(83, 68, 70, 71, 72, 74, 75, 76, 69, 82, 89, 85, 73);
document.body.onkeydown = function (e) {
for (var i = 0; i < keyCodes.length; i++) {
if (e.keyCode == keyCodes[i]) {
if (i < 8) {
buttons[i].classList.add('white_active');
} else {
blacks[i-8].classList.add('black_active');
}
audios[i].load();
audios[i].play();
}
}
};
// 操做鍵盤鋼琴黑鍵發出聲音
document.body.onkeyup = function (e) {
//document.title=e.keyCode;
for (var i = 0; i < keyCodes.length; i++) {
if (e.keyCode == keyCodes[i]) {
if (i < 8) {
buttons[i].classList.remove('white_active');
} else {
blacks[i-8].classList.remove('black_active');
}
}
}
};
<div>
請輸入簡譜:
<input type="text" id="song" placeholder="請輸入歌曲名">
<button id="check">肯定</button>
</div>
<div>
請輸入簡譜:
<input type="text" id="song" placeholder="請輸入歌曲名">
<button id="check">肯定</button>
</div>
而後開始寫邏輯部分,scroe 對象是用來存放歌曲信息的,不想去網上找歌曲信息的,能夠直接複製最後面部分的簡譜。格式相似於 {‘歌曲名’:‘歌詞和簡譜’}
,這裏能夠用模板字符串的形式。app
var scroe = {};
var numReg = /[1-9]/g;
var wordReg = /[\u4e00-\u9fa5]+/g;
var str;
var shows;
var words;
var wordsIndex;
var wordsCurrent;
var current;
function play(){
buttons.forEach((el)=>el.classList.remove('show'))
var songName = song.value;
if(songName === ''){
return alert('請輸入歌曲名')
}
if(scroe.hasOwnProperty(songName)){
str = scroe[songName];
shows = str.match(numReg);
words = str.match(wordReg);
wordsIndex = 0;
wordsCurrent = 0;
current = 0;
console.log(songName)
buttons[shows[0]/1 - 1].classList.add('show');
text.textContent = words[0]
}else{
return alert('您輸入的歌曲名還沒有加入曲庫')
}
}
check.addEventListener('click',play)
var scroe = {};
var numReg = /[1-9]/g;
var wordReg = /[\u4e00-\u9fa5]+/g;
var str;
var shows;
var words;
var wordsIndex;
var wordsCurrent;
var current;
function play(){
buttons.forEach((el)=>el.classList.remove('show'))
var songName = song.value;
if(songName === ''){
return alert('請輸入歌曲名')
}
if(scroe.hasOwnProperty(songName)){
str = scroe[songName];
shows = str.match(numReg);
words = str.match(wordReg);
wordsIndex = 0;
wordsCurrent = 0;
current = 0;
console.log(songName)
buttons[shows[0]/1 - 1].classList.add('show');
text.textContent = words[0]
}else{
return alert('您輸入的歌曲名還沒有加入曲庫')
}
}
check.addEventListener('click',play)
function start(){
function start(){
// 顯示歌詞的部分
if(wordsCurrent>=words[wordsIndex].length-1){
wordsCurrent = 0;
wordsIndex++;
text.textContent = words[wordsIndex]
}else{
wordsCurrent++
}
if(current === shows.length-1){
buttons[shows[shows.length-1]/1 - 1].classList.remove('show');
return console.log('演奏結束',current)
}
var showButton = shows[current+1]/1 - 1;
var hiddenButton = shows[current]/1 - 1;
buttons[hiddenButton].classList.remove('show');
buttons[showButton].classList.add('show');
current++
}
// 顯示歌詞的部分
if(wordsCurrent>=words[wordsIndex].length-1){
wordsCurrent = 0;
wordsIndex++;
text.textContent = words[wordsIndex]
}else{
wordsCurrent++
}
if(current === shows.length-1){
buttons[shows[shows.length-1]/1 - 1].classList.remove('show');
return console.log('演奏結束',current)
}
var showButton = shows[current+1]/1 - 1;
var hiddenButton = shows[current]/1 - 1;
buttons[hiddenButton].classList.remove('show');
buttons[showButton].classList.add('show');
current++
}
而後再在對應的按鍵事件和鼠標點擊事件加上這段代碼。dom
i == shows[current]-1 ? start() : '';
如今咱們想要的效果基本已經實現。編輯器
// 給琴鍵加上數字
buttons.forEach((el,index)=>el.textContent = index+1)
window.addEventListener('keydown',function(e){
if(e.keyCode == 86){
if(buttons[0].textContent != ''){
buttons.forEach((el)=>el.textContent = '')
}else{
buttons.forEach((el,index)=>el.textContent = index+1)
}
}
})
// 給琴鍵加上數字
buttons.forEach((el,index)=>el.textContent = index+1)
window.addEventListener('keydown',function(e){
if(e.keyCode == 86){
if(buttons[0].textContent != ''){
buttons.forEach((el)=>el.textContent = '')
}else{
buttons.forEach((el,index)=>el.textContent = index+1)
}
}
})
let x = document.getElementById("btn-js"),
y = document.getElementById("js"),
//介紹的隱藏與顯示
function hide() {
y.style.display === "block" ? y.style.display = "none" : y.style.display = "block"
}
x.addEventListener('click',hide);
let x = document.getElementById("btn-js"),
y = document.getElementById("js"),
//介紹的隱藏與顯示
function hide() {
y.style.display === "block" ? y.style.display = "none" : y.style.display = "block"
}
x.addEventListener('click',hide);
該項目的 Gitee 地址:關注微信公衆號 逛逛GitHub,回覆 鋼琴 獲取。 推薦閱讀 1. 給新手的 11 個 Docker 免費上手項目 2. 我在 GitHub 上找到開源的《植物大戰殭屍》 3. 推薦 22 款好用的命令行工具 4. 百度網盤忽然大調整
喜歡文章,點個 在看
本文分享自微信公衆號 - 逛逛GitHub(ggGithub)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。ide