IT/development

[bootstrap] bootstrap modal์ฐฝ ๋„์šฐ๊ธฐ

์•Œ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž 2022. 12. 8.

๋ชฉ์ฐจ

    bootstrap modal ๐Ÿ˜„

    ์—ญ์‹œ ๋ฏธ๋ž˜์˜ ๋‚ด๊ฐ€ ๋ณด๊ธฐ ์œ„ํ•ด ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์„ ์ด์šฉํ•ด์„œ ๋ชจ๋‹ฌ์ฐฝ์„ ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•์„ ๊ธฐ๋กํ•œ๋‹ค.

    html ์ฝ”๋“œ์™€ js๋กœ modal์„ ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ html๋งŒ์œผ๋กœ ๋„์šด๋‹ค.

    js๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.

    button์— id๊ฐ’ ์ฃผ๊ณ  ํ•ด๋‹น ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ $("#๋ชจ๋‹ฌ์•„์ด๋””").modal("show"); ์ด๋ ‡๊ฒŒ ์ด๋ฒคํŠธ๋ฅผ ์ค€๋‹ค.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    	<script  src="http://code.jquery.com/jquery-latest.min.js"></script>
    	<title>modal-test</title>
    </head>
    <body>						
    <button class="btn btn-danger" type="button" id="btn">์ˆ˜์ •</button>
    <!-- div modal์ฐฝ ์ƒ๋žต --> 
    <script>
    	$(document).ready(function() {
    		$("#btn").on("click", function(){
    			$("#userModal").modal("show");
    		});
    	});
    </script>	
    </body>
    </html>

    ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ: ๋ถ€ํŠธ์ŠคํŠธ๋žฉ 5.2.3๋ฒ„์ „

    ์ˆ˜์ • ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด id๊ฐ€ "userModal"์ธ ๋ชจ๋‹ฌ์ฐฝ์„ ๋„์šฐ๋Š” ์˜ˆ์ œ์ด๋‹ค.

    bootstrap.min.css, bootstrap.bundle.min.css 2๊ฐœ์˜ ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ˆ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ ํ•˜๊ฑฐ๋‚˜ CDN์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ฑฐ๋‚˜ ํ•˜๋ฉด ๋œ๋‹ค.

    ์•„๋ž˜ ๋งํฌ์—์„œ ๋‹ค์šด๋กœ๋“œ or CDN

     

    Download

    Download Bootstrap to get the compiled CSS and JavaScript, source code, or include it with your favorite package managers like npm, RubyGems, and more.

    getbootstrap.com

    12 line์˜ data-bs-toggle="modal" data-bs-target="#userModal" ์†์„ฑ์ด ์—†์œผ๋ฉด ๋ชจ๋‹ฌ์ด ๋œจ์ง€ ์•Š๋Š”๋‹ค.

    ์ง๊ด€์ ์œผ๋กœ ๋ณด๊ธฐ์—๋„ toggleํ˜•์‹์œผ๋กœ modal์„ ๋„์šด๋‹ค๋Š” ์†Œ๋ฆฌ๊ณ  ๋Œ€์ƒ์€ #userModal(id="userModal")์„ ํ† ๊ธ€ํ™”ํ•œ๋‹ค๋Š” ์†Œ๋ฆฌ๋กœ ์ดํ•ด๋œ๋‹ค.

    ๋ชจ๋‹ฌ์€ header, body, footer๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋‹ˆ ์ ์ ˆํžˆ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

    34 line์˜ data-bs-dismiss="modal" ์†์„ฑ์€ ๋ชจ๋‹ฌ์ฐฝ์„ ๋‹ซ๋Š” ์ด๋ฒคํŠธ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

    ์ด ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ณ  ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ฐ€์ด๋“œ ๋ณด๊ณ  ํ•˜๋ฉด ๋œ๋‹ค.

    ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ฒจ๋ถ€ํ•œ๋‹ค.

    See the Pen Untitled by SangYeop Lee (@devLsy) on CodePen.

    reference: https://getbootstrap.com/docs/5.2/components/modal/

     

    Modal

    Use Bootstrap’s JavaScript modal plugin to add dialogs to your site for lightboxes, user notifications, or completely custom content.

    getbootstrap.com

    ๋Œ“๊ธ€