[HTML | CSS | JavaScript | jQuery] 정리/JavaScript

JavaScript - CSS 상자 크기 조절

쎈코 2023. 5. 8. 00:39

<!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>Document</title>
    <link rel="stylesheet" href="css/example.css">
    <style>
        #box {
            /* background-image: url(images/corner.png); */
            background-repeat: no-repeat;
            background-position: 99% 99%;
        }
    </style>
</head>
<body>

     <!-- 박스 안움직임. 다시보기 코드 다시 달라하기. 보류 -->
    <h1>test</h1>

    <div id="box" class="box bg-yellow" style="width: 200px; height: 200px;"></div>

    <hr>
    <textarea rows="5"></textarea>


    <script>
        const box = document.getElementById('box');
        let isDown = false;
       
        box.onmouseout = function() {
            event.target.style.backgroundImage = 'url()';
            event.target.style.cursor = 'default';    

            window.onmousedown = null;
        };

        box.onmousemove = function() {
            if ((event.offsetX >= parseInt(event.target.style.width) - 20) && (event.offsetY >= parseInt(event.target.style.height) -20)) {
                // event.target.style.backgroundColor = 'tomato';
                event.target.style.backgroundImage = 'url(images/corner.png)';
                event.target.style.cursor = 'nw-resize';

                window.onmousedown = function() {
                    if (event.target.id == 'box') {
                        isDown = true;
                    }
                };
            } else {
                event.target.style.backgroundImage = 'url()';
                event.target.style.cursor = 'default';

                window.onmousedown = null;
            }
        };

        window.onmousedown = function() {
            if (event.target.id == 'box') {
                isDown = true;
            }
        };

        window.onmousemove = function() {
            if (isDown) {
                box.style.width = event.clientX + 'px';
                box.style.height = event.clientY + 'px';
            }
        };

        window.onmouseup = function() {
            isDown = false;
        };

    </script>
</body>
</html>