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

[jQuery] ์Šคํƒ€์ผ ๊ด€๋ จ ๋ฉ”์„œ๋“œ ์•Œ์•„๋ณด๊ธฐ

by ์ฝ”๋”ฉ๊ณต์ฑ… 2022. 9. 3.
๋ฐ˜์‘ํ˜•

jQuery : ์Šคํƒ€์ผ ๊ด€๋ จ ๋ฉ”์„œ๋“œ

์ œ์ด์ฟผ๋ฆฌ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์Šคํƒ€์ผ ๊ด€๋ จ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค.


css() ๋ฉ”์„œ๋“œ

์‹คํ–‰ ๋ถ„๋ฅ˜ ํ˜•์‹
์ทจ๋“ $("div").css("width");
์ƒ์„ฑ, ๋ณ€๊ฒฝ $("div").css("background-color", "red").css("padding", "10px");
$("div").css({background-color: "red", padding: "10px"});
์ฝœ๋ฐฑ ํ•จ์ˆ˜ $("div").css("width", function(index, .w) {
ใ€€// index๋Š” ๊ฐ div ์š”์†Œ์˜ index 0,1,2
ใ€€// w์€ ๊ฐ div ์š”์†Œ width ์†์„ฑ
return css ์†์„ฑ // ๊ฐ div ์š”์†Œ์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•จ.
});
โ€ฆโ€ฆ
<div>๋‚ด์šฉ</div>
<div>๋‚ด์šฉ</div>
<div>๋‚ด์šฉ</div>

css() ๋ฉ”์„œ๋“œ : ์˜ˆ์ œ ์ฝ”๋“œ

<!DOCTYPE html> 
<html lang="ko"> 
<head> 
    <meta charset="UTF-8"> 
    <title>css() ๋ฉ”์„œ๋“œ<title> 
    <style> 
        * {
            margin: 0;
            padding: 0;
        }
        div:nth-child(1) {
            background: red;
        }
        div:nth-child(2) {
            background: green;
        }
        div:nth-child(3) {
            background: blue;
        }
    </style> 
    <script src="jquery-3.3.1.min.js"></script> 
    <script> 
        $(document).ready(function() {
            $("div").eq(0).css({padding: 10px, "text-align": "center"});
            $("div").css("width", function(index) {
                return index * 100 + 100; // 100,200,300
            });
        });
    </script> 
</head> 
<body> 
    <div>๋‚ด์šฉ1</div>
    <div>๋‚ด์šฉ2</div>
    <div>๋‚ด์šฉ3</div>
</body> 
</html>

์ฝ”๋“œํŽœ ๊ฒฐ๊ณผ


width, height ๊ด€๋ จ ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์ข…๋ฅ˜ ์„ค๋ช…
width() ์š”์†Œ์˜ ๊ฐ€๋กœ ๊ธธ์ด๋ฅผ ์ทจ๋“, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
innerWidth() padding์ด ์ ์šฉ๋œ ์š”์†Œ์˜ ๊ฐ€๋กœ ๊ธธ์ด๋ฅผ ์ทจ๋“, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
outerWidth() border์™€ margin์ด ์ ์šฉ๋œ ์š”์†Œ์˜ ๊ฐ€๋กœ ๊ธธ์ด๋ฅผ ์ทจ๋“, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ.
outerWidth()๋Š” ์š”์†Œ์˜ width๊ฐ’ + ์ขŒ/์šฐ border ๊ฐ’
outerWidth(true)๋Š” ์š”์†Œ์˜ width๊ฐ’ + ์ขŒ/์šฐ border๊ฐ’ + ์ขŒ/์šฐ margin๊ฐ’
height() ์š”์†Œ์˜ ๋†’์ด๋ฅผ ์ทจ๋“, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
innerHeight() padding์ด ์ ์šฉ๋œ ์š”์†Œ์˜ ๋†’์ด๋ฅผ ์ทจ๋“, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
outerHeight() border์™€ margin์ด ์ ์šฉ๋œ ์š”์†Œ์˜ ๋†’์ด๋ฅผ ์ทจ๋“, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ.
outerHeight()๋Š” ์š”์†Œ์˜ height๊ฐ’ + ์ƒ/ํ•˜ border ๊ฐ’
outerHeight(true)๋Š” ์š”์†Œ์˜ height๊ฐ’ + ์ƒ/ํ•˜ border๊ฐ’ + ์ƒ/ํ•˜ margin๊ฐ’

width, height ๊ด€๋ จ ๋ฉ”์„œ๋“œ : ์˜ˆ์ œ ์ฝ”๋“œ

<!DOCTYPE html> 
<html lang="ko"> 
<head> 
    <meta charset="UTF-8"> 
    <title>width, height ๋ฉ”์„œ๋“œ<title> 
    <style> 
        * {
            margin: 0;
            padding: 0;
        }
        div {
            padding: 20px;
            margin: 20px;
            background: #ff6600;
        }
    </style> 
    <script src="jquery-3.3.1.min.js"></script> 
    <script> 
        $(document).ready(function() {
            $("div").width(150).height(150);
            console.log("width 150" + $("div").width());
            console.log("height " + $("div").height());
            console.log("innerWidth " + $("div").innerWidth());
            console.log("innerHeight " + $("div").innerHeight());
            console.log("outerWidth " + $("div").outerWidth(true));
            console.log("outerHeight " + $("div").outerHeight(true));
        });
    </script> 
</head> 
<body> 
    <div>๋‚ด์šฉ</div>
</body> 
</html>

์ฝ”๋“œํŽœ ๊ฒฐ๊ณผ


์œ„์น˜ ๊ด€๋ จ ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์ข…๋ฅ˜ ์„ค๋ช…
offset() $("div").offset().left
$("div").offset().top
$("div").offset({left: 10, top: 10})
html ๊ธฐ์ค€์œผ๋กœ left, top ๊ฐ’์„ ์ทจ๋“, ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Œ.
position() $("div").position().left
$("div").position().top
๋ถ€๋ชจ ์š”์†Œ ๊ธฐ์ค€์œผ๋กœ left, top ๊ฐ’์„ ์ทจ๋“ํ•  ์ˆ˜ ์žˆ์Œ.

์œ„์น˜ ๊ด€๋ จ ๋ฉ”์„œ๋“œ : ์˜ˆ์ œ ์ฝ”๋“œ

<!DOCTYPE html> 
<html lang="ko"> 
<head> 
    <meta charset="UTF-8"> 
    <title>์œ„์น˜ ๊ด€๋ จ ๋ฉ”์„œ๋“œ ๋ฉ”์„œ๋“œ<title> 
    <style> 
        * {
            margin: 0;
            padding: 0;
        }
        #outer {
            width: 500px;
            height: 500px;
            margin: 50px;
            position: relative;
            background: #ff6600;
        }
        #inner {
            width: 100px;
            height: 100px;
            position: absolute;
            left: 80px;
            top: 50px;
            background: #ccc;
        }
    </style> 
    <script src="jquery-3.3.1.min.js"></script> 
    <script> 
        $(document).ready(function() {
            console.log($("#inner").offset().left);
            console.log($("#inner").offset().top);
            console.log($("#inner").position().left);
            console.log($("#inner").position().top);
        });
    </script> 
</head> 
<body>
    <div id="outer">
        <div id="inner">๋‚ด์šฉ</div>
    </div>
</body> 
</html>
๊ฒฐ๊ณผ ๋ณด๊ธฐ
130
100
80
50

์ฝ”๋“œํŽœ ๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

JavaScript
HTML
CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.