๐ฎ ๊ฒ์ ํจ๊ณผ 01 : ๋ฎค์ง ํ๋ ์ด์ด
์ด๋ฒ ์๊ฐ์๋ ๊ฒ์ ํจ๊ณผ 1๋ฒ์งธ ์ค ๋ฎค์งํ๋ ์ด์ด๋ฅผ ๊ตฌํํ๋ ์ค์ต์ ์งํํ๋๋ก ํ๊ฒ ์ต๋๋ค!
#1. HTML / CSS ์ค์ ํ๊ธฐ
! HTML TIP !
ํ๋ ์ด์ด์ ๊ตฌ์กฐ๋ฅผ ์์ฑํฉ๋๋ค. ๋ํ ์คํฌ๋ฆฝํธ๋ก ์์ฑํ ๋ถ๋ถ๋ค์ ์ฃผ์์ฒ๋ฆฌ๋ฅผ ํด์ค๋๋ค.
โ๏ธ HTML ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ
<!-- ๋ฎค์ง ํ๋ ์ด์ด -->
<div class="music__wrap">
<div class="music_inner">
<div class="music__header">
<div>*</div>
<h2>MUSIC PlAYER</h2>
<div>*</div>
</div>
<div class="music__contents">
<div class="music__view">
<div class="img">
<img src="../assets/img/game_musicPlayer_img01.png" alt="game_musicPlayer_img01">
</div>
<div class="title">
<h3>BACK ON TOP (feat.Griff) - HONNE</h3>
<p>YOUTUBE MUSIC</p>
</div>
</div>
<div class="music__control">
<div class="progress">
<div class="bar">
<audio id="main-audio" src="../assets/audio/music-1.mp3"></audio>
</div>
<div class="timer">
<span class="current">0:00</span>
<span class="duration">4:00</span>
</div>
</div>
<div class="control">
<i title="์ ์ฒด ๋ฐ๋ณต" class="repeat" id="control-repeat"></i>
<!-- <i title="ํ๊ณก ๋ฐ๋ณต" class="repeat_one"></i> -->
<i title="์ด์ ๊ณก" class="prev" id="control-prev"></i>
<i title="์ฌ์" class="play" id="control-play"></i>
<i title="๋์๊ณก" class="next" id="control-next"></i>
<i title="์ฌ์๋ชฉ๋ก" class="list" id="control-list"></i>
<i title="๋ณผ๋ฅจ" class="sound" id="control-sound"> <input type="range" min="1" max="15" id="player_sound" class="open"></i>
<!-- <i title="ํ๊ณก ๋ฐ๋ณต" class="repeat_one"></i>
<i title="๋๋ค ๋ฐ๋ณต" class="shuffle"></i> -->
<!-- <i title="์ ์ง" class="pause"></i> -->
</div>
</div>
</div>
<div class="music__footer">
<div class="music__list">
<h3><span class="list"></span>MUSIC LIST<span class="close-btn"></span></h3>
<ul>
<!-- <li>
<strong>์ ๋ชฉ</strong>
<em>์ํฐ์คํธ</em>
<span>์ฌ์์๊ฐ</span>
</li> -->
</ul>
</div>
</div>
</div>
</div>
<!-- // ๋ฎค์ง ํ๋ ์ด์ด -->
! CSS TIP !
์ํ๋ ๋๋์ ๊ฒ์ ๋ฐฐ๊ฒฝ๊ณผ ํ๋ ์ด์ด ์คํ์ผ์ ์ง์ ํด์ฃผ์๋ฉด ๋ฉ๋๋ค.
๋ํ ์ฃผ์์ฒ๋ฆฌ๋ ๋ฒํผ๋ ์์น๋ฅผ ์กฐ์ ํ์ฌ ์คํธ๋ฆฝํธ ์์ฑ ์ ๋ฌธ์ ๊ฐ ์๊ธฐ์ง ์๋๋ก ํฉ๋๋ค.
โ๏ธ CSS ์คํฌ๋ฆฝํธ ๋ณด๊ธฐ
/* music */
.music__wrap .music_inner {
width: 450px;
background: #000;
position: absolute;
right: 100px;
top: 100px;
padding: 0 10px 10px 10px;
}
.music__wrap .music__header {
width: 100%;
height: 30px;
display: flex;
align-items: center;
justify-content: space-between;
color: #f5f5f5;
}
.music__wrap .music__header h2 {
font-family: 'YdestreetB';
font-size: 14px;
}
.music__wrap .music__contents {
background: #b0b0b0;
width: 100%;
}
.music__view {
display: flex;
padding: 20px;
}
.music__view .img {
width: 30%;
}
.music__view .img img {
width: 100%;
}
.music__view .title {
width: 70%;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.music__view .title h3 {
margin-bottom: 5px;
font-size: 20px;
line-height: 1.2;
}
.music__view .title p {
color: #323232;
}
.music__control {
width: 100%;
height: 100px;
background: #b0b0b0;
padding: 0 20px 20px 20px;
}
.music__control .progress {
width: 100%;
height: 6px;
background: #000;
border-radius: 5px;
}
.music__control .progress .bar {
width: 0;
height: inherit; /* ๋์ด๋ ์์๋ฐ๊ธฐ */
background: #c405e2;
border-radius: 5px;
}
.music__control .progress .timer {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 3px;
}
.music__control .control {
display: flex;
align-items: center;
justify-content: space-evenly;
margin-top: 30px;
}
.music__control .control .repeat {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -205px;
}
.music__control .control .repeat_one {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -240px;
}
.music__control .control .shuffle {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -274px;
}
.music__control .control .prev {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -70px;
}
.music__control .control .play {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: 4px;
}
.music__control .control .stop {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -136px;
}
.music__control .control .next {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -35px;
}
.music__control .control .list {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -167px;
}
.music__control .control .sound {
width: 35px;
height: 35px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
background-repeat: no-repeat;
background-position: -107px;
}
.music__control .control .sound #player_sound.open {
display: none;
}
.music__footer {
background: #b0b0b0;
}
.music__list {
padding: 0 20px 20px 20px;
display: none;
}
.music__list h3 {
font-family: 'YdestreetB';
border-top: 2px solid #000;
padding-top: 10px;
font-size: 20px;
}
.music__list h3 .list {
display: inline-block;
margin-right: 5px;
vertical-align: middle;
margin-bottom: 7px;
background-image: url(../img/game_musicPlayer_icons.svg);
background-size: cover;
width: 25px;
height: 25px;
background-position: -119px -24px;
}
.music__list ul {
max-height: 180px;
overflow-y: scroll;
}
.music__list li {
border-bottom: 1px dashed #888;
list-style: none;
position: relative;
padding: 10px 0 5px;
}
.music__list li.playing {
color: #c405e2;
}
.music__list strong {
display: block;
font-size: 18px;
margin-bottom: 4px;
}
.music__list em {
font-style: normal;
}
.music__list li span {
position: absolute;
right: 0;
top: 10px;
}
.music__list .close-btn {
display: inline-block;
float: right;
margin-top: 3px;
margin-right: 5px;
vertical-align: middle;
background-image: url(../img/game_music_close-btn.svg);
background-size: cover;
width: 18px;
height: 18px;
}
#2. JAVASCRIPT : ์คํฌ๋ฆฝํธ
์ด๋ฒ ์คํฌ๋ฆฝํธ์์๋ ๋ฐฐ๊ฒฝ์ ํค๋์ ํธํฐ ๋ถ๋ถ์ ํ์ฌ ์๊ฐ, ๋ ์ง, ์ฌ์ฉํ๊ณ ์๋ ๋๋ฐ์ด์ค ์ข
๋ฅ๊ฐ ๋์ค๋๋ก ํด๋ณด๊ฒ ์ต๋๋ค.
๋ํ ๋ฎค์ง ํ๋ ์ด์ด ๊ตฌํ๊ณผ ์ ์ด์ฟผ๋ฆฌ๋ฅผ ์ด์ฉํ ๋๋๊ทธ ํจ๊ณผ๋ ํจ๊ป ๊ตฌํํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
! JAVASCRIPT TIP !
1. now.getFullYear() = ๋
/ (now.getMonth() = ์ / now.getDate() = ์ผ / now.getHours() = ์ / now.getMinutes() = ๋ถ / now.getSeconds() = ์ด
2. ๋
ธ๋, ์ํฐ์คํธ, ์ ๋ชฉ ๋ฑ์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด ์ฃผ์ด ์คํฌ๋ฆฝํธ๋ก ์๋๋๋๋ก ํด์ค๋๋ค. ๋จ ํ์ผ๋ช
๊ณผ ์ด๋ฆ์ด ๊ฐ์์ผํ๋ฉฐ ์์๋๋ก ์์ฑํด์ฃผ์ด์ผ ํฉ๋๋ค.
๐ก ๋๋๊ทธ, ์์ด์ฝ ์๊ฐ, ๋ ์ง, ๋๋ฐ์ด์ค
// 0. ๋ง์ฐ์ค ์ปค์
const cursor = document.querySelector(".mouse__cursor");
window.addEventListener("mousemove", (e) => {
cursor.style.left = e.clientX + "px";
cursor.style.top = e.clientY + "px";
});
// 1. ์๊ฐ ์ถ๋ ฅ
// function printTime() {
// const clock = document.querySelector(".time");
// const now = new Date();
// const nowTime = now.getFullYear() + "๋
" + (now.getMonth() + 1) + "์ " + now.getDate() + "์ผ " + now.getHours() + "์ " + now.getMinutes() + "๋ถ " + now.getSeconds() + "์ด";
// // getMonth()์ ๋ฐํ๊ฐ์ 0-11 ์ฌ์ด์. ๊ทธ๋์ 1์ ๋ํด์ค์ผ ํจ;; ์ ์ง ์๋ฌด๋ ๋ชจ๋ฆ...
// // ์ด์ : ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ํด๋นํ๋ ๋ฌ์ ์ด๋ฆ์ ๊ฐ์ ธ์ฌ ๋ ํธ๋ฆฌํ๋ค๊ณ ํจ
// clock.innerText = nowTime;
// setTimeout("printTime()", 1000);
// // 1์ด๋ง๋ค ์๊ฐ์ด ๋ฐ๋๋๋ก ํ๋ ค๋ฉด setTimeout ์ฌ์ฉํด์ผ ํจ
// }
// 1. ์๊ฐ ์ถ๋ ฅ
function printTime() {
const clock = document.querySelector(".time");
const now = new Date();
let hour = now.getHours();
let minute = now.getMinutes();
let second = now.getSeconds();
if (hour > 12) hour = hour - 12;
if (hour < 10) hour = `0${hour}`;
if (minute < 10) minute = `0${minute}`;
if (second < 10) second = `0${second}`;
const nowTime =
now.getFullYear() + "๋
" + (now.getMonth() + 1) + "์ " +
now.getDate() +
"์ผ " +
hour +
":" +
minute +
":" +
second;
clock.innerText = nowTime;
setTimeout("printTime()", 1000);
}
// 2. ํ์ฌ ์ฌ์ฉํ๊ณ ์๋ ํ๊ฒฝ
function printAgent() {
const agent = document.querySelector(".agent");
const os = navigator.userAgent.toLocaleLowerCase();
// agent.innerText = os;
// ์๋์ฐ ๊ธฐ์ค ์ถ๋ ฅ๋ฌผ
// mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/105.0.0.0 safari/537.36
if (os.indexOf("window") >= 0) {
// ๊ธฐ์ข
์ถ๋ ฅ
// ๊ธฐ์ข
๋ณ๋ก CSS๋ฅผ ๋ฌ๋ฆฌ ์ค์ ํ ์ ์์
agent.innerText = "ํ์ฌ windows๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ, ํ๋ฉด ํฌ๊ธฐ๋ " + screen.width + " * " + screen.height + "์
๋๋ค.";
document.querySelector("body").classList.add("window");
} else if (os.indexOf("macintosh") >= 0) {
agent.innerText = "ํ์ฌ mac์ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ, ํ๋ฉด ํฌ๊ธฐ๋ " + screen.width + " * " + screen.height + "์
๋๋ค.";
document.querySelector("body").classList.add("mac");
} else if (os.indexOf("iphone") >= 0) {
agent.innerText = "ํ์ฌ IPhone์ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ, ํ๋ฉด ํฌ๊ธฐ๋ " + screen.width + " * " + screen.height + "์
๋๋ค.";
document.querySelector("body").classList.add("iphone");
} else if (os.indexOf("android") >= 0) {
agent.innerText = "ํ์ฌ Android๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ, ํ๋ฉด ํฌ๊ธฐ๋ " + screen.width + " * " + screen.height + "์
๋๋ค.";
document.querySelector("body").classList.add("android");
}
}
// 3. ๊ฐ๊ฐ์ ํด๋ ๋๋๊ทธ ํจ๊ณผ, ๋๋๊ทธํ๋ฉด ๋ง์ฐ์ค์ ์์์ด ๋ณํํ๋๋ก
$(".icon1").draggable({
drag: function () {
$(".mouse__cursor img").attr("src", "../assets/img/game_cursor01.png");
},
});
$(".icon2").draggable({
drag: function () {
$(".mouse__cursor img").attr("src", "../assets/img/game_cursor02.png");
},
});
$(".icon3").draggable({
drag: function () {
$(".mouse__cursor img").attr("src", "../assets/img/game_cursor03.png");
},
});
$(".icon4").draggable({
drag: function () {
$(".mouse__cursor img").attr("src", "../assets/img/game_cursor04.png");
},
});
$(".icon5").draggable({
drag: function () {
$(".mouse__cursor img").attr("src", "../assets/img/game_cursor05.png");
},
});
$(".icon6").draggable({
drag: function () {
$(".mouse__cursor img").attr("src", "../assets/img/game_cursor06.png");
},
});
$(".music__wrap").draggable();
window.onload = function () {
printTime();
printAgent();
};
๐ก ๋ฎค์งํ๋ ์ด์ด
const allMusic = [
{
name: "BACK ON TOP (feat.Griff)",
artist: "HONNE",
img: "game_musicPlayer_img01",
audio: "music-1"
},
{
name: "Tie Me Down",
artist: "Gryffin, Elley Duhรฉ",
img: "game_musicPlayer_img02",
audio: "music-2"
},
{
name: "Angel Baby",
artist: "Troye Sivan",
img: "game_musicPlayer_img03",
audio: "music-3"
},
{
name: "Smells Like Me",
artist: "Charlie puth",
img: "game_musicPlayer_img04",
audio: "music-4"
},
{
name: "Nick Mono",
artist: "Rusty",
img: "game_musicPlayer_img05",
audio: "music-5"
},
{
name: "๋์ด",
artist: "LUCY",
img: "game_musicPlayer_img06",
audio: "music-6"
},
{
name: "Answer",
artist: "์",
img: "game_musicPlayer_img07",
audio: "music-7"
},
{
name: "๊ทธ๋ผ๋ฐ์ด์
",
artist: "10cm",
img: "game_musicPlayer_img08",
audio: "music-8"
},
{
name: "๋ด ๊ธฐ์จ์ ๋๊ฐ ๋ฒคํ๋ฆฌ๋ฅผ ๋๋ ๊ฑฐ์ผ",
artist: "๊น์น๋ฏผ",
img: "game_musicPlayer_img09",
audio: "music-9"
},
{
name: "Tea time (Feat.10cm)",
artist: "meenoi",
img: "game_musicPlayer_img10",
audio: "music-10"
}
]
// ์ ํ์
const musicWrap = document.querySelector(".music__wrap");
const musicView = musicWrap.querySelector(".music__view .img img");
const musicName = musicWrap.querySelector(".music__view .title h3");
const musicArtist = musicWrap.querySelector(".music__view .title p");
const musicAudio = musicWrap.querySelector("#main-audio");
const musicPlay = musicWrap.querySelector("#control-play");
const musicPrevBtn = musicWrap.querySelector("#control-prev");
const musicNextBtn = musicWrap.querySelector("#control-next");
const musicListBtn = musicWrap.querySelector("#control-list");
const musicList = musicWrap.querySelector(".music__list");
const musicListUl = musicWrap.querySelector(".music__list ul");
const musicProgress = musicWrap.querySelector(".progress");
const musicProgressBar = musicWrap.querySelector(".progress .bar");
const musicProgressCurrent = musicWrap.querySelector(".progress .timer .current");
const musicProgressDuration = musicWrap.querySelector(".progress .timer .duration");
const musicRepeat = musicWrap.querySelector("#control-repeat");
// ํ์ฌ์์
์ ์ธ๋ฑ์ค
let musicIndex = 1;
//์์
์ฌ์
function loadMusic(num){
musicName.innerText = allMusic[num-1].name; //๋ฎค์ง ์ด๋ฆ ๊ฐ์ ธ์ค๊ธฐ
musicArtist.innerText = allMusic[num-1].artist; //๋ฎค์ง ์ํฐ์คํธ ๊ฐ์ ธ์ค๊ธฐ
musicView.src = `../assets/img/${allMusic[num-1].img}.png`; //๋ฎค์ง ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ
musicView.alt = allMusic[num-1].name; //๋ฎค์ง ์ด๋ฏธ์ง alt ๊ฐ์ ธ์ค๊ธฐ
musicAudio.src = `../assets/audio/${allMusic[num-1].audio}.mp3`; //๋ฎค์ง ์ค๋์ค ๊ฐ์ ธ์ค๊ธฐ
}
//์ฌ์๋ฒํผ
function playMusic(){
//์์
ฉ๋ณ๊ฒฝ
musicWrap.classList.add("paused");
musicPlay.setAttribute("title", "์ ์ง");
musicPlay.setAttribute("class", "stop");
musicAudio.play();
}
//์ ์ง๋ฒํผ
function pauseMusic(){
//์์ฑ ๋ณ๊ฒฝ
musicWrap.classList.remove("paused");
musicPlay.setAttribute("title", "์ฌ์");
musicPlay.setAttribute("class", "play");
musicAudio.pause();
}
//์ด์ ๊ณก ๋ฃ๊ธฐ
function prevMusic(){
//musicIndex -- : ์์
์ด ์ฒซ ๊ณก์ผ ๋ ์์
์ ์ธ๋ฑ์ค ๊ฐ๊ณผ ๋ฐฐ์ด์ ๊ธธ์ด์ ๊ฐ๋ค๋ฉด ์ฐธ. ์๋ ๊ฒฝ์ฐ ์ด์ ๊ณก
musicIndex == 1 ? musicIndex = allMusic.length : musicIndex--;
loadMusic(musicIndex); //๋งค๊ฐ๋ณ์
playMusic(); //ํจ์ ์คํ
playListMusic(); //๋ฆฌ์คํธ ์
๋ฐ์ดํธ
}
//๋ค์ ๊ณก ๋ฃ๊ธฐ
function nextMusic(){
//musicIndex ++ : ์์
์ธ๋ฑ์ค๊ฐ ๋ฐฐ์ด์ ๊ธธ์ด์ ๊ฐ์ ๋ ์ฒซ๊ณก ์ฌ์ ์๋๋ฉด ๋ค์๊ณก ์ฌ์
musicIndex == allMusic.length ? musicIndex = 1 : musicIndex ++;
loadMusic(musicIndex); //๋งค๊ฐ๋ณ์
playMusic();
playListMusic(); //๋ฆฌ์คํธ ์
๋ฐ์ดํธ
}
//๋ฎค์ง ์งํ๋ฐ : currentTime
musicAudio.addEventListener("timeupdate", e => {
// console.log(e); ์์ ์ ๋ณด ๋ณด๊ธฐ
const currentTime = e.target.currentTime; //์ค๋์ค์ ์ด ๊ธธ์ด
const duration = e.target.duration; //ํ์ฌ ์ฌ์ ์๊ฐ
let progressWidth = (currentTime/duration) * 100; //์ ์ฒด ๊ธธ์ด์์ ํ์ฌ ์งํ๋๋ ์๊ฐ์ ๋ฐฑ๋ถ์๋ก ๋๋ ์ค๋๋ค.
musicProgressBar.style.width = `${progressWidth}%`;
//์ ์ฒด์๊ฐ
musicAudio.addEventListener("loadeddata", () => {
let audioDuration = musicAudio.duration;
let totalMin = Math.floor(audioDuration / 60); //์ ์ฒด ์๊ฐ(์ด)์ ๋ถ ๋จ์๋ก ์ชผ๊ฐญ๋๋ค. (Floor : ๋๋จธ์ง ๊ฐ ๋ฒ๋ฆผ)
let totalSec = Math.floor(audioDuration % 60); //๋จ์ ์ด๋ฅผ ์ ์ฅ
//ํ ํ ์ด๊ฐ 10๋ณด๋ค ์์ผ๋ฉด
if(totalSec < 10) totalSec = `0${totalSec}`; //์ด๊ฐ ํ ์๋ฆฟ์์ผ ๋ ์์ 0์ ๋ถ์ธ๋ค.
musicProgressDuration.innerText = `${totalMin}:${totalSec}`; //์์ฑ๋ ์๊ฐ์ ๋ฌธ์์ด๋ก ์ถ๋ ฅํ๊ธฐ
});
//์งํ์๊ฐ (ํ์ฌ ์ฌ์ ์๊ฐ)
let currentMin = Math.floor(currentTime / 60);
let currentSec = Math.floor(currentTime % 60);
if(currentSec < 10) currentSec = `0${currentSec}`;
musicProgressCurrent.innerText = `${currentMin}:${currentSec}`;
});
//์งํ๋ฐ ๋ฒํผ ํด๋ฆญ
musicProgress.addEventListener("click", (e) => {
let progressWidth = musicProgress.clientWidth; //์งํ๋ฐ ์ ์ฒด ๊ธธ์ด
let clickedOffsetX = e.offsetX; //์งํ๋ฐ ๊ธฐ์ค์ผ๋ก ์ธก์ ๋๋ X์ขํ
let songDuration = musicAudio.duration; //์ค๋์ค ์ ์ฒด ๊ธธ์ด
musicAudio.currentTime = (clickedOffsetX / progressWidth) * songDuration; //๋ฐฑ๋ถ์๋ก ๋๋ ์ซ์์ ๋ค์ ์ ์ฒด๊ธธ์ด๋ฅผ ๊ณฑํด์ ํ์ฌ ์ฌ์๊ฐ์ผ๋ก ๋ฐ๊พผ๋ค.
});
//๋ฐ๋ณต ๋ฒํผ ํด๋ฆญ
musicRepeat.addEventListener("click", () => {
let getAttr = musicRepeat.getAttribute("class");
switch(getAttr){
case "repeat" :
musicRepeat.setAttribute("class", "repeat_one");
musicRepeat.setAttribute("title", "ํ ๊ณก ๋ฐ๋ณต");
break;
case "repeat_one" :
musicRepeat.setAttribute("class", "shuffle");
musicRepeat.setAttribute("title", "๋๋ค ๋ฐ๋ณต");
break;
case "shuffle" :
musicRepeat.setAttribute("class", "repeat");
musicRepeat.setAttribute("title", "์ ์ฒด ๋ฐ๋ณต");
break;
}
});
//์ค๋์ค๊ฐ ๋๋ ๊ฒฝ์ฐ
musicAudio.addEventListener("ended", () => {
let getAttr = musicRepeat.getAttribute("class");
switch(getAttr){
case "repeat" :
nextMusic();
break;
case "repeat_one" :
playMusic();
break;
case "shuffle" :
//๋ฐฐ์ด์ ์ด ๊ธธ์ด์์ ๋๋ค์ผ๋ก ์ธ๋ฑ์ค ๊ฐ์ ์ถ์ถํ๋ค. 0๋ถํฐ 1๊น์ง์ ์๋ฅผ 10์ ๊ณฑํ์ฌ ์์ฐ์๋ก ๋ง๋ค์ด ์ค๋ค. ๊ทธ๋ฆฌ๊ณ floor๋ก ๋๋จธ์ง ๊ฐ์ ๋ฒ๋ฆฐ๋ค. ๋ง์ง๋ง์ผ๋ก +1์ ํ์ฌ ๋ฐฐ์ด๊ณผ ์ธ๋ฑ์ค ๊ฐ์ ๋ง์ถฐ์ค๋ค. ๊ทธ๋ฌ๋ฉด 1๋ถํฐ 10 ๊น์ง ๊ฐ์ด ๋์จ๋ค.
let randomIndex = Math.floor(Math.random() * allMusic.length + 1); //๋๋ค ์ธ๋ฑ์ค ์์ฑ
do {
randomIndex = Math.floor(Math.random() * allMusic.length + 1);
} while (musicIndex == randomIndex)
musicIndex = randomIndex; //ํ์ฌ ์ธ๋ฑ์ค๋ฅผ ๋๋ค ์ธ๋ฑ์ค๋ก ๋ณ๊ฒฝ
loadMusic(musicIndex); //๋๋ค ์ธ๋ฑ์ค๊ฐ ๋ฐ์๋ ํ์ ์ธ๋ฑ์ค ๊ฐ์ผ๋ก ์์
์ ๋ค์ ๋ก๋ํ๋ค.
playMusic(); //๋ก๋ํ ์์
์ ์ฌ์
break;
}
playListMusic(); //์ฌ์ ๋ชฉ๋ก ์
๋ฐ์ดํธ
});
//ํ๋ ์ด ๋ฒํผ
//์ฌ์ ๋ฒํผ ํด๋ฆญํ์ ๋ ํจ์ ์คํ
//pause - ํ๋ ์ด ๋ฒํผ์ ๋๋ฅด๋ฉด ์ ์ง ๋ฒํผ์ด ๋์์ผ ํ๋ค.
musicPlay.addEventListener("click", () => {
//ํด๋์ค๋ฅผ ์ถ๊ฐ์์ผ pause๊ฐ ์๋ ๊ฒฝ์ฐ ์ ์งํ๊ณ ์๋ ๊ฒฝ์ฐ ์ฌ์์ํค๊ธฐ
const isMusicPaused = musicWrap.classList.contains("paused"); //์์
์ฌ์ ์ค
isMusicPaused ? pauseMusic() : playMusic();
});
//์ด์ ๊ณก ๋ฒํผ ํด๋ฆญ
musicPrevBtn.addEventListener("click", () => {
prevMusic();
});
//๋ค์๊ณก ๋ฒํผ ํด๋ฆญ
musicNextBtn.addEventListener("click", () => {
nextMusic();
});
//๋ฎค์ง ๋ฆฌ์คํธ ๋ฒํผ
musicPlay.addEventListener("click", () => {
musicList.classList.add("show");
});
// ๋ฎค์ง ๋ฆฌ์คํธ ๊ตฌํํ๊ธฐ (์์ ์ )
// for(let i=0; i < allMusic.length; i++){
// let li = `
// <li data-index="${i + 1}">
// <strong>${allMusic[i].name}</strong>
// <em>${allMusic[i].artist}</em>
// <audio class="${allMusic[i].audio}" src="../assets/audio/${allMusic[i].audio}.mp3"></audio>
// <span class="audio-duration" id="${allMusic[i].audio}">์ฌ์์๊ฐ</span>
// </li>
// `;
// // musicListUl.innerHTML += li; : ํ ๋ฒ์ ๋ก๋ฉํด์ ๊ฐ์ ์ธ์ํ์ง ๋ชปํจ.
// musicListUl.insertAdjacentHTML("beforeend", li); //
// //๋ฆฌ์คํธ์ ์์
์๊ฐ ๋ถ๋ฌ์ค๊ธฐ
// let liAudioDration = musicListUl.querySelector(`${allMusic[i].audio}`); //๋ฆฌ์คํธ์์ ์๊ฐ์ ํ์ํ ์ ํ์๋ฅผ ๊ฐ์ ธ์จ๋ค
// let liAudio = musicListUl.querySelector(`${allMusic[i].audio}`); //๋ฆฌ์คํธ์์ ์ค๋์ค๋ฅผ ๊ฐ์ ธ์ด
// liAudio.addEventListener("loadeddata", () => {
// let audioDuration = liAudio.duration; //์ค๋์ค ์ ์ฒด ๊ธธ์ด
// let totalMin = Math.floor(audioDuration / 60); //์ ์ฒด ๊ธธ์ด๋ฅผ ๋ถ ๋จ์๋ก ์ชผ๊ฐ๊ธฐ
// let totalSec = Math.floor(audioDuration % 60); //์ด ๊ณ์ฐํ๊ธฐ
// if(totalSec < 10) totalSec = `0${totalSec}`; //์์๋ฆฌ์ 0์ถ๊ฐํ๊ธฐ
// liAudioDration.innerText = `${totalMin}:${totalSec}`; //์๊ฐ ๋ฌธ์์ด๋ก ์ถ๋ ฅํ๊ธฐ
// liAudioDration.setAttribute("data-duration", `${totalMin}:${totalSec}`); //์์ฑ์ ์ค๋์ค ๊ธธ์ด ๊ธฐ๋ก
// });
// }
// ๋ฎค์ง ๋ฆฌ์คํธ ๊ตฌํํ๊ธฐ (์์ ํ)
for (let i = 0; i < allMusic.length; i++) {
let li = `
<li data-index="${i + 1}">
<strong>${allMusic[i].name}</strong>
<em>${allMusic[i].artist}</em>
<audio class="${allMusic[i].audio}" src="../assets/audio/${allMusic[i].audio}.mp3"></audio>
<span class="audio-duration" id="${allMusic[i].audio}">์ฌ์์๊ฐ</span>
</li>
`;
// musicListUl.innerHTML += li; ์ด๋ ๊ฒ ํ๋ฉด ํ ๋ฒ์ ๋ก๋ฉํด์ ๊ฐ์ ์ธ์ํ์ง ๋ชปํ์ฌ ๋ง์ง๋ง๊ณก๋ง ์ฌ์์๊ฐ ๋์ด
musicListUl.insertAdjacentHTML("beforeend", li); // ์ด๋ ๊ฒ ํด์ผ ๋ค ๋์ด!!
// insertAdjacentHTML :
// beforeend : element ์์ ๊ฐ์ฅ ๋ง์ง๋ง child
// ๋ฆฌ์คํธ์ ์ฌ์ ์๊ฐ ๋ถ๋ฌ์ค๊ธฐ
let liAudioDuration = musicListUl.querySelector(`#${allMusic[i].audio}`); // ๋ฆฌ์คํธ์์ ์๊ฐ์ ํ์ํ ์ ํ์๋ฅผ ๊ฐ์ ธ์ด
let liAudio = musicListUl.querySelector(`.${allMusic[i].audio}`); // ๋ฆฌ์คํธ์์ ์ค๋์ค๋ฅผ ๊ฐ์ ธ์ด
liAudio.addEventListener("loadeddata", () => {
let audioDuration = liAudio.duration; // ์ค๋์ค ์ ์ฒด ๊ธธ์ด
let totalMin = Math.floor(audioDuration / 60); // ์ ์ฒด ๊ธธ์ด๋ฅผ ๋ถ ๋จ์๋ก ์ชผ๊ฐฌ
let totalSec = Math.floor(audioDuration % 60); // ์ด
if (totalSec < 10) totalSec = `0${totalSec}`; // ์ด ์ ์๋ฆฌ์ 0 ์ถ๊ฐ
liAudioDuration.innerText = `${totalMin}:${totalSec}`; // ๋ฌธ์์ด ์ถ๋ ฅ
liAudioDuration.setAttribute("data-duration", `${totalMin}:${totalSec}`); // ์์ฑ์ ์ค๋์ค ๊ธธ์ด ๊ธฐ๋ก
});
}
//๋ฎค์ง ๋ฆฌ์คํธ๋ฅผ ํด๋ฆญํ๋ฉด ์ฌ์ - ๋๋ ์ ๋ ์๋์ ํจ์๊ฐ ์คํ๋๊ฒ ํด์ค๋ค.
function playListMusic(){
const musicListAll = musicListUl.querySelectorAll("li"); //๋ฎค์ง ๋ฆฌ์คํธ ๋ชฉ๋ก
for( let i=0; i<musicListAll.length; i++){
let audioTag = musicListAll[i].querySelector(".audio-duration");
if(musicListAll[i].classList.contains("playing")){ //ํด๋์ค๊ฐ ์กด์ฌํ๋ฉด ์ญ์
musicListAll[i].classList.remove("playing");
let adDuration = audioTag.getAttribute("data-duration"); //์ค๋์ค ๊ธธ์ด ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
audioTag.innerText = adDuration; //์ค๋์ค ๊ธธ์ด ๊ฐ ์ถ๋ ฅ
}
if(musicListAll[i].getAttribute("data-index") == musicIndex){ //ํ์ฌ ๋ฎค์ง ์ธ๋ฑ์ค๋ ๋ฆฌ์คํธ ์ธ๋ฑ์ค ๊ฐ์ด ๊ฐ์ผ๋ฉด
musicListAll[i].classList.add("playing"); //ํด๋์ค playing ์ถ๊ฐ
audioTag.innerText = "์ฌ์์ค"; // ์ฌ์์ค์ผ ๊ฒฝ์ฐ ์ฌ์ ์ค ๋ฉํธ ์ถ๊ฐ
}
// this : ์๊ธฐ์์ ์ ์๋ฏธ
musicListAll[i].setAttribute("onclick", "clicked(this)"); // setAttribute : ์์์ ์์ฑ ๋ถ์ด๊ธฐ! (์ : ์์ฑ๋ช
/ ๋ค : ์์ฑ๊ฐ)
}
}
//๋ฎค์ง ๋ฆฌ์คํธ๋ฅผ ํด๋ฆญํ๋ฉด - ํด๋ฆญํ๋ฉด ์ฌ์๋๋๋ก ํจ.
function clicked(el){
let getLiIndex = el.getAttribute("data-index"); //ํด๋ฆญํ ๋ฎค์ง ๋ฆฌ์คํฌ์ ์ธ๋ฑ์ค ๊ฐ์ ์ ์ฅํ๋ค.
musicIndex = getLiIndex; //ํด๋ฆญํ ์ธ๋ฑ์ค ๊ฐ์ ๋ฎค์ง ์ธ๋ฑ์ค์ ์ ์ฅํ๋ค.
loadMusic(musicIndex); //ํด๋น ์ธ๋ฑ์ค ๋ฎค์ง์ ๋ก๋ํ๋ค.
playMusic(); // ์์
์ ์ฌ์
playListMusic(); //์์
์ด ์ถ๊ฐ ๋ ๋ ๋ฆฌ์คํธ๋ฅผ ์
๋ฐ์ดํธ ์ํจ๋ค.
}
window.addEventListener("load", () => {
loadMusic(musicIndex); //์์
์ฌ์
playListMusic(); //๋ฆฌ์คํธ ์ด๊ธฐํ
});
//๋ฎค์ง ๋ฆฌ์คํธ ์ด๊ธฐ, ๋ซ๊ธฐ
const closeBtn = document.querySelector(".close-btn");
closeBtn.addEventListener("click", () => {
musicList.style.display = "none";
});
musicListBtn.addEventListener("click", () => {
musicList.style.display = "block";
});
//๋ณผ๋ฅจ์กฐ์
const soundBtn = document.querySelector("#control-sound");
const sound = document.querySelector("#player_sound");
let i = 0;
soundBtn.addEventListener("click", () => {
if(i==0){
sound.classList.add("open");
i++;
}else{
sound.classList.remove("open");
i--;
}
});
sound.addEventListener("change", function(e) {
musicAudio.volume = this.value/15;
});
๋๊ธ