๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

[EFFECT] ๊ฒŒ์ž„ ์ดํŽ™ํŠธ01 : ๋ ˆํŠธ๋กœ st ๊ตฌ์„ฑํ•˜๊ธฐ

by _ํ† ๋งคํ†  2022. 10. 24.
728x90

๐ŸŽฎ ๊ฒŒ์ž„ ํšจ๊ณผ 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;
});

#3. ๊ฒฐ๊ณผ

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css