๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/Vanilla JS

[๋ฐ”๋‹๋ผJS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ] DOM - If else - Function practice Two

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 26.
๋ฐ˜์‘ํ˜•

[์ถœ์ฒ˜-์œ ํŠœ๋ธŒ ๋…ธ๋งˆ๋“œ ์ฝ”๋” Nomad Coders]

https://youtu.be/S4BN1tZmmWw

 

index.js

const title = document.querySelector("#title");

const CLICKED_CLASS = "clicked";

function handleClick(){
    const currentClass = title.className;
    if(currentClass !== CLICKED_CLASS){
        title.className = CLICKED_CLASS;
    } else {
        title.className = "";
    }
}

function init(){
    title.addEventListener("click", handleClick);
}

init();

 

index.css (css๋Š” ๊ณ ์ •)

body {
    background-color: #ecf0f1;
}

.btn {
    cursor: pointer;
}

h1 {
    color: #34495e;
    transition: color 0.5s ease-in-out;
}

.clicked{
    color: #7f8c8d;
}

 

 

const title = document.querySelector("#title");

const CLICKED_CLASS = "clicked";

function handleClick(){
    const currentClass = title.className;
    if(currentClass !== CLICKED_CLASS){
        title.classList.add(CLICKED_CLASS);
    } else {
        title.classList.remove(CLICKED_CLASS);
    }
}

function init(){
    title.addEventListener("click", handleClick);
}

init();

์ฒ˜์Œ ํด๋ฆญํ–ˆ์„ ์‹œ์—๋งŒ ๋ณ€๊ฒฝ๋˜๊ณ  ๊ทธ ํ›„๋ก  ํด๋ฆญํ•ด๋„ ๋ณ€ํ•˜์ง€์•Š์Œ!!

 

 

-toggle์€ on/off ์Šค์œ„์น˜์™€ ๋น„์Šทํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค

const title = document.querySelector("#title");

const CLICKED_CLASS = "clicked";

function handleClick() {
   title.classList.toggle(CLICKED_CLASS);
}

function init(){
    title.addEventListener("click", handleClick);
}

init();

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€