상세 컨텐츠

본문 제목

[자바스크립트] 버튼눌러서 속성변경, 클래스리스트토글 상자색

카테고리 없음

by esoesmio 2023. 5. 23. 02:24

본문

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <style>
.box{
    width: 100px;
    height: 100px;
    background-color: aqua;
    position: absolute;
    top: 40px;
    left: 40px;
}
.click{
    width: 500px;
    height: 500px;
    background-color: #ff2200;
}
    </style>
</head>
<body>
<div class="box">
    <button class="btn" id="btn1">클릭</button>
    <button class="btn" id="btn2">더블클릭</button>
</div>


</body>

<script>
    let status1 = false;
    let status2 = false;
const box = document.querySelector(".box");
const btn1 = document.querySelector("#btn1")
const btn2 = document.querySelector("#btn2")
btn1.addEventListener("click",()=>{
    // if(!status1) {
    //     box.style.width = "300px";
    //     box.style.height = "300px";
    // }
    // if(status1){
    //     box.style.width = "100px";
    //     box.style.height = "100px";
    // }
    // status1 = !status1
    //
    box.classList.toggle("click");
});
btn2.ondblclick = () =>{
    if(!status2){
    box.style.marginLeft="200px";
    box.style.marginTop = "200px";
    // return;
    //리턴하면 메소드가 종료된다.
    }
    if(status2){
        box.style.marginLeft="0px";
        box.style.marginTop ="0px";
    }
    status2 = !status2
}

btn1.addEventListener('mouseenter',()=>{
btn1.textContent='꺼지라';
    btn1.style.color="red";
btn1.setAttribute('color','red');

});
    btn1.addEventListener('mouseleave',()=>{
        btn1.textContent='미안해';
        btn1.style.color="red";
        btn1.setAttribute('color','red');

    });





    // box.onmouseover = ()=>{
    //     box.style.background='orange'
    // }
    // box.onmouseout = () => {
    //     box.style.background = 'skyblue';
    // }



    btn2.addEventListener('mouseenter',()=>{
        btn2.textContent='너도 꺼지라';
        btn2.style.color="gold";
    });
    btn2.addEventListener('mouseleave',()=>{
        btn2.textContent='미안해2';
        btn2.style.color="gold";
    });
//box 마우스오버 시 배경색 오렌지
//box 마우스아웃 시 배경식 오렌지블루




</script>
</html>

댓글 영역